You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
5.3 KiB

  1. // namespace
  2. window.semantic = {
  3. handler: {}
  4. };
  5. // ready event
  6. semantic.ready = function() {
  7. // selector cache
  8. var
  9. $ui = $('.ui').not('.hover, .down'),
  10. $swap = $('.theme.menu .item'),
  11. $menu = $('.sidebar.button'),
  12. $sortTable = $('.sortable.table'),
  13. $demo = $('.demo'),
  14. $waypoints = $('h2'),
  15. $example = $('.example'),
  16. $peek = $('.peek'),
  17. $peekMenu = $peek.find('.item'),
  18. $code = $('div.code'),
  19. // alias
  20. handler
  21. ;
  22. // event handlers
  23. handler = {
  24. createIcon: function() {
  25. $example
  26. .each(function(){
  27. $('<i/>')
  28. .addClass('icon code')
  29. .prependTo( $(this) )
  30. ;
  31. })
  32. ;
  33. },
  34. createCode: function() {
  35. var
  36. $example = $(this).closest('.example'),
  37. $shape = $example.find('.shape.module'),
  38. $demo = $example.children().slice(3).not('.annotated, .ignore'),
  39. $annotated = $example.find('.annotated'),
  40. $code = $annotated.find('.code'),
  41. whiteSpace = new RegExp('\\n\\s{4}', 'g'),
  42. code = ''
  43. ;
  44. // if ui has wrapper use that
  45. if($demo.filter('.ui').size() === 0) {
  46. $demo = $example.children().eq(3).children();
  47. }
  48. // add source if doesnt exist and initialize
  49. if($annotated.size() === 0) {
  50. $annotated = $('<div/>')
  51. .addClass('annotated')
  52. .appendTo($example)
  53. ;
  54. }
  55. if( $code.size() === 0) {
  56. $demo
  57. .each(function(){
  58. if($(this).hasClass('ui')) {
  59. code += $(this).get(0).outerHTML + "\n";
  60. }
  61. })
  62. ;
  63. code = $.trim(code.replace(whiteSpace, '\n'));
  64. $code = $('<div/>')
  65. .data('type', 'html')
  66. .addClass('code')
  67. .text(code)
  68. .appendTo($annotated)
  69. ;
  70. $.proxy(handler.initializeCode, $code)();
  71. }
  72. if( $demo.first().is(':visible') ) {
  73. $demo.hide();
  74. $annotated.fadeIn(500);
  75. }
  76. else {
  77. $annotated.hide();
  78. if($demo.size() > 1) {
  79. $demo.show();
  80. }
  81. else {
  82. $demo.fadeIn(500);
  83. }
  84. }
  85. },
  86. initializeCode: function() {
  87. var
  88. $code = $(this),
  89. contentType = $code.data('type') || 'javascript',
  90. editor = ace.edit($code[0]),
  91. editorSession = editor.getSession(),
  92. padding = -1,
  93. codeHeight = editor.getSession().getScreenLength() * (editor.renderer.lineHeight) + editor.renderer.scrollBar.getWidth() + padding
  94. ;
  95. editor.setTheme('ace/theme/github');
  96. editor.setShowPrintMargin(false);
  97. editor.setReadOnly(true);
  98. //editor.renderer.setShowGutter(false);
  99. //editor.setHighlightActiveLine(false);
  100. editorSession.setUseWrapMode(true);
  101. editorSession.setMode('ace/mode/'+ contentType);
  102. editorSession.setTabSize(2);
  103. editorSession.setUseSoftTabs(true);
  104. $(this).height(codeHeight + 'px');
  105. editor.resize();
  106. },
  107. movePeek: function() {
  108. if( $('.stuck .peek').size() > 0 ) {
  109. $('.peek')
  110. .toggleClass('pushed')
  111. ;
  112. }
  113. else {
  114. $('.peek')
  115. .removeClass('pushed')
  116. ;
  117. }
  118. },
  119. peek: function() {
  120. $('html, body')
  121. .stop()
  122. .animate({
  123. scrollTop: $waypoints.eq( $peekMenu.index( $(this) ) ).offset().top - 90
  124. }, 500, function(){
  125. $(this).addClass('active').siblings().removeClass('active');
  126. })
  127. ;
  128. $('html')
  129. .one('scroll', function() {
  130. $('html,body').stop();
  131. })
  132. ;
  133. },
  134. swapStyle: function() {
  135. var
  136. theme = $(this).data('theme')
  137. ;
  138. $(this)
  139. .addClass('active')
  140. .siblings()
  141. .removeClass('active')
  142. ;
  143. $('head link.ui')
  144. .each(function() {
  145. var
  146. href = $(this).attr('href'),
  147. subDirectory = href.split('/')[3],
  148. newLink = href.replace(subDirectory, theme)
  149. ;
  150. console.log(theme, newLink);
  151. $(this)
  152. .attr('href', newLink)
  153. ;
  154. })
  155. ;
  156. }
  157. };
  158. // attach events
  159. $ui
  160. .state()
  161. ;
  162. if($.fn.tablesort !== undefined) {
  163. $sortTable
  164. .tablesort()
  165. ;
  166. }
  167. $waypoints
  168. .waypoint({
  169. continuous : false,
  170. offset : 215,
  171. handler : function(direction) {
  172. var
  173. index = (direction == 'down')
  174. ? $waypoints.index(this)
  175. : ($waypoints.index(this) - 1 >= 0)
  176. ? ($waypoints.index(this) - 1)
  177. : 0
  178. ;
  179. $peekMenu
  180. .removeClass('active')
  181. .eq( index )
  182. .addClass('active')
  183. ;
  184. }
  185. })
  186. ;
  187. if(window.ace !== undefined) {
  188. $code
  189. .each(handler.initializeCode)
  190. ;
  191. }
  192. handler.createIcon();
  193. $example
  194. .find('i.code')
  195. .on('click', handler.createCode)
  196. ;
  197. $swap
  198. .on('click', handler.swapStyle)
  199. ;
  200. $menu
  201. .sidr({
  202. name: 'menu'
  203. })
  204. .on('click', handler.movePeek)
  205. ;
  206. $peek
  207. .waypoint('sticky', {
  208. offset: 80,
  209. stuckClass: 'stuck'
  210. })
  211. ;
  212. $peekMenu
  213. .state()
  214. .on('click', handler.peek)
  215. ;
  216. };
  217. // attach ready event
  218. $(document)
  219. .ready(semantic.ready)
  220. ;