docs.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var Docs = {
  2. anchorsPath: '../Docs/index.html',
  3. scriptsJson: '../scripts.json',
  4. start: function(){
  5. if (location.protocol == 'file:') Docs.local();
  6. Docs.loadScripts();
  7. },
  8. loadScripts: function(){
  9. Docs.Scripts = new Hash();
  10. new Request({ link: 'cancel', onSuccess: function(json){
  11. var scripts = JSON.decode(json);
  12. $each(scripts, function(files, dir) {
  13. Docs.Scripts.set(dir, $H(files).getKeys());
  14. });
  15. Docs.process();
  16. }
  17. }).get(Docs.scriptsJson);
  18. },
  19. process: function(){
  20. var menu = $('menu-wrapper'), elements = [], files;
  21. var request = new Request({ link: 'cancel', onSuccess: Docs.update });
  22. // selectors buggy, hack varible current
  23. var current;
  24. ////
  25. Docs.Scripts.each(function(scripts, folder){
  26. var head = new Element('h2', { 'text': folder });
  27. var list = new Element('ul', { 'class': 'folder' });
  28. list.adopt(scripts.map(function(script){
  29. var file = new Element('h3').adopt(new Element('a', {
  30. 'text': script,
  31. 'href': '#' + folder + '/' + script,
  32. 'events': {
  33. 'click': function(){
  34. $('docs-wrapper').empty().set('html', '<h2>Loading...</h2>');
  35. files.removeClass('selected');
  36. file.addClass('selected');
  37. request.get(this.get('href').split('#')[1] + '.md');
  38. }
  39. }
  40. }));
  41. /////
  42. if(('#' + folder + '/' + script) == window.location.hash){
  43. current=file.getFirst();
  44. }
  45. /////
  46. return new Element('li').adopt(file);
  47. }));
  48. elements.push(head);
  49. elements.push(list);
  50. });
  51. files = menu.adopt(elements).getElements('h3');
  52. ////
  53. (current || document.getElement('#menu a[href=' + window.location.hash + ']') || document.getElement('#menu a')).fireEvent('click');
  54. },
  55. local: function() {
  56. Request.implement({
  57. getXHR: function(){
  58. return (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
  59. },
  60. isSuccess: function() {
  61. return (!this.status || (this.status >= 200) && (this.status < 300));
  62. }
  63. });
  64. },
  65. update: function(markdown){
  66. var wrapper = $('docs-wrapper'), submenu = $('submenu');
  67. if (!submenu) submenu = new Element('div').set('id', 'submenu');
  68. var parse = Docs.parse(markdown);
  69. wrapper.set('html', parse.innerHTML);
  70. document.getElement('#menu-wrapper h3.selected').getParent().grab(submenu.empty());
  71. var methods = Docs.methods(parse, submenu);
  72. Docs.scroll();
  73. },
  74. parse: function(markdown){
  75. var html = ShowDown(markdown);
  76. var temp = new Element('div').set('html', html);
  77. var anchor = (/\{#(.*)\}/);
  78. temp.getElements('h1, h2, h3, h4, h5, h6').each(function(h){
  79. var matches = h.innerHTML.match(anchor);
  80. if (matches) h.set('id', matches[1]);
  81. h.innerHTML = h.innerHTML.replace(anchor, '');
  82. });
  83. var heading = temp.getElement('h1');
  84. if (heading) heading.set('class', 'first');
  85. return temp;
  86. },
  87. methods: function(parse, wrapper) {
  88. var headers = parse.getElements('h1');
  89. var anchors = parse.getElements('h2[id]');
  90. headers.each(function(header, i) {
  91. var group = new Element('ul').inject(wrapper);
  92. var head = header.get('text').split(':');
  93. head = (head.length == 1) ? head[0] : head[1];
  94. var section = header.id.split(':')[0];
  95. var lnk = '<a href="' + Docs.anchorsPath + '#' + header.id+ '">' + head + '</a>';
  96. new Element('li').set('html', lnk).inject(group);
  97. var subgroup = new Element('ul', {'class': 'subgroup'}).inject(group);
  98. anchors.each(function(anchor) {
  99. var sep = anchor.id.match(':');
  100. var subSection = anchor.id.split(':')[0];
  101. if (section == subSection || (!i && !sep)) {
  102. var method = anchor.get('text').replace(section, '');
  103. lnk = '<a href="' + Docs.anchorsPath + '#' + anchor.id + '">' + method + '</a>';
  104. new Element('li').set('html', lnk).inject(subgroup);
  105. }
  106. });
  107. });
  108. },
  109. scroll: function() {
  110. if (!Docs.scrolling) Docs.scrolling = new Fx.Scroll('docs', {'offset': {x: 0, y: -4}});
  111. $$('#submenu a').each(function(anchor) {
  112. anchor.addEvent('click', function(e) {
  113. e.stop();
  114. var lnk = $(anchor.href.split('#')[1]);
  115. Docs.scrolling.toElement(lnk);
  116. });
  117. });
  118. }
  119. };
  120. var ShowDown = function(text){
  121. return new Showdown.converter().makeHtml(text);
  122. };
  123. window.addEvent('domready', Docs.start);