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.

783 lines
19 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. console.log('returning' + indent);
  234. return false;
  235. }
  236. });
  237. }
  238. return indent || 4;
  239. },
  240. generateCode: function() {
  241. var
  242. $example = $(this).closest('.example'),
  243. $annotation = $example.find('.annotation'),
  244. $code = $annotation.find('.code'),
  245. $header = $example.children('.ui.header:first-of-type').eq(0).add('p:first-of-type'),
  246. $demo = $example.children().not($header).not('i.code:first-child, .code, .instructive, .language.label, .annotation, br, .ignore, .ignored'),
  247. code = ''
  248. ;
  249. if( $code.size() === 0) {
  250. $demo
  251. .each(function(){
  252. var $this = $(this).clone(false);
  253. if($this.not('br')) {
  254. code += $this.removeAttr('style').get(0).outerHTML + "\n";
  255. }
  256. })
  257. ;
  258. }
  259. $example.data('code', code);
  260. return code;
  261. },
  262. createCode: function(type) {
  263. var
  264. $example = $(this).closest('.example'),
  265. $header = $example.children('.ui.header:first-of-type').eq(0).add('p:first-of-type'),
  266. $annotation = $example.find('.annotation'),
  267. $code = $annotation.find('.code'),
  268. $demo = $example.children().not($header).not('i.code:first-child, .code, .instructive, .language.label, .annotation, br, .ignore, .ignored'),
  269. code = $example.data('code') || $.proxy(handler.generateCode, this)()
  270. ;
  271. if( $code.hasClass('existing') ) {
  272. $annotation.show();
  273. $code.removeClass('existing');
  274. $.proxy(handler.initializeCode, $code)();
  275. }
  276. if($annotation.size() === 0) {
  277. $annotation = $('<div/>')
  278. .addClass('annotation')
  279. .appendTo($example)
  280. ;
  281. }
  282. if( $example.find('.ace_editor').size() === 0) {
  283. $code = $('<div/>')
  284. .data('type', 'html')
  285. .addClass('code')
  286. .html(code)
  287. .hide()
  288. .appendTo($annotation)
  289. ;
  290. $.proxy(handler.initializeCode, $code)();
  291. }
  292. if( ($demo.first().is(':visible') || type == 'developer') && type != 'designer' ) {
  293. $demo.hide();
  294. $header.show();
  295. $annotation.fadeIn(500);
  296. }
  297. else {
  298. $annotation.hide();
  299. if($demo.size() > 1) {
  300. $demo.show();
  301. }
  302. else {
  303. $demo.fadeIn(500);
  304. }
  305. }
  306. },
  307. createAnnotation: function() {
  308. if(!$(this).data('type')) {
  309. $(this).data('type', 'html');
  310. }
  311. $(this)
  312. .wrap('<div class="annotation">')
  313. .parent()
  314. .hide()
  315. ;
  316. },
  317. resizeCode: function() {
  318. $('.ace_editor')
  319. .each(function() {
  320. var
  321. $code = $(this),
  322. padding = 20,
  323. editor,
  324. editorSession,
  325. codeHeight
  326. ;
  327. $code.css('height', 'auto');
  328. editor = ace.edit($code[0]);
  329. editorSession = editor.getSession();
  330. codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
  331. $code.css('height', codeHeight);
  332. editor.resize();
  333. })
  334. ;
  335. },
  336. makeCode: function() {
  337. if(window.ace !== undefined) {
  338. $code
  339. .filter(':visible')
  340. .each(handler.initializeCode)
  341. ;
  342. $existingCode
  343. .each(handler.createAnnotation)
  344. ;
  345. }
  346. },
  347. initializeCode: function() {
  348. var
  349. $code = $(this).show(),
  350. code = $code.html(),
  351. existingCode = $code.hasClass('existing'),
  352. contentType = $code.data('type') || 'javascript',
  353. title = $code.data('title') || false,
  354. demo = $code.data('demo') || false,
  355. preview = $code.data('preview') || false,
  356. label = $code.data('label') || false,
  357. displayType = {
  358. html : 'HTML',
  359. javascript : 'Javascript',
  360. css : 'CSS',
  361. text : 'Command Line',
  362. sh : 'Command Line'
  363. },
  364. indent = handler.getIndent(code) || 4,
  365. padding = 20,
  366. whiteSpace,
  367. $label,
  368. editor,
  369. editorSession,
  370. codeHeight
  371. ;
  372. // trim whitespace
  373. whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g');
  374. code = $.trim(code).replace(whiteSpace, '\n');
  375. if(contentType == 'html') {
  376. $code.text(code);
  377. }
  378. else {
  379. $code.html(code);
  380. }
  381. // evaluate if specified
  382. if($code.hasClass('evaluated')) {
  383. eval(code);
  384. }
  385. // initialize
  386. editor = ace.edit($code[0]);
  387. editorSession = editor.getSession();
  388. editor.setTheme('ace/theme/github');
  389. editor.setShowPrintMargin(false);
  390. editor.setReadOnly(true);
  391. editor.renderer.setShowGutter(false);
  392. editor.setHighlightActiveLine(false);
  393. editorSession.setMode('ace/mode/'+ contentType);
  394. editorSession.setUseWrapMode(true);
  395. editorSession.setTabSize(2);
  396. editorSession.setUseSoftTabs(true);
  397. codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
  398. $(this)
  399. .height(codeHeight + 'px')
  400. .wrap('<div class="ui instructive segment">')
  401. ;
  402. // add label
  403. if(title) {
  404. $('<div>')
  405. .addClass('ui attached top label')
  406. .html('<span class="title">' + title + '</span>' + '<em>' + (displayType[contentType] || contentType) + '</em>')
  407. .prependTo( $(this).parent() )
  408. ;
  409. }
  410. if(label) {
  411. $('<div>')
  412. .addClass('ui pointing below label')
  413. .html(displayType[contentType] || contentType)
  414. .insertBefore ( $(this).parent() )
  415. ;
  416. }
  417. // add run code button
  418. if(demo) {
  419. $('<a>')
  420. .addClass('ui pointing below black label')
  421. .html('Run Code')
  422. .on('click', function() {
  423. eval(code);
  424. })
  425. .insertBefore ( $(this).parent() )
  426. ;
  427. }
  428. // add preview if specified
  429. if(preview) {
  430. $(code)
  431. .insertAfter( $(this).parent() )
  432. ;
  433. }
  434. editor.resize();
  435. },
  436. movePeek: function() {
  437. if( $('.stuck .peek').size() > 0 ) {
  438. $('.peek')
  439. .toggleClass('pushed')
  440. ;
  441. }
  442. else {
  443. $('.peek')
  444. .removeClass('pushed')
  445. ;
  446. }
  447. },
  448. menu: {
  449. mouseenter: function() {
  450. $(this)
  451. .stop()
  452. .animate({
  453. width: '155px'
  454. }, 300, function() {
  455. $(this).find('.text').show();
  456. })
  457. ;
  458. },
  459. mouseleave: function(event) {
  460. $(this).find('.text').hide();
  461. $(this)
  462. .stop()
  463. .animate({
  464. width: '70px'
  465. }, 300)
  466. ;
  467. }
  468. },
  469. peek: function() {
  470. var
  471. $body = $('html, body'),
  472. $header = $(this),
  473. $menu = $header.parent(),
  474. $group = $menu.children(),
  475. $headers = $group.add( $group.find('.menu .item') ),
  476. $waypoint = $waypoints.eq( $group.index( $header ) ),
  477. offset
  478. ;
  479. offset = $waypoint.offset().top - 70;
  480. if(!$header.hasClass('active') ) {
  481. $menu
  482. .addClass('animating')
  483. ;
  484. $headers
  485. .removeClass('active')
  486. ;
  487. $body
  488. .stop()
  489. .one('scroll', function() {
  490. $body.stop();
  491. })
  492. .animate({
  493. scrollTop: offset
  494. }, 500)
  495. .promise()
  496. .done(function() {
  497. $menu
  498. .removeClass('animating')
  499. ;
  500. $headers
  501. .removeClass('active')
  502. ;
  503. $header
  504. .addClass('active')
  505. ;
  506. $waypoint
  507. .css('color', $header.css('border-right-color'))
  508. ;
  509. $waypoints
  510. .removeAttr('style')
  511. ;
  512. })
  513. ;
  514. }
  515. },
  516. peekSub: function() {
  517. var
  518. $body = $('html, body'),
  519. $subHeader = $(this),
  520. $header = $subHeader.parents('.item'),
  521. $menu = $header.parent(),
  522. $subHeaderGroup = $header.find('.item'),
  523. $headerGroup = $menu.children(),
  524. $waypoint = $('h2').eq( $headerGroup.index( $header ) ),
  525. $subWaypoint = $waypoint.nextAll('h3').eq( $subHeaderGroup.index($subHeader) ),
  526. offset = $subWaypoint.offset().top - 80
  527. ;
  528. $menu
  529. .addClass('animating')
  530. ;
  531. $headerGroup
  532. .removeClass('active')
  533. ;
  534. $subHeaderGroup
  535. .removeClass('active')
  536. ;
  537. $body
  538. .stop()
  539. .animate({
  540. scrollTop: offset
  541. }, 500, function() {
  542. $menu
  543. .removeClass('animating')
  544. ;
  545. $subHeader
  546. .addClass('active')
  547. ;
  548. })
  549. .one('scroll', function() {
  550. $body.stop();
  551. })
  552. ;
  553. },
  554. swapStyle: function() {
  555. var
  556. theme = $(this).data('theme')
  557. ;
  558. $(this)
  559. .addClass('active')
  560. .siblings()
  561. .removeClass('active')
  562. ;
  563. $('head link.ui')
  564. .each(function() {
  565. var
  566. href = $(this).attr('href'),
  567. subDirectory = href.split('/')[3],
  568. newLink = href.replace(subDirectory, theme)
  569. ;
  570. $(this)
  571. .attr('href', newLink)
  572. ;
  573. })
  574. ;
  575. }
  576. };
  577. $(window)
  578. .on('resize', function() {
  579. clearTimeout(handler.timer);
  580. handler.timer = setTimeout(handler.resizeCode, 100);
  581. })
  582. ;
  583. $downloadDropdown
  584. .dropdown({
  585. on : 'click',
  586. transition : 'scale'
  587. })
  588. ;
  589. // attach events
  590. if($.fn.tablesort !== undefined) {
  591. $sortTable
  592. .tablesort()
  593. ;
  594. }
  595. if( $pageTabs.size() > 0 ) {
  596. $pageTabs
  597. .tab({
  598. onTabInit : handler.makeCode,
  599. onTabLoad : function() {
  600. $.waypoints('refresh');
  601. $peekItem.removeClass('active').first().addClass('active');
  602. }
  603. })
  604. ;
  605. }
  606. else {
  607. handler.makeCode();
  608. }
  609. handler.createIcon();
  610. $example
  611. .one('mousemove', handler.generateCode)
  612. .find('i.code')
  613. .on('click', handler.createCode)
  614. ;
  615. $shownExample
  616. .each(handler.createCode)
  617. ;
  618. $helpPopup
  619. .popup()
  620. ;
  621. $swap
  622. .on('click', handler.swapStyle)
  623. ;
  624. $increaseFont
  625. .on('click', handler.font.increase)
  626. ;
  627. $decreaseFont
  628. .on('click', handler.font.decrease)
  629. ;
  630. $developer
  631. .on('click', handler.developerMode)
  632. ;
  633. $designer
  634. .on('click', handler.designerMode)
  635. ;
  636. $overview
  637. .on('click', handler.overviewMode)
  638. ;
  639. $menuPopup
  640. .popup({
  641. position : 'bottom center',
  642. className: {
  643. popup: 'ui popup'
  644. }
  645. })
  646. ;
  647. $sortableTables
  648. .tablesort()
  649. ;
  650. $menuDropdown
  651. .dropdown({
  652. on : 'hover',
  653. action : 'nothing'
  654. })
  655. ;
  656. $sidebarButton
  657. .on('mouseenter', handler.menu.mouseenter)
  658. .on('mouseleave', handler.menu.mouseleave)
  659. ;
  660. $menu
  661. .sidebar('attach events', '.launch.button, .launch.item')
  662. .sidebar('attach events', $hideMenu, 'hide')
  663. ;
  664. $waypoints
  665. .waypoint({
  666. continuous : false,
  667. offset : 100,
  668. handler : function(direction) {
  669. var
  670. index = (direction == 'down')
  671. ? $waypoints.index(this)
  672. : ($waypoints.index(this) - 1 >= 0)
  673. ? ($waypoints.index(this) - 1)
  674. : 0
  675. ;
  676. $peekItem
  677. .removeClass('active')
  678. .eq( index )
  679. .addClass('active')
  680. ;
  681. }
  682. })
  683. ;
  684. $('body')
  685. .waypoint({
  686. handler: function(direction) {
  687. if(direction == 'down') {
  688. if( !$('body').is(':animated') ) {
  689. $peekItem
  690. .removeClass('active')
  691. .eq( $peekItem.size() - 1 )
  692. .addClass('active')
  693. ;
  694. }
  695. }
  696. },
  697. offset: 'bottom-in-view'
  698. })
  699. ;
  700. $peek
  701. .waypoint('sticky', {
  702. offset : 85,
  703. stuckClass : 'stuck'
  704. })
  705. ;
  706. $peekItem
  707. .on('click', handler.peek)
  708. ;
  709. $peekSubItem
  710. .on('click', handler.peekSub)
  711. ;
  712. };
  713. // attach ready event
  714. $(document)
  715. .ready(semantic.ready)
  716. ;