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.

219 lines
4.7 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  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 = $('.swap'),
  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. removeIndents: function(code) {
  86. },
  87. initializeCode: function() {
  88. var
  89. $code = $(this),
  90. contentType = $code.data('type') || 'javascript',
  91. editor = ace.edit($code[0]),
  92. editorSession = editor.getSession(),
  93. padding = 4,
  94. codeHeight = editor.getSession().getScreenLength() * (editor.renderer.lineHeight) + editor.renderer.scrollBar.getWidth() + padding
  95. ;
  96. editor.setTheme('ace/theme/github');
  97. editor.setShowPrintMargin(false);
  98. editor.setReadOnly(true);
  99. editor.renderer.setShowGutter(false);
  100. editor.setHighlightActiveLine(false);
  101. editorSession.setMode('ace/mode/'+ contentType);
  102. editorSession.setTabSize(2);
  103. editorSession.setUseSoftTabs(true);
  104. $(this).height(codeHeight + 'px');
  105. editor.resize();
  106. },
  107. peek: function() {
  108. $('html, body')
  109. .stop()
  110. .animate({
  111. scrollTop: $waypoints.eq( $peekMenu.index( $(this) ) ).offset().top - 90
  112. }, 500, function(){
  113. $(this).addClass('active').siblings().removeClass('active');
  114. })
  115. ;
  116. $('html')
  117. .one('scroll', function() {
  118. $('html,body').stop();
  119. })
  120. ;
  121. },
  122. swapStyle: function() {
  123. $('head link.ui')
  124. .each(function() {
  125. var
  126. href = $(this).attr('href')
  127. ;
  128. if( href.search('flat') > -1 ) {
  129. $(this).attr('href', href.replace('flat', 'shaded'));
  130. }
  131. else {
  132. $(this).attr('href', href.replace('shaded', 'flat'));
  133. }
  134. })
  135. ;
  136. }
  137. };
  138. // attach events
  139. $ui
  140. .state()
  141. ;
  142. $waypoints
  143. .waypoint({
  144. continuous : false,
  145. offset : 215,
  146. handler : function(direction) {
  147. var
  148. index = (direction == 'down')
  149. ? $waypoints.index(this)
  150. : ($waypoints.index(this) - 1 >= 0)
  151. ? ($waypoints.index(this) - 1)
  152. : 0
  153. ;
  154. $peekMenu
  155. .removeClass('active')
  156. .eq( index )
  157. .addClass('active')
  158. ;
  159. }
  160. })
  161. ;
  162. if(window.ace !== undefined) {
  163. $code
  164. .each(handler.initializeCode)
  165. ;
  166. }
  167. handler.createIcon();
  168. $example
  169. .find('i.code')
  170. .on('click', handler.createCode)
  171. ;
  172. $swap
  173. .on('click', handler.swapStyle)
  174. ;
  175. $menu
  176. .sidr({
  177. name: 'menu'
  178. })
  179. ;
  180. $peek
  181. .waypoint('sticky', {
  182. offset: 80,
  183. stuckClass: 'stuck'
  184. })
  185. ;
  186. $peekMenu
  187. .state()
  188. .on('click', handler.peek)
  189. ;
  190. };
  191. // attach ready event
  192. $(document)
  193. .ready(semantic.ready)
  194. ;