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.

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