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.

493 lines
13 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Install some useful jQuery extensions that we use a lot
  2. $.support.touch = 'ontouchstart' in window;
  3. $.extend($.fn, {
  4. orNull: function() {
  5. return this.length > 0 ? this : null;
  6. },
  7. findAndSelf: function(selector) {
  8. return this.find(selector).add(this.filter(selector));
  9. }
  10. });
  11. // Little Helpers
  12. function hyphenate(str) {
  13. return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  14. }
  15. function isVisible(el) {
  16. if (el.is(':hidden'))
  17. return false;
  18. var viewTop = $(window).scrollTop();
  19. var viewBottom = viewTop + $(window).height();
  20. var top = el.offset().top;
  21. var bottom = top + el.height();
  22. return top >= viewTop && bottom <= viewBottom
  23. || top <= viewTop && bottom >= viewTop
  24. || top <= viewBottom && bottom >= viewBottom;
  25. }
  26. function smoothScrollTo(el, callback) {
  27. $('html, body').animate({
  28. scrollTop: el.offset().top
  29. }, 250, callback);
  30. }
  31. var behaviors = {};
  32. behaviors.hiDPI = function() {
  33. // Turn off hiDPI for all touch devices for now, until the site is built
  34. // true to scale.
  35. if ($.support.touch)
  36. $('canvas').attr('hidpi', 'off');
  37. };
  38. behaviors.sections = function() {
  39. var toc = $('.toc');
  40. var checks = [];
  41. var active;
  42. function update() {
  43. $.each(checks, function() {
  44. if (this())
  45. return false;
  46. });
  47. }
  48. $(document).scroll(update);
  49. $(window).resize(update);
  50. setTimeout(update, 0);
  51. $('article section').each(function() {
  52. var section = $(this);
  53. var anchor = $('a', section);
  54. // Move content until next section inside section
  55. section.append(section.nextUntil('section'));
  56. var title = anchor.attr('title') || $('h1,h2', section).first().text();
  57. var id = section.attr('id');
  58. if (!id) {
  59. id = hyphenate(title)
  60. .replace(/\s+/g, '-')
  61. .replace(/^#/, '')
  62. .replace(/[!"#$%&'\()*+,.\/:;<=>?@\[\\\]\^_`{|}~]+/g, '-')
  63. .replace(/-+/g, '-');
  64. section.attr('id', id);
  65. anchor.attr('name', id);
  66. }
  67. function activate() {
  68. if (active)
  69. active.removeClass('active');
  70. selector.addClass('active');
  71. active = selector;
  72. }
  73. // Create table of contents on the fly
  74. if (toc) {
  75. var selector = $('<li class="entry selector"><a href="#' + id + '">'
  76. + title + '</a></li>').appendTo(toc);
  77. if (section.is('.spacer'))
  78. selector.addClass('spacer');
  79. $('a', selector).click(function() {
  80. smoothScrollTo(section, function() {
  81. window.location.hash = id;
  82. });
  83. return false;
  84. });
  85. checks.push(function() {
  86. var visible = isVisible(section);
  87. if (visible)
  88. activate();
  89. return visible;
  90. });
  91. }
  92. });
  93. // Adjust height of last section so that the last anchor aligns perfectly
  94. // with the top of the browser window.
  95. var lastSection = $('article section:last');
  96. var lastAnchor = $('a[name]', lastSection);
  97. function resize() {
  98. lastSection.height('auto');
  99. var bottom = $(document).height() - lastAnchor.offset().top - $(window).height();
  100. if (bottom < 0)
  101. lastSection.height(lastSection.height() - bottom);
  102. }
  103. if (lastSection.length && lastAnchor.length) {
  104. $(window).on({
  105. load: resize,
  106. resize: resize
  107. });
  108. resize();
  109. }
  110. };
  111. behaviors.sticky = function() {
  112. $('.sticky').each(function() {
  113. me = $(this);
  114. container = $('<div/>').append(me.contents()).appendTo(me);
  115. // Insert a div wrapper of which the fixed class is modified depending on position
  116. $(window).scroll(function() {
  117. if (container.is(':visible'))
  118. container.toggleClass('fixed', me.offset().top - $(this).scrollTop() <= 0);
  119. });
  120. });
  121. };
  122. behaviors.hash = function() {
  123. var hash = unescape(window.location.hash);
  124. if (hash) {
  125. // First see if there's a class member to open
  126. var target = $(hash);
  127. if (target.length) {
  128. if (target.hasClass('member'))
  129. toggleMember(target);
  130. smoothScrollTo(target);
  131. }
  132. }
  133. };
  134. behaviors.thumbnails = function() {
  135. var thumbnails = $('.thumbnail');
  136. var height = 0;
  137. thumbnails.each(function() {
  138. height = Math.max(height, $(this).height());
  139. });
  140. $('.thumbnail').height(height);
  141. };
  142. behaviors.expandableLists = function() {
  143. $('.expandable-list').each(function() {
  144. var list = $(this);
  145. $('<a href="#" class="arrow"/>')
  146. .prependTo(list)
  147. .click(function() {
  148. list.toggleClass('expanded');
  149. });
  150. });
  151. };
  152. behaviors.referenceClass = function() {
  153. var classes = $('.reference-classes');
  154. if (classes.length) {
  155. // Mark currently selected class as active. Do it client-sided
  156. // since the menu is generated by jsdocs.
  157. var path = window.location.pathname.toLowerCase();
  158. $('a[href="' + path + '"]', classes).addClass('active');
  159. }
  160. };
  161. behaviors.hover = function() {
  162. $('.hover').hover(function() {
  163. $('.normal', this).toggleClass('hidden');
  164. $('.over', this).toggleClass('hidden');
  165. });
  166. };
  167. behaviors.code = function() {
  168. $('.code:visible, pre:visible code').each(function() {
  169. createCode($(this));
  170. });
  171. };
  172. behaviors.paperscript = function() {
  173. // Ignore all paperscripts in the automatic load event, and load them
  174. // separately in createPaperScript() when needed.
  175. $('script[type="text/paperscript"]').attr('ignore', 'true');
  176. $('.paperscript:visible').each(function() {
  177. createPaperScript($(this));
  178. });
  179. };
  180. function createCodeMirror(place, options, source) {
  181. return new CodeMirror(place, $.extend({}, {
  182. mode: 'javascript',
  183. lineNumbers: true,
  184. matchBrackets: true,
  185. tabSize: 4,
  186. indentUnit: 4,
  187. indentWithTabs: true,
  188. tabMode: 'shift',
  189. value: source.text().match(
  190. // Remove first & last empty line
  191. /^\s*?[\n\r]?([\u0000-\uffff]*?)[\n\r]?\s*?$/)[1]
  192. }, options));
  193. }
  194. function createCode(element) {
  195. if (element.data('initialized'))
  196. return;
  197. var start = element.attr('start');
  198. var highlight = element.attr('highlight');
  199. var editor = createCodeMirror(function(el) {
  200. element.replaceWith(el);
  201. }, {
  202. lineNumbers: !element.parent('.resource-text').length,
  203. firstLineNumber: parseInt(start || 1, 10),
  204. mode: element.attr('mode') || 'javascript',
  205. readOnly: true
  206. }, element);
  207. if (highlight) {
  208. var highlights = highlight.split(',');
  209. for (var i = 0, l = highlights.length; i < l; i++) {
  210. var highlight = highlights[i].split('-');
  211. var hlStart = parseInt(highlight[0], 10) - 1;
  212. var hlEnd = highlight.length == 2
  213. ? parseInt(highlight[1], 10) - 1 : hlStart;
  214. if (start) {
  215. hlStart -= start - 1;
  216. hlEnd -= start - 1;
  217. }
  218. for (var j = hlStart; j <= hlEnd; j++) {
  219. editor.setLineClass(j, 'highlight');
  220. }
  221. }
  222. }
  223. element.data('initialized', true);
  224. }
  225. function createPaperScript(element) {
  226. if (element.data('initialized'))
  227. return;
  228. var script = $('script', element).orNull(),
  229. runButton = $('.button.run', element).orNull();
  230. if (!script)
  231. return;
  232. // Now load / parse / execute the script
  233. script.removeAttr('ignore');
  234. var scope = paper.PaperScript.load(script[0]);
  235. if (!runButton)
  236. return;
  237. var canvas = $('canvas', element),
  238. hasResize = canvas.attr('resize'),
  239. showSplit = element.hasClass('split'),
  240. sourceFirst = element.hasClass('source'),
  241. editor = null,
  242. hasBorders = true,
  243. edited = false,
  244. animateExplain,
  245. explain = $('.explain', element).orNull(),
  246. source = $('<div class="source hidden"/>').insertBefore(script);
  247. if (explain) {
  248. explain.addClass('hidden');
  249. var text = explain.html().replace(/http:\/\/([\w.]+)/g, function(url, domain) {
  250. return '<a href="' + url + '">' + domain + '</a>';
  251. }).trim();
  252. // Add explanation bubbles to tickle the visitor's fancy
  253. var explanations = [{
  254. index: 0,
  255. list: [
  256. [text ? 4 : 3, text || ''],
  257. [1, ''],
  258. [4, '<b>Note:</b> You can view and even edit<br>the source right here in the browser'],
  259. [1, ''],
  260. [3, 'To do so, simply press the <b>Source</b> button &rarr;']
  261. ]
  262. }, {
  263. index: 0,
  264. indexIfEdited: 3, // Skip first sentence if user has already edited code
  265. list: [
  266. [4, ''],
  267. [3, 'Why don\'t you try editing the code?'],
  268. [1, ''],
  269. [4, 'To run it again, simply press press <b>Run</b> &rarr;']
  270. ]
  271. }];
  272. var timer,
  273. mode;
  274. animateExplain = function(clearPrevious) {
  275. if (timer)
  276. timer = clearTimeout(timer);
  277. // Set previous mode's index to the end?
  278. if (mode && clearPrevious)
  279. mode.index = mode.list.length;
  280. mode = explanations[source.hasClass('hidden') ? 0 : 1];
  281. if (edited && mode.index < mode.indexIfEdited)
  282. mode.index = mode.indexIfEdited;
  283. var entry = mode.list[mode.index];
  284. if (entry) {
  285. explain.removeClass('hidden');
  286. explain.html(entry[1]);
  287. timer = setTimeout(function() {
  288. // Only increase once we're stepping, not in animate()
  289. // itself, as entering & leaving would continuosly step
  290. mode.index++;
  291. animateExplain();
  292. }, entry[0] * 1000);
  293. }
  294. if (!entry || !entry[1])
  295. explain.addClass('hidden');
  296. };
  297. element
  298. .mouseover(function() {
  299. if (!timer)
  300. animateExplain();
  301. })
  302. .mouseout(function() {
  303. // Check the effect of :hover on button to see if we need
  304. // to turn off...
  305. // TODO: make mouseenter / mouseleave events work again
  306. if (timer && runButton.css('display') == 'none') {
  307. timer = clearTimeout(timer);
  308. explain.addClass('hidden');
  309. }
  310. });
  311. }
  312. function showSource(show) {
  313. source.toggleClass('hidden', !show);
  314. runButton.text(show ? 'Run' : 'Source');
  315. if (explain)
  316. animateExplain(true);
  317. if (show && !editor) {
  318. editor = createCodeMirror(source[0], {
  319. onKeyEvent: function(editor, event) {
  320. edited = true;
  321. }
  322. }, script);
  323. }
  324. }
  325. function runScript() {
  326. // Update script to edited version
  327. var code = editor.getValue();
  328. script.text(code);
  329. // Keep a reference to the used canvas, since we're going to
  330. // fully clear the scope and initialize again with this canvas.
  331. // Support both old and new versions of paper.js for now:
  332. var element = scope.view.element;
  333. // Clear scope first, then evaluate a new script.
  334. scope.clear();
  335. scope.initialize(script[0]);
  336. scope.setup(element);
  337. scope.execute(code);
  338. }
  339. function resize() {
  340. source
  341. .width(element.width() - (hasBorders ? 2 : 1))
  342. .height(element.height() - (hasBorders ? 2 : 0));
  343. if (editor)
  344. editor.refresh();
  345. }
  346. function toggleView() {
  347. var show = source.hasClass('hidden');
  348. resize();
  349. canvas.toggleClass('hidden', show);
  350. showSource(show);
  351. if (!show)
  352. runScript();
  353. // Add extra margin if there is scrolling
  354. var scrollHeight = $('.CodeMirror .CodeMirror-scroll', source).height();
  355. runButton.css('margin-right', scrollHeight > element.height() ? 25 : 8);
  356. }
  357. if (hasResize) {
  358. paper.view.on('resize', resize);
  359. hasBorders = false;
  360. source.css('border-width', '0 0 0 1px');
  361. }
  362. if (showSplit) {
  363. showSource(true);
  364. } else if (sourceFirst) {
  365. toggleView();
  366. }
  367. runButton
  368. .click(function() {
  369. if (showSplit) {
  370. runScript();
  371. } else {
  372. toggleView();
  373. }
  374. return false;
  375. })
  376. .mousedown(function() {
  377. return false;
  378. });
  379. element.data('initialized', true);
  380. }
  381. // Reference (before behaviors)
  382. var lastMember = null;
  383. function toggleMember(member, dontScroll, offsetElement) {
  384. var link = $('.member-link:first', member);
  385. if (!link.length)
  386. return true;
  387. var desc = $('.member-description', member);
  388. var visible = !link.hasClass('hidden');
  389. // Retrieve y-offset before any changes, so we can correct scrolling after
  390. var offset = (offsetElement || member).offset().top;
  391. if (lastMember && !lastMember.is(member)) {
  392. var prev = lastMember;
  393. lastMember = null;
  394. toggleMember(prev, true);
  395. }
  396. lastMember = visible && member;
  397. link.toggleClass('hidden', visible);
  398. desc.toggleClass('hidden', !visible);
  399. if (!dontScroll) {
  400. // Correct scrolling relatively to where we are, by checking the amount
  401. // the element has shifted due to the above toggleMember call, and
  402. // correcting by 11px offset, caused by 1px border and 10px padding.
  403. var scroll = $(document).scrollTop();
  404. // Only change hash if we're not in frames, since there are redrawing
  405. // issues with that on Chrome.
  406. if (parent === self)
  407. window.location.hash = visible ? member.attr('id') : '';
  408. $(document).scrollTop(scroll
  409. + (visible ? desc : link).offset().top - offset
  410. + 11 * (visible ? 1 : -1));
  411. }
  412. if (!member.data('initialized') && visible) {
  413. behaviors.code();
  414. behaviors.paperscript();
  415. member.data('initialized', true);
  416. }
  417. return false;
  418. }
  419. $(function() {
  420. $('.reference .member').each(function() {
  421. var member = $(this);
  422. var link = $('.member-link', member);
  423. // Add header to description, with link and closing button
  424. var header = $('<div class="member-header"/>').prependTo($('.member-description', member));
  425. // Clone link, but remove name, id and change href
  426. link.clone().removeAttr('name').removeAttr('id').attr('href', '#').appendTo(header);
  427. // Add closing button.
  428. header.append('<div class="member-close"><input type="button" value="Close"></div>');
  429. });
  430. // Give open / close buttons behavior
  431. $('.reference')
  432. .on('click', '.member-link, .member-close', function() {
  433. return toggleMember($(this).parents('.member'));
  434. })
  435. .on('click', '.member-text a', function() {
  436. if (this.href.match(/^(.*?)\/?#|$/)[1] === document.location.href.match(/^(.*?)\/?(?:#|$)/)[1]) {
  437. toggleMember($(this.href.match(/(#.*)$/)[1]), false, $(this));
  438. return false;
  439. }
  440. });
  441. });
  442. // DOM-Ready
  443. $(function() {
  444. for (var i in behaviors)
  445. behaviors[i]();
  446. });