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.

853 lines
26 KiB

10 years ago
  1. /*
  2. * # Semantic - Sidebar
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2014 Contributor
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. "use strict";
  13. $.fn.sidebar = function(parameters) {
  14. var
  15. $allModules = $(this),
  16. $head = $('head'),
  17. moduleSelector = $allModules.selector || '',
  18. time = new Date().getTime(),
  19. performance = [],
  20. query = arguments[0],
  21. methodInvoked = (typeof query == 'string'),
  22. queryArguments = [].slice.call(arguments, 1),
  23. requestAnimationFrame = window.requestAnimationFrame
  24. || window.mozRequestAnimationFrame
  25. || window.webkitRequestAnimationFrame
  26. || window.msRequestAnimationFrame
  27. || function(callback) { setTimeout(callback, 0); },
  28. returnedValue
  29. ;
  30. $allModules
  31. .each(function() {
  32. var
  33. settings = ( $.isPlainObject(parameters) )
  34. ? $.extend(true, {}, $.fn.sidebar.settings, parameters)
  35. : $.extend({}, $.fn.sidebar.settings),
  36. selector = settings.selector,
  37. className = settings.className,
  38. namespace = settings.namespace,
  39. error = settings.error,
  40. eventNamespace = '.' + namespace,
  41. moduleNamespace = 'module-' + namespace,
  42. $module = $(this),
  43. $context = $(settings.context),
  44. $style = $('style[title=' + namespace + ']'),
  45. $sidebars = $context.children(selector.sidebar),
  46. $pusher = $context.children(selector.pusher),
  47. $page = $pusher.children(selector.page),
  48. $fixed = $pusher.find(selector.fixed),
  49. element = this,
  50. instance = $module.data(moduleNamespace),
  51. currentScroll,
  52. transitionEnd,
  53. module
  54. ;
  55. module = {
  56. initialize: function() {
  57. module.debug('Initializing sidebar', $module);
  58. transitionEnd = module.get.transitionEvent();
  59. // cache on initialize
  60. if( module.is.legacy() ) {
  61. settings.useLegacy = true;
  62. }
  63. module.setup.context();
  64. // avoid locking rendering to change layout if included in onReady
  65. requestAnimationFrame(module.setup.layout);
  66. module.instantiate();
  67. },
  68. instantiate: function() {
  69. module.verbose('Storing instance of module', module);
  70. instance = module;
  71. $module
  72. .data(moduleNamespace, module)
  73. ;
  74. },
  75. destroy: function() {
  76. module.verbose('Destroying previous module for', $module);
  77. $module
  78. .off(eventNamespace)
  79. .removeData(moduleNamespace)
  80. ;
  81. },
  82. event: {
  83. clickaway: function(event) {
  84. if( $module.find(event.target).size() === 0 && $(event.target).filter($module).size() === 0 ) {
  85. module.verbose('User clicked on dimmed page');
  86. module.hide();
  87. }
  88. },
  89. scroll: function(event) {
  90. if( $module.find(event.target).size() === 0 && $(event.target).filter($module).size() === 0 ) {
  91. event.preventDefault();
  92. }
  93. }
  94. },
  95. bind: {
  96. clickaway: function() {
  97. if(settings.scrollLock) {
  98. $(window)
  99. .on('DOMMouseScroll' + eventNamespace, module.event.scroll)
  100. ;
  101. }
  102. $context
  103. .on('click' + eventNamespace, module.event.clickaway)
  104. .on('touchend' + eventNamespace, module.event.clickaway)
  105. ;
  106. }
  107. },
  108. unbind: {
  109. clickaway: function() {
  110. $context
  111. .off(eventNamespace)
  112. ;
  113. if(settings.scrollLock) {
  114. $(window).off('DOMMouseScroll' + eventNamespace);
  115. }
  116. }
  117. },
  118. refresh: function() {
  119. module.verbose('Refreshing selector cache');
  120. $context = $(settings.context);
  121. $style = $('style[title=' + namespace + ']');
  122. $sidebars = $context.children(selector.sidebar);
  123. $pusher = $context.children(selector.pusher);
  124. $page = $pusher.children(selector.page);
  125. $fixed = $pusher.find(selector.fixed);
  126. },
  127. repaint: function() {
  128. module.verbose('Forcing repaint event');
  129. var fakeAssignment = $context[0].offsetWidth;
  130. },
  131. setup: {
  132. layout: function() {
  133. if( $context.find(selector.pusher).size() === 0 ) {
  134. module.debug('Adding wrapper element for sidebar');
  135. $pusher = $('<div class="pusher" />');
  136. $page = $('<div class="page" />');
  137. $pusher.append($page);
  138. $context
  139. .children()
  140. .not(selector.omitted)
  141. .not($sidebars)
  142. .wrapAll($pusher)
  143. ;
  144. }
  145. if($module.prevAll($page)[0] !== $page[0]) {
  146. module.debug('Moved sidebar to correct parent element');
  147. $module.detach().prependTo($context);
  148. }
  149. module.refresh();
  150. },
  151. context: function() {
  152. module.verbose('Adding pusshable class to wrapper');
  153. $context.addClass(className.pushable);
  154. }
  155. },
  156. attachEvents: function(selector, event) {
  157. var
  158. $toggle = $(selector)
  159. ;
  160. event = $.isFunction(module[event])
  161. ? module[event]
  162. : module.toggle
  163. ;
  164. if($toggle.size() > 0) {
  165. module.debug('Attaching sidebar events to element', selector, event);
  166. $toggle
  167. .on('click' + eventNamespace, event)
  168. ;
  169. }
  170. else {
  171. module.error(error.notFound, selector);
  172. }
  173. },
  174. show: function(callback) {
  175. var
  176. animateMethod = (settings.useLegacy)
  177. ? module.legacyPushPage
  178. : module.pushPage
  179. ;
  180. callback = $.isFunction(callback)
  181. ? callback
  182. : function(){}
  183. ;
  184. if(module.is.closed() || module.is.outward()) {
  185. if(settings.overlay) {
  186. module.error(error.overlay);
  187. settings.transition = 'overlay';
  188. }
  189. if(settings.transition !== 'overlay') {
  190. module.hideAll();
  191. }
  192. animateMethod(function() {
  193. $.proxy(callback, element)();
  194. $.proxy(settings.onShow, element)();
  195. });
  196. $.proxy(settings.onChange, element)();
  197. $.proxy(settings.onVisible, element)();
  198. }
  199. else {
  200. module.debug('Sidebar is already visible');
  201. }
  202. },
  203. hide: function(callback) {
  204. var
  205. animateMethod = (settings.useLegacy)
  206. ? module.legacyPullPage
  207. : module.pullPage
  208. ;
  209. callback = $.isFunction(callback)
  210. ? callback
  211. : function(){}
  212. ;
  213. if(module.is.visible() || module.is.inward()) {
  214. module.debug('Hiding sidebar', callback);
  215. animateMethod(function() {
  216. $.proxy(callback, element)();
  217. $.proxy(settings.onHidden, element)();
  218. });
  219. $.proxy(settings.onChange, element)();
  220. $.proxy(settings.onHide, element)();
  221. }
  222. },
  223. hideAll: function() {
  224. var
  225. $visibleSidebars = $sidebars.find('.' + className.visible)
  226. ;
  227. $visibleSidebars
  228. .sidebar('hide')
  229. ;
  230. },
  231. toggle: function() {
  232. module.verbose('Determining toggled direction');
  233. if(module.is.closed() || module.is.outward()) {
  234. module.show();
  235. }
  236. else {
  237. module.hide();
  238. }
  239. },
  240. pushPage: function(callback) {
  241. var
  242. transition = module.get.transition(),
  243. $transition = (transition == 'safe')
  244. ? $context
  245. : (transition == 'overlay')
  246. ? $module
  247. : $pusher,
  248. animate
  249. ;
  250. callback = $.isFunction(callback)
  251. ? callback
  252. : function(){}
  253. ;
  254. animate = function() {
  255. module.remove.outward();
  256. module.set.visible();
  257. module.set.transition();
  258. module.set.direction();
  259. requestAnimationFrame(function() {
  260. module.set.inward();
  261. module.set.pushed();
  262. });
  263. };
  264. $transition
  265. .off(transitionEnd + eventNamespace)
  266. .on(transitionEnd + eventNamespace, function(event) {
  267. if( event.target == $transition[0] ) {
  268. $transition.off(transitionEnd + eventNamespace);
  269. module.remove.inward();
  270. module.bind.clickaway();
  271. module.set.active();
  272. $.proxy(callback, element)();
  273. }
  274. })
  275. ;
  276. module.verbose('Adding context push state', $context);
  277. if(transition === 'overlay') {
  278. requestAnimationFrame(animate);
  279. }
  280. else {
  281. if(settings.transition == 'scale down' || module.is.mobile()) {
  282. $module.scrollTop(0);
  283. currentScroll = $(window).scrollTop();
  284. window.scrollTo(0, 0);
  285. }
  286. module.remove.allVisible();
  287. requestAnimationFrame(animate);
  288. }
  289. },
  290. pullPage: function(callback) {
  291. var
  292. transition = module.get.transition(),
  293. $transition = (transition == 'safe')
  294. ? $context
  295. : (transition == 'overlay')
  296. ? $module
  297. : $pusher
  298. ;
  299. callback = $.isFunction(callback)
  300. ? callback
  301. : function(){}
  302. ;
  303. module.verbose('Removing context push state', module.get.direction());
  304. module.unbind.clickaway();
  305. $transition
  306. .off(transitionEnd + eventNamespace)
  307. .on(transitionEnd + eventNamespace, function(event) {
  308. if( event.target == $transition[0] ) {
  309. $transition.off(transitionEnd + eventNamespace);
  310. module.remove.transition();
  311. module.remove.direction();
  312. module.remove.outward();
  313. module.remove.visible();
  314. if(transition == 'scale down' || (settings.returnScroll && transition !== 'overlay' && module.is.mobile()) ) {
  315. window.scrollTo(0, currentScroll);
  316. }
  317. $.proxy(callback, element)();
  318. }
  319. })
  320. ;
  321. requestAnimationFrame(function() {
  322. module.remove.inward();
  323. module.set.outward();
  324. module.remove.active();
  325. module.remove.pushed();
  326. });
  327. },
  328. legacyPushPage: function(callback) {
  329. var
  330. distance = $module.width(),
  331. direction = module.get.direction(),
  332. properties = {}
  333. ;
  334. distance = distance || $module.width();
  335. callback = $.isFunction(callback)
  336. ? callback
  337. : function(){}
  338. ;
  339. properties[direction] = distance;
  340. module.debug('Using javascript to push context', properties);
  341. module.set.visible();
  342. module.set.transition();
  343. module.set.direction();
  344. module.set.inward();
  345. module.set.pushed();
  346. $context
  347. .animate(properties, settings.duration, settings.easing, function() {
  348. module.remove.inward();
  349. module.bind.clickaway();
  350. module.set.active();
  351. $.proxy(callback, module)();
  352. })
  353. ;
  354. },
  355. legacyPullPage: function(callback) {
  356. var
  357. distance = 0,
  358. direction = module.get.direction(),
  359. properties = {}
  360. ;
  361. distance = distance || $module.width();
  362. callback = $.isFunction(callback)
  363. ? callback
  364. : function(){}
  365. ;
  366. properties[direction] = '0px';
  367. module.debug('Using javascript to pull context', properties);
  368. module.unbind.clickaway();
  369. module.set.outward();
  370. module.remove.active();
  371. module.remove.pushed();
  372. $context
  373. .animate(properties, settings.duration, settings.easing, function() {
  374. module.remove.transition();
  375. module.remove.direction();
  376. module.remove.outward();
  377. module.remove.visible();
  378. $.proxy(callback, module)();
  379. })
  380. ;
  381. },
  382. set: {
  383. active: function() {
  384. $context.addClass(className.active);
  385. },
  386. direction: function(direction) {
  387. direction = direction || module.get.direction();
  388. $context.addClass(className[direction]);
  389. },
  390. visible: function() {
  391. $module.addClass(className.visible);
  392. },
  393. transition: function(transition) {
  394. transition = transition || module.get.transition();
  395. $context.addClass(transition);
  396. },
  397. inward: function() {
  398. $context.addClass(className.inward);
  399. },
  400. outward: function() {
  401. $context.addClass(className.outward);
  402. },
  403. pushed: function() {
  404. if(settings.dimPage) {
  405. $page.addClass(className.dimmed);
  406. }
  407. $context.addClass(className.pushed);
  408. }
  409. },
  410. remove: {
  411. active: function() {
  412. $context.removeClass(className.active);
  413. },
  414. visible: function() {
  415. $module.removeClass(className.visible);
  416. },
  417. allVisible: function() {
  418. if($sidebars.hasClass(className.visible)) {
  419. module.debug('Other sidebars visible, hiding');
  420. $sidebars.removeClass(className.visible);
  421. }
  422. },
  423. transition: function(transition) {
  424. transition = transition || module.get.transition();
  425. $context.removeClass(transition);
  426. },
  427. pushed: function() {
  428. if(settings.dimPage) {
  429. $page.removeClass(className.dimmed);
  430. }
  431. $context.removeClass(className.pushed);
  432. },
  433. inward: function() {
  434. $context.removeClass(className.inward);
  435. },
  436. outward: function() {
  437. $context.removeClass(className.outward);
  438. },
  439. direction: function(direction) {
  440. direction = direction || module.get.direction();
  441. $context.removeClass(className[direction]);
  442. }
  443. },
  444. get: {
  445. direction: function() {
  446. if($module.hasClass(className.top)) {
  447. return className.top;
  448. }
  449. else if($module.hasClass(className.right)) {
  450. return className.right;
  451. }
  452. else if($module.hasClass(className.bottom)) {
  453. return className.bottom;
  454. }
  455. return className.left;
  456. },
  457. transition: function() {
  458. var
  459. direction = module.get.direction(),
  460. transition
  461. ;
  462. return ( module.is.mobile() )
  463. ? (settings.mobileTransition == 'auto')
  464. ? settings.defaultTransition.mobile[direction]
  465. : settings.mobileTransition
  466. : (settings.transition == 'auto')
  467. ? settings.defaultTransition.computer[direction]
  468. : settings.transition
  469. ;
  470. },
  471. transitionEvent: function() {
  472. var
  473. element = document.createElement('element'),
  474. transitions = {
  475. 'transition' :'transitionend',
  476. 'OTransition' :'oTransitionEnd',
  477. 'MozTransition' :'transitionend',
  478. 'WebkitTransition' :'webkitTransitionEnd'
  479. },
  480. transition
  481. ;
  482. for(transition in transitions){
  483. if( element.style[transition] !== undefined ){
  484. return transitions[transition];
  485. }
  486. }
  487. }
  488. },
  489. is: {
  490. legacy: function() {
  491. var
  492. element = document.createElement('div'),
  493. transforms = {
  494. 'webkitTransform' :'-webkit-transform',
  495. 'OTransform' :'-o-transform',
  496. 'msTransform' :'-ms-transform',
  497. 'MozTransform' :'-moz-transform',
  498. 'transform' :'transform'
  499. },
  500. has3D
  501. ;
  502. // Add it to the body to get the computed style.
  503. document.body.insertBefore(element, null);
  504. for (var transform in transforms) {
  505. if (element.style[transform] !== undefined) {
  506. element.style[transform] = "translate3d(1px,1px,1px)";
  507. has3D = window.getComputedStyle(element).getPropertyValue(transforms[transform]);
  508. }
  509. }
  510. document.body.removeChild(element);
  511. return !(has3D !== undefined && has3D.length > 0 && has3D !== 'none');
  512. },
  513. mobile: function() {
  514. var
  515. userAgent = navigator.userAgent,
  516. mobileRegExp = /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/,
  517. isMobile = mobileRegExp.test(userAgent)
  518. ;
  519. if(isMobile) {
  520. module.verbose('Browser was found to be mobile', userAgent);
  521. return true;
  522. }
  523. else {
  524. module.verbose('Browser is not mobile, using regular transition', userAgent);
  525. return false;
  526. }
  527. },
  528. closed: function() {
  529. return !module.is.visible();
  530. },
  531. visible: function() {
  532. return $module.hasClass(className.visible);
  533. },
  534. vertical: function() {
  535. return $module.hasClass(className.top);
  536. },
  537. inward: function() {
  538. return $context.hasClass(className.inward);
  539. },
  540. outward: function() {
  541. return $context.hasClass(className.outward);
  542. },
  543. animating: function() {
  544. return module.is.inward() || module.is.outward();
  545. }
  546. },
  547. setting: function(name, value) {
  548. module.debug('Changing setting', name, value);
  549. if( $.isPlainObject(name) ) {
  550. $.extend(true, settings, name);
  551. }
  552. else if(value !== undefined) {
  553. settings[name] = value;
  554. }
  555. else {
  556. return settings[name];
  557. }
  558. },
  559. internal: function(name, value) {
  560. if( $.isPlainObject(name) ) {
  561. $.extend(true, module, name);
  562. }
  563. else if(value !== undefined) {
  564. module[name] = value;
  565. }
  566. else {
  567. return module[name];
  568. }
  569. },
  570. debug: function() {
  571. if(settings.debug) {
  572. if(settings.performance) {
  573. module.performance.log(arguments);
  574. }
  575. else {
  576. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  577. module.debug.apply(console, arguments);
  578. }
  579. }
  580. },
  581. verbose: function() {
  582. if(settings.verbose && settings.debug) {
  583. if(settings.performance) {
  584. module.performance.log(arguments);
  585. }
  586. else {
  587. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  588. module.verbose.apply(console, arguments);
  589. }
  590. }
  591. },
  592. error: function() {
  593. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  594. module.error.apply(console, arguments);
  595. },
  596. performance: {
  597. log: function(message) {
  598. var
  599. currentTime,
  600. executionTime,
  601. previousTime
  602. ;
  603. if(settings.performance) {
  604. currentTime = new Date().getTime();
  605. previousTime = time || currentTime;
  606. executionTime = currentTime - previousTime;
  607. time = currentTime;
  608. performance.push({
  609. 'Name' : message[0],
  610. 'Arguments' : [].slice.call(message, 1) || '',
  611. 'Element' : element,
  612. 'Execution Time' : executionTime
  613. });
  614. }
  615. clearTimeout(module.performance.timer);
  616. module.performance.timer = setTimeout(module.performance.display, 100);
  617. },
  618. display: function() {
  619. var
  620. title = settings.name + ':',
  621. totalTime = 0
  622. ;
  623. time = false;
  624. clearTimeout(module.performance.timer);
  625. $.each(performance, function(index, data) {
  626. totalTime += data['Execution Time'];
  627. });
  628. title += ' ' + totalTime + 'ms';
  629. if(moduleSelector) {
  630. title += ' \'' + moduleSelector + '\'';
  631. }
  632. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  633. console.groupCollapsed(title);
  634. if(console.table) {
  635. console.table(performance);
  636. }
  637. else {
  638. $.each(performance, function(index, data) {
  639. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  640. });
  641. }
  642. console.groupEnd();
  643. }
  644. performance = [];
  645. }
  646. },
  647. invoke: function(query, passedArguments, context) {
  648. var
  649. object = instance,
  650. maxDepth,
  651. found,
  652. response
  653. ;
  654. passedArguments = passedArguments || queryArguments;
  655. context = element || context;
  656. if(typeof query == 'string' && object !== undefined) {
  657. query = query.split(/[\. ]/);
  658. maxDepth = query.length - 1;
  659. $.each(query, function(depth, value) {
  660. var camelCaseValue = (depth != maxDepth)
  661. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  662. : query
  663. ;
  664. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  665. object = object[camelCaseValue];
  666. }
  667. else if( object[camelCaseValue] !== undefined ) {
  668. found = object[camelCaseValue];
  669. return false;
  670. }
  671. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  672. object = object[value];
  673. }
  674. else if( object[value] !== undefined ) {
  675. found = object[value];
  676. return false;
  677. }
  678. else {
  679. module.error(error.method, query);
  680. return false;
  681. }
  682. });
  683. }
  684. if ( $.isFunction( found ) ) {
  685. response = found.apply(context, passedArguments);
  686. }
  687. else if(found !== undefined) {
  688. response = found;
  689. }
  690. if($.isArray(returnedValue)) {
  691. returnedValue.push(response);
  692. }
  693. else if(returnedValue !== undefined) {
  694. returnedValue = [returnedValue, response];
  695. }
  696. else if(response !== undefined) {
  697. returnedValue = response;
  698. }
  699. return found;
  700. }
  701. }
  702. ;
  703. if(methodInvoked) {
  704. if(instance === undefined) {
  705. module.initialize();
  706. }
  707. module.invoke(query);
  708. }
  709. else {
  710. if(instance !== undefined) {
  711. module.destroy();
  712. }
  713. module.initialize();
  714. }
  715. });
  716. return (returnedValue !== undefined)
  717. ? returnedValue
  718. : this
  719. ;
  720. };
  721. $.fn.sidebar.settings = {
  722. name : 'Sidebar',
  723. namespace : 'sidebar',
  724. debug : false,
  725. verbose : false,
  726. performance : false,
  727. workaround : false,
  728. transition : 'auto',
  729. mobileTransition : 'auto',
  730. defaultTransition : {
  731. computer: {
  732. left : 'uncover',
  733. right : 'uncover',
  734. top : 'overlay',
  735. bottom : 'overlay'
  736. },
  737. mobile: {
  738. left : 'uncover',
  739. right : 'uncover',
  740. top : 'overlay',
  741. bottom : 'overlay'
  742. }
  743. },
  744. context : 'body',
  745. exclusive : true,
  746. dimPage : true,
  747. scrollLock : false,
  748. returnScroll : true,
  749. useLegacy : false,
  750. duration : 500,
  751. easing : 'easeInOutQuint',
  752. onChange : function(){},
  753. onShow : function(){},
  754. onHide : function(){},
  755. onHidden : function(){},
  756. onVisible : function(){},
  757. className : {
  758. active : 'active',
  759. bottom : 'bottom',
  760. dimmed : 'dimmed',
  761. inward : 'show',
  762. left : 'left',
  763. outward : 'hide',
  764. pushable : 'pushable',
  765. pushed : 'pushed',
  766. right : 'right',
  767. top : 'top',
  768. visible : 'visible'
  769. },
  770. selector: {
  771. fixed : '.ui.fixed',
  772. omitted : 'script, link, style, .ui.modal, .ui.dimmer, .ui.nag, .ui.fixed',
  773. page : '.page',
  774. pusher : '.pusher',
  775. sidebar : '.ui.sidebar'
  776. },
  777. error : {
  778. method : 'The method you called is not defined.',
  779. overlay : 'The overlay setting is no longer supported, use animation: overlay',
  780. notFound : 'There were no elements that matched the specified selector'
  781. }
  782. };
  783. // Adds easing
  784. $.extend( $.easing, {
  785. easeInOutQuint: function (x, t, b, c, d) {
  786. if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
  787. return c/2*((t-=2)*t*t*t*t + 2) + b;
  788. }
  789. });
  790. })( jQuery, window , document );