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.

799 lines
20 KiB

  1. // namespace
  2. window.semantic = {
  3. handler: {}
  4. };
  5. // Allow for console.log to not break IE
  6. if (typeof window.console == "undefined" || typeof window.console.log == "undefined") {
  7. window.console = {
  8. log : function() {},
  9. info : function(){},
  10. warn : function(){}
  11. };
  12. }
  13. if(typeof window.console.group == 'undefined' || typeof window.console.groupEnd == 'undefined' || typeof window.console.groupCollapsed == 'undefined') {
  14. window.console.group = function(){};
  15. window.console.groupEnd = function(){};
  16. window.console.groupCollapsed = function(){};
  17. }
  18. if(typeof window.console.markTimeline == 'undefined') {
  19. window.console.markTimeline = function(){};
  20. }
  21. window.console.clear = function(){};
  22. // ready event
  23. semantic.ready = function() {
  24. // selector cache
  25. var
  26. $peek = $('.peek'),
  27. $peekItem = $peek.children('.menu').children('a.item'),
  28. $peekSubItem = $peek.find('.item .menu .item'),
  29. $sortableTables = $('.sortable.table'),
  30. $ui = $('.ui').not('.hover, .down'),
  31. $swap = $('.theme.menu .item'),
  32. $menu = $('#menu'),
  33. $hideMenu = $('#menu .hide.item'),
  34. $sortTable = $('.sortable.table'),
  35. $demo = $('.demo'),
  36. $waypoints = $peek.closest('.tab, .container').find('h2').first().siblings('h2').addBack(),
  37. $menuPopup = $('.ui.main.menu .popup.item'),
  38. $menuDropdown = $('.ui.main.menu .dropdown'),
  39. $pageTabMenu = $('body > .tab.segment .tabular.menu'),
  40. $pageTabs = $('body > .tab.segment .menu .item'),
  41. $downloadDropdown = $('.download.buttons .dropdown'),
  42. $helpPopup = $('.header .help.icon'),
  43. $example = $('.example'),
  44. $shownExample = $example.filter('.shown'),
  45. $developer = $('.developer.item'),
  46. $overview = $('.overview.item, .overview.button'),
  47. $designer = $('.designer.item'),
  48. $sidebarButton = $('.attached.launch.button'),
  49. $increaseFont = $('.font .increase'),
  50. $decreaseFont = $('.font .decrease'),
  51. $code = $('div.code').not('.existing'),
  52. $existingCode = $('.existing.code'),
  53. // alias
  54. handler
  55. ;
  56. // event handlers
  57. handler = {
  58. createIcon: function() {
  59. $example
  60. .each(function(){
  61. $('<i/>')
  62. .addClass('icon code')
  63. .prependTo( $(this) )
  64. ;
  65. })
  66. ;
  67. },
  68. getSpecification: function(callback) {
  69. var
  70. url = $(this).data('url') || false
  71. ;
  72. callback = callback || function(){};
  73. if(url) {
  74. $.ajax({
  75. method: 'get',
  76. url: url,
  77. type: 'json',
  78. complete: callback
  79. });
  80. }
  81. },
  82. create: {
  83. tick: function() {
  84. },
  85. examples: function(json) {
  86. var
  87. types = json['Types'],
  88. text = json['Text'],
  89. states = json['States'],
  90. variations = json['Variations'],
  91. $element,
  92. html
  93. ;
  94. $.each(types, function(name, type){
  95. html += '<h2 class="ui dividing header">' + name + '</h2';
  96. if($.isPlainObject(type)) {
  97. $.each(type, function(name, subType) {
  98. $element = $.zc(subType);
  99. $element = handler.create.text($element, text);
  100. html += '<h3 class="ui header">' + name + '</h3';
  101. html += handler.create.variations($element, variations);
  102. });
  103. }
  104. else {
  105. $element = $.zc(type);
  106. $element = handler.create.text($element);
  107. html += handler.create.variations($element, variations);
  108. }
  109. });
  110. // Each TYPE
  111. // show type name
  112. // html = koan (html)
  113. // each text
  114. // find label
  115. // if(obj)
  116. // replace random text
  117. // else
  118. // replace text
  119. // end
  120. // Each variation
  121. // (if obj)
  122. // each
  123. // add class
  124. // (else)
  125. // add class
  126. // label = property
  127. // class = class
  128. // show html
  129. // end
  130. // end
  131. },
  132. element: function(koan, type, text, variation) {
  133. },
  134. variations: function($element, variations) {
  135. $.each(variations, function(name, variation){
  136. });
  137. },
  138. text: function($element, text) {
  139. $.each(text, function(selector, text) {
  140. $element.find(selector).text(text);
  141. });
  142. return $element;
  143. }
  144. },
  145. font: {
  146. increase: function() {
  147. var
  148. $container = $(this).parent().prev('.ui.segment'),
  149. fontSize = parseInt( $container.css('font-size'), 10)
  150. ;
  151. $container
  152. .css('font-size', fontSize + 1)
  153. ;
  154. },
  155. decrease: function() {
  156. var
  157. $container = $(this).parent().prev('.ui.segment'),
  158. fontSize = parseInt( $container.css('font-size'), 10)
  159. ;
  160. $container
  161. .css('font-size', fontSize - 1)
  162. ;
  163. }
  164. },
  165. overviewMode: function() {
  166. var
  167. $button = $(this),
  168. $body = $('body'),
  169. $example = $('.example')
  170. ;
  171. $body.toggleClass('overview');
  172. $button.toggleClass('active');
  173. if($body.hasClass('overview')) {
  174. $developer.addClass('disabled').popup('destroy');
  175. $designer.addClass('disabled').popup('destroy');
  176. $example.each(function() {
  177. $(this).children().not('.ui.header:eq(0), .example p:eq(0), .annotation').hide();
  178. });
  179. $example.filter('.another').hide();
  180. }
  181. else {
  182. $developer.removeClass('disabled').popup();
  183. $designer.removeClass('disabled').popup();
  184. $example.each(function() {
  185. $(this).children().not('.ui.header:eq(0), .example p:eq(0), .annotation').show();
  186. });
  187. $example.filter('.another').show();
  188. }
  189. },
  190. developerMode: function() {
  191. var
  192. $example = $('.example').not('.no')
  193. ;
  194. $developer.addClass('active');
  195. $designer.removeClass('active');
  196. $example
  197. .each(function() {
  198. $.proxy(handler.createCode, $(this))('developer');
  199. })
  200. ;
  201. },
  202. designerMode: function() {
  203. var
  204. $example = $('.example').not('.no')
  205. ;
  206. $designer.addClass('active');
  207. $developer.removeClass('active');
  208. $example
  209. .each(function() {
  210. $.proxy(handler.createCode, $(this))('designer');
  211. })
  212. ;
  213. },
  214. getIndent: function(text) {
  215. var
  216. lines = text.split("\n"),
  217. firstLine = (lines[0] === '')
  218. ? lines[1]
  219. : lines[0],
  220. spacesPerIndent = 2,
  221. leadingSpaces = firstLine.length - firstLine.replace(/^\s*/g, '').length,
  222. indent
  223. ;
  224. if(leadingSpaces !== 0) {
  225. indent = leadingSpaces;
  226. }
  227. else {
  228. // string has already been trimmed, get first indented line and subtract 2
  229. $.each(lines, function(index, line) {
  230. leadingSpaces = line.length - line.replace(/^\s*/g, '').length;
  231. if(leadingSpaces !== 0) {
  232. indent = leadingSpaces - spacesPerIndent;
  233. return false;
  234. }
  235. });
  236. }
  237. return indent || 4;
  238. },
  239. generateCode: function() {
  240. var
  241. $example = $(this).closest('.example'),
  242. $annotation = $example.find('.annotation'),
  243. $code = $annotation.find('.code'),
  244. $header = $example.not('.another').children('.ui.header:first-of-type').eq(0).add('p:first-of-type'),
  245. $demo = $example.children().not($header).not('i.code:first-child, .code, .instructive, .language.label, .annotation, br, .ignore, .ignored'),
  246. code = ''
  247. ;
  248. if( $code.size() === 0) {
  249. $demo
  250. .each(function(){
  251. var $this = $(this).clone(false);
  252. if($this.not('br')) {
  253. code += $this.removeAttr('style').get(0).outerHTML + "\n";
  254. }
  255. })
  256. ;
  257. }
  258. $example.data('code', code);
  259. return code;
  260. },
  261. createCode: function(type) {
  262. var
  263. $example = $(this).closest('.example'),
  264. $header = $example.children('.ui.header:first-of-type').eq(0).add('p:first-of-type'),
  265. $annotation = $example.find('.annotation'),
  266. $code = $annotation.find('.code'),
  267. $demo = $example.children().not($header).not('i.code:first-child, .code, .instructive, .language.label, .annotation, br, .ignore, .ignored'),
  268. code = $example.data('code') || $.proxy(handler.generateCode, this)()
  269. ;
  270. if( $code.hasClass('existing') ) {
  271. $annotation.show();
  272. $code.removeClass('existing');
  273. $.proxy(handler.initializeCode, $code)();
  274. }
  275. if($annotation.size() === 0) {
  276. $annotation = $('<div/>')
  277. .addClass('annotation')
  278. .appendTo($example)
  279. ;
  280. }
  281. if( $example.find('.ace_editor').size() === 0) {
  282. $code = $('<div/>')
  283. .data('type', 'html')
  284. .addClass('code')
  285. .html(code)
  286. .hide()
  287. .appendTo($annotation)
  288. ;
  289. $.proxy(handler.initializeCode, $code)();
  290. }
  291. if( ($demo.first().is(':visible') || type == 'developer') && type != 'designer' ) {
  292. $demo.hide();
  293. $header.show();
  294. $annotation.fadeIn(500);
  295. }
  296. else {
  297. $annotation.hide();
  298. if($demo.size() > 1) {
  299. $demo.show();
  300. }
  301. else {
  302. $demo.fadeIn(500);
  303. }
  304. }
  305. },
  306. createAnnotation: function() {
  307. if(!$(this).data('type')) {
  308. $(this).data('type', 'html');
  309. }
  310. $(this)
  311. .wrap('<div class="annotation">')
  312. .parent()
  313. .hide()
  314. ;
  315. },
  316. resizeCode: function() {
  317. $('.ace_editor')
  318. .each(function() {
  319. var
  320. $code = $(this),
  321. padding = 20,
  322. editor,
  323. editorSession,
  324. codeHeight
  325. ;
  326. $code.css('height', 'auto');
  327. editor = ace.edit($code[0]);
  328. editorSession = editor.getSession();
  329. codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
  330. $code.css('height', codeHeight);
  331. editor.resize();
  332. })
  333. ;
  334. },
  335. makeCode: function() {
  336. if(window.ace !== undefined) {
  337. $code
  338. .filter(':visible')
  339. .each(handler.initializeCode)
  340. ;
  341. $existingCode
  342. .each(handler.createAnnotation)
  343. ;
  344. }
  345. },
  346. makeStickyColumns: function() {
  347. var
  348. $visibleStuck = $(this).find('.fixed.column .image, .fixed.column .content'),
  349. isInitialized = ($visibleStuck.parent('.sticky-wrapper').size() !== 0)
  350. ;
  351. if(!isInitialized) {
  352. $visibleStuck
  353. .waypoint('sticky', {
  354. offset : 65,
  355. stuckClass : 'fixed'
  356. })
  357. ;
  358. }
  359. // apparently this doesnt refresh on first hit
  360. $.waypoints('refresh');
  361. $.waypoints('refresh');
  362. },
  363. initializeCode: function() {
  364. var
  365. $code = $(this).show(),
  366. code = $code.html(),
  367. existingCode = $code.hasClass('existing'),
  368. contentType = $code.data('type') || 'javascript',
  369. title = $code.data('title') || false,
  370. demo = $code.data('demo') || false,
  371. preview = $code.data('preview') || false,
  372. label = $code.data('label') || false,
  373. displayType = {
  374. html : 'HTML',
  375. javascript : 'Javascript',
  376. css : 'CSS',
  377. text : 'Command Line',
  378. sh : 'Command Line'
  379. },
  380. indent = handler.getIndent(code) || 4,
  381. padding = 20,
  382. whiteSpace,
  383. $label,
  384. editor,
  385. editorSession,
  386. codeHeight
  387. ;
  388. // trim whitespace
  389. whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g');
  390. code = $.trim(code).replace(whiteSpace, '\n');
  391. if(contentType == 'html') {
  392. $code.text(code);
  393. }
  394. else {
  395. $code.html(code);
  396. }
  397. // evaluate if specified
  398. if($code.hasClass('evaluated')) {
  399. eval(code);
  400. }
  401. // initialize
  402. editor = ace.edit($code[0]);
  403. editorSession = editor.getSession();
  404. editor.setTheme('ace/theme/github');
  405. editor.setShowPrintMargin(false);
  406. editor.setReadOnly(true);
  407. editor.renderer.setShowGutter(false);
  408. editor.setHighlightActiveLine(false);
  409. editorSession.setMode('ace/mode/'+ contentType);
  410. editorSession.setUseWrapMode(true);
  411. editorSession.setTabSize(2);
  412. editorSession.setUseSoftTabs(true);
  413. codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
  414. $(this)
  415. .height(codeHeight + 'px')
  416. .wrap('<div class="ui instructive segment">')
  417. ;
  418. // add label
  419. if(title) {
  420. $('<div>')
  421. .addClass('ui attached top label')
  422. .html('<span class="title">' + title + '</span>' + '<em>' + (displayType[contentType] || contentType) + '</em>')
  423. .prependTo( $(this).parent() )
  424. ;
  425. }
  426. if(label) {
  427. $('<div>')
  428. .addClass('ui pointing below label')
  429. .html(displayType[contentType] || contentType)
  430. .insertBefore ( $(this).parent() )
  431. ;
  432. }
  433. // add run code button
  434. if(demo) {
  435. $('<a>')
  436. .addClass('ui pointing below black label')
  437. .html('Run Code')
  438. .on('click', function() {
  439. eval(code);
  440. })
  441. .insertBefore ( $(this).parent() )
  442. ;
  443. }
  444. // add preview if specified
  445. if(preview) {
  446. $(code)
  447. .insertAfter( $(this).parent() )
  448. ;
  449. }
  450. editor.resize();
  451. },
  452. movePeek: function() {
  453. if( $('.stuck .peek').size() > 0 ) {
  454. $('.peek')
  455. .toggleClass('pushed')
  456. ;
  457. }
  458. else {
  459. $('.peek')
  460. .removeClass('pushed')
  461. ;
  462. }
  463. },
  464. menu: {
  465. mouseenter: function() {
  466. $(this)
  467. .stop()
  468. .animate({
  469. width: '155px'
  470. }, 300, function() {
  471. $(this).find('.text').show();
  472. })
  473. ;
  474. },
  475. mouseleave: function(event) {
  476. $(this).find('.text').hide();
  477. $(this)
  478. .stop()
  479. .animate({
  480. width: '70px'
  481. }, 300)
  482. ;
  483. }
  484. },
  485. peek: function() {
  486. var
  487. $body = $('html, body'),
  488. $header = $(this),
  489. $menu = $header.parent(),
  490. $group = $menu.children(),
  491. $headers = $group.add( $group.find('.menu .item') ),
  492. $waypoint = $waypoints.eq( $group.index( $header ) ),
  493. offset
  494. ;
  495. offset = $waypoint.offset().top - 70;
  496. if(!$header.hasClass('active') ) {
  497. $menu
  498. .addClass('animating')
  499. ;
  500. $headers
  501. .removeClass('active')
  502. ;
  503. $body
  504. .stop()
  505. .one('scroll', function() {
  506. $body.stop();
  507. })
  508. .animate({
  509. scrollTop: offset
  510. }, 500)
  511. .promise()
  512. .done(function() {
  513. $menu
  514. .removeClass('animating')
  515. ;
  516. $headers
  517. .removeClass('active')
  518. ;
  519. $header
  520. .addClass('active')
  521. ;
  522. $waypoint
  523. .css('color', $header.css('border-right-color'))
  524. ;
  525. $waypoints
  526. .removeAttr('style')
  527. ;
  528. })
  529. ;
  530. }
  531. },
  532. peekSub: function() {
  533. var
  534. $body = $('html, body'),
  535. $subHeader = $(this),
  536. $header = $subHeader.parents('.item'),
  537. $menu = $header.parent(),
  538. $subHeaderGroup = $header.find('.item'),
  539. $headerGroup = $menu.children(),
  540. $waypoint = $('h2').eq( $headerGroup.index( $header ) ),
  541. $subWaypoint = $waypoint.nextAll('h3').eq( $subHeaderGroup.index($subHeader) ),
  542. offset = $subWaypoint.offset().top - 80
  543. ;
  544. $menu
  545. .addClass('animating')
  546. ;
  547. $headerGroup
  548. .removeClass('active')
  549. ;
  550. $subHeaderGroup
  551. .removeClass('active')
  552. ;
  553. $body
  554. .stop()
  555. .animate({
  556. scrollTop: offset
  557. }, 500, function() {
  558. $menu
  559. .removeClass('animating')
  560. ;
  561. $subHeader
  562. .addClass('active')
  563. ;
  564. })
  565. .one('scroll', function() {
  566. $body.stop();
  567. })
  568. ;
  569. },
  570. swapStyle: function() {
  571. var
  572. theme = $(this).data('theme')
  573. ;
  574. $(this)
  575. .addClass('active')
  576. .siblings()
  577. .removeClass('active')
  578. ;
  579. $('head link.ui')
  580. .each(function() {
  581. var
  582. href = $(this).attr('href'),
  583. subDirectory = href.split('/')[3],
  584. newLink = href.replace(subDirectory, theme)
  585. ;
  586. $(this)
  587. .attr('href', newLink)
  588. ;
  589. })
  590. ;
  591. }
  592. };
  593. $(window)
  594. .on('resize', function() {
  595. clearTimeout(handler.timer);
  596. handler.timer = setTimeout(handler.resizeCode, 100);
  597. })
  598. ;
  599. $downloadDropdown
  600. .dropdown({
  601. on : 'click',
  602. transition : 'scale'
  603. })
  604. ;
  605. // attach events
  606. if($.fn.tablesort !== undefined) {
  607. $sortTable
  608. .tablesort()
  609. ;
  610. }
  611. if( $pageTabs.size() > 0 ) {
  612. $pageTabs
  613. .tab({
  614. onTabInit : handler.makeCode,
  615. onTabLoad : function() {
  616. $.proxy(handler.makeStickyColumns, this)();
  617. $peekItem.removeClass('active').first().addClass('active');
  618. }
  619. })
  620. ;
  621. }
  622. else {
  623. handler.makeCode();
  624. }
  625. handler.createIcon();
  626. $example
  627. .one('mousemove', handler.generateCode)
  628. .find('i.code')
  629. .on('click', handler.createCode)
  630. ;
  631. $shownExample
  632. .each(handler.createCode)
  633. ;
  634. $helpPopup
  635. .popup()
  636. ;
  637. $swap
  638. .on('click', handler.swapStyle)
  639. ;
  640. $increaseFont
  641. .on('click', handler.font.increase)
  642. ;
  643. $decreaseFont
  644. .on('click', handler.font.decrease)
  645. ;
  646. $developer
  647. .on('click', handler.developerMode)
  648. ;
  649. $designer
  650. .on('click', handler.designerMode)
  651. ;
  652. $overview
  653. .on('click', handler.overviewMode)
  654. ;
  655. $menuPopup
  656. .popup({
  657. position : 'bottom center',
  658. className: {
  659. popup: 'ui popup'
  660. }
  661. })
  662. ;
  663. $sortableTables
  664. .tablesort()
  665. ;
  666. $menuDropdown
  667. .dropdown({
  668. on : 'hover',
  669. action : 'nothing'
  670. })
  671. ;
  672. $sidebarButton
  673. .on('mouseenter', handler.menu.mouseenter)
  674. .on('mouseleave', handler.menu.mouseleave)
  675. ;
  676. $menu
  677. .sidebar('attach events', '.launch.button, .launch.item')
  678. .sidebar('attach events', $hideMenu, 'hide')
  679. ;
  680. $waypoints
  681. .waypoint({
  682. continuous : false,
  683. offset : 100,
  684. handler : function(direction) {
  685. var
  686. index = (direction == 'down')
  687. ? $waypoints.index(this)
  688. : ($waypoints.index(this) - 1 >= 0)
  689. ? ($waypoints.index(this) - 1)
  690. : 0
  691. ;
  692. $peekItem
  693. .removeClass('active')
  694. .eq( index )
  695. .addClass('active')
  696. ;
  697. }
  698. })
  699. ;
  700. $('body')
  701. .waypoint({
  702. handler: function(direction) {
  703. if(direction == 'down') {
  704. if( !$('body').is(':animated') ) {
  705. $peekItem
  706. .removeClass('active')
  707. .eq( $peekItem.size() - 1 )
  708. .addClass('active')
  709. ;
  710. }
  711. }
  712. },
  713. offset: 'bottom-in-view'
  714. })
  715. ;
  716. $peek
  717. .waypoint('sticky', {
  718. offset : 85,
  719. stuckClass : 'stuck'
  720. })
  721. ;
  722. $peekItem
  723. .on('click', handler.peek)
  724. ;
  725. $peekSubItem
  726. .on('click', handler.peekSub)
  727. ;
  728. };
  729. // attach ready event
  730. $(document)
  731. .ready(semantic.ready)
  732. ;