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.

830 lines
25 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
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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * # Semantic - Dropdown
  3. * http://github.com/jlukic/semantic-ui/
  4. *
  5. *
  6. * Copyright 2013 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. $.fn.dropdown = function(parameters) {
  13. var
  14. $allModules = $(this),
  15. $document = $(document),
  16. moduleSelector = $allModules.selector || '',
  17. hasTouch = ('ontouchstart' in document.documentElement),
  18. time = new Date().getTime(),
  19. performance = [],
  20. query = arguments[0],
  21. methodInvoked = (typeof query == 'string'),
  22. queryArguments = [].slice.call(arguments, 1),
  23. returnedValue
  24. ;
  25. $allModules
  26. .each(function() {
  27. var
  28. settings = ( $.isPlainObject(parameters) )
  29. ? $.extend(true, {}, $.fn.dropdown.settings, parameters)
  30. : $.extend({}, $.fn.dropdown.settings),
  31. className = settings.className,
  32. metadata = settings.metadata,
  33. namespace = settings.namespace,
  34. selector = settings.selector,
  35. error = settings.error,
  36. eventNamespace = '.' + namespace,
  37. moduleNamespace = 'module-' + namespace,
  38. $module = $(this),
  39. $item = $module.find(selector.item),
  40. $text = $module.find(selector.text),
  41. $input = $module.find(selector.input),
  42. $menu = $module.children(selector.menu),
  43. element = this,
  44. instance = $module.data(moduleNamespace),
  45. module
  46. ;
  47. module = {
  48. initialize: function() {
  49. module.debug('Initializing dropdown', settings);
  50. module.set.selected();
  51. if(hasTouch) {
  52. module.bind.touchEvents();
  53. }
  54. // no use detecting mouse events because touch devices emulate them
  55. module.bind.mouseEvents();
  56. module.instantiate();
  57. },
  58. instantiate: function() {
  59. module.verbose('Storing instance of dropdown', module);
  60. instance = module;
  61. $module
  62. .data(moduleNamespace, module)
  63. ;
  64. },
  65. destroy: function() {
  66. module.verbose('Destroying previous dropdown for', $module);
  67. $item
  68. .off(eventNamespace)
  69. ;
  70. $module
  71. .off(eventNamespace)
  72. .removeData(moduleNamespace)
  73. ;
  74. },
  75. bind: {
  76. touchEvents: function() {
  77. module.debug('Touch device detected binding touch events');
  78. $module
  79. .on('touchstart' + eventNamespace, module.event.test.toggle)
  80. ;
  81. $item
  82. .on('touchstart' + eventNamespace, module.event.item.mouseenter)
  83. .on('touchstart' + eventNamespace, module.event.item.click)
  84. ;
  85. },
  86. mouseEvents: function() {
  87. module.verbose('Mouse detected binding mouse events');
  88. if(settings.on == 'click') {
  89. $module
  90. .on('click' + eventNamespace, module.event.test.toggle)
  91. ;
  92. }
  93. else if(settings.on == 'hover') {
  94. $module
  95. .on('mouseenter' + eventNamespace, module.delay.show)
  96. .on('mouseleave' + eventNamespace, module.delay.hide)
  97. ;
  98. }
  99. else {
  100. $module
  101. .on(settings.on + eventNamespace, module.toggle)
  102. ;
  103. }
  104. $item
  105. .on('mouseenter' + eventNamespace, module.event.item.mouseenter)
  106. .on('mouseleave' + eventNamespace, module.event.item.mouseleave)
  107. .on('click' + eventNamespace, module.event.item.click)
  108. ;
  109. },
  110. intent: function() {
  111. module.verbose('Binding hide intent event to document');
  112. if(hasTouch) {
  113. $document
  114. .on('touchstart' + eventNamespace, module.event.test.touch)
  115. .on('touchmove' + eventNamespace, module.event.test.touch)
  116. ;
  117. }
  118. $document
  119. .on('click' + eventNamespace, module.event.test.hide)
  120. ;
  121. }
  122. },
  123. unbind: {
  124. intent: function() {
  125. module.verbose('Removing hide intent event from document');
  126. if(hasTouch) {
  127. $document
  128. .off('touchstart' + eventNamespace)
  129. ;
  130. }
  131. $document
  132. .off('click' + eventNamespace)
  133. ;
  134. }
  135. },
  136. event: {
  137. test: {
  138. toggle: function(event) {
  139. if( module.determine.intent(event, module.toggle) ) {
  140. event.preventDefault();
  141. }
  142. },
  143. touch: function(event) {
  144. module.determine.intent(event, function() {
  145. if(event.type == 'touchstart') {
  146. module.timer = setTimeout(module.hide, settings.delay.touch);
  147. }
  148. else if(event.type == 'touchmove') {
  149. clearTimeout(module.timer);
  150. }
  151. });
  152. event.stopPropagation();
  153. },
  154. hide: function(event) {
  155. module.determine.intent(event, module.hide);
  156. }
  157. },
  158. item: {
  159. mouseenter: function(event) {
  160. var
  161. $currentMenu = $(this).find(selector.menu),
  162. $otherMenus = $(this).siblings(selector.item).children(selector.menu)
  163. ;
  164. if( $currentMenu.size() > 0 ) {
  165. clearTimeout(module.itemTimer);
  166. module.itemTimer = setTimeout(function() {
  167. module.animate.hide(false, $otherMenus);
  168. module.verbose('Showing sub-menu', $currentMenu);
  169. module.animate.show(false, $currentMenu);
  170. }, settings.delay.show * 2);
  171. event.preventDefault();
  172. }
  173. },
  174. mouseleave: function(event) {
  175. var
  176. $currentMenu = $(this).find(selector.menu)
  177. ;
  178. if($currentMenu.size() > 0) {
  179. clearTimeout(module.itemTimer);
  180. module.itemTimer = setTimeout(function() {
  181. module.verbose('Hiding sub-menu', $currentMenu);
  182. module.animate.hide(false, $currentMenu);
  183. }, settings.delay.hide);
  184. }
  185. },
  186. click: function (event) {
  187. var
  188. $choice = $(this),
  189. text = ( $choice.data(metadata.text) !== undefined )
  190. ? $choice.data(metadata.text)
  191. : $choice.text(),
  192. value = ( $choice.data(metadata.value) !== undefined)
  193. ? $choice.data(metadata.value)
  194. : text.toLowerCase()
  195. ;
  196. if( $choice.find(selector.menu).size() === 0 ) {
  197. module.determine.selectAction(text, value);
  198. $.proxy(settings.onChange, element)(value, text);
  199. }
  200. }
  201. },
  202. resetStyle: function() {
  203. $(this).removeAttr('style');
  204. }
  205. },
  206. determine: {
  207. selectAction: function(text, value) {
  208. module.verbose('Determining action', settings.action);
  209. if( $.isFunction( module.action[settings.action] ) ) {
  210. module.verbose('Triggering preset action', settings.action, text, value);
  211. module.action[ settings.action ](text, value);
  212. }
  213. else if( $.isFunction(settings.action) ) {
  214. module.verbose('Triggering user action', settings.action, text, value);
  215. settings.action(text, value);
  216. }
  217. else {
  218. module.error(error.action, settings.action);
  219. }
  220. },
  221. intent: function(event, callback) {
  222. module.debug('Determining whether event occurred in dropdown', event.target);
  223. callback = callback || function(){};
  224. if( $(event.target).closest($menu).size() === 0 ) {
  225. module.verbose('Triggering event', callback);
  226. callback();
  227. return true;
  228. }
  229. else {
  230. module.verbose('Event occurred in dropdown, canceling callback');
  231. return false;
  232. }
  233. }
  234. },
  235. action: {
  236. nothing: function() {},
  237. hide: function() {
  238. module.hide();
  239. },
  240. activate: function(text, value) {
  241. value = (value !== undefined)
  242. ? value
  243. : text
  244. ;
  245. module.set.selected(value);
  246. module.set.value(value);
  247. module.hide();
  248. },
  249. /* Deprecated */
  250. auto: function(text, value) {
  251. value = (value !== undefined)
  252. ? value
  253. : text
  254. ;
  255. module.set.selected(value);
  256. module.set.value(value);
  257. module.hide();
  258. },
  259. /* Deprecated */
  260. changeText: function(text, value) {
  261. value = (value !== undefined)
  262. ? value
  263. : text
  264. ;
  265. module.set.selected(value);
  266. module.hide();
  267. },
  268. /* Deprecated */
  269. updateForm: function(text, value) {
  270. value = (value !== undefined)
  271. ? value
  272. : text
  273. ;
  274. module.set.selected(value);
  275. module.set.value(value);
  276. module.hide();
  277. }
  278. },
  279. get: {
  280. text: function() {
  281. return $text.text();
  282. },
  283. value: function() {
  284. return $input.val();
  285. },
  286. item: function(value) {
  287. var
  288. $selectedItem
  289. ;
  290. value = (value !== undefined)
  291. ? value
  292. : ( module.get.value() !== undefined)
  293. ? module.get.value()
  294. : module.get.text()
  295. ;
  296. if(value) {
  297. $item
  298. .each(function() {
  299. var
  300. $choice = $(this),
  301. optionText = ( $choice.data(metadata.text) !== undefined )
  302. ? $choice.data(metadata.text)
  303. : $choice.text(),
  304. optionValue = ( $choice.data(metadata.value) !== undefined )
  305. ? $choice.data(metadata.value)
  306. : optionText.toLowerCase()
  307. ;
  308. if( optionValue == value || optionText == value ) {
  309. $selectedItem = $(this);
  310. return false;
  311. }
  312. })
  313. ;
  314. }
  315. else {
  316. value = module.get.text();
  317. }
  318. return $selectedItem || false;
  319. }
  320. },
  321. set: {
  322. text: function(text) {
  323. module.debug('Changing text', text, $text);
  324. $text.removeClass(className.placeholder);
  325. $text.text(text);
  326. },
  327. value: function(value) {
  328. module.debug('Adding selected value to hidden input', value, $input);
  329. $input.val(value);
  330. },
  331. active: function() {
  332. $module.addClass(className.active);
  333. },
  334. visible: function() {
  335. $module.addClass(className.visible);
  336. },
  337. selected: function(value) {
  338. var
  339. $selectedItem = module.get.item(value),
  340. selectedText
  341. ;
  342. if($selectedItem) {
  343. module.debug('Setting selected menu item to', $selectedItem);
  344. selectedText = ($selectedItem.data(metadata.text) !== undefined)
  345. ? $selectedItem.data(metadata.text)
  346. : $selectedItem.text()
  347. ;
  348. $item
  349. .removeClass(className.active)
  350. ;
  351. $selectedItem
  352. .addClass(className.active)
  353. ;
  354. module.set.text(selectedText);
  355. }
  356. }
  357. },
  358. remove: {
  359. active: function() {
  360. $module.removeClass(className.active);
  361. },
  362. visible: function() {
  363. $module.removeClass(className.visible);
  364. }
  365. },
  366. is: {
  367. selection: function() {
  368. return $module.hasClass(className.selection);
  369. },
  370. animated: function($subMenu) {
  371. return ($subMenu)
  372. ? $subMenu.is(':animated') || $subMenu.transition('is animating')
  373. : $menu.is(':animated') || $menu.transition('is animating')
  374. ;
  375. },
  376. visible: function($subMenu) {
  377. return ($subMenu)
  378. ? $subMenu.is(':visible')
  379. : $menu.is(':visible')
  380. ;
  381. },
  382. hidden: function($subMenu) {
  383. return ($subMenu)
  384. ? $subMenu.is(':not(:visible)')
  385. : $menu.is(':not(:visible)')
  386. ;
  387. }
  388. },
  389. can: {
  390. click: function() {
  391. return (hasTouch || settings.on == 'click');
  392. },
  393. show: function() {
  394. return !$module.hasClass(className.disabled);
  395. }
  396. },
  397. animate: {
  398. show: function(callback, $subMenu) {
  399. var
  400. $currentMenu = $subMenu || $menu
  401. ;
  402. callback = callback || function(){};
  403. if( module.is.hidden($currentMenu) ) {
  404. module.verbose('Doing menu show animation', $currentMenu);
  405. if(settings.transition == 'none') {
  406. callback();
  407. }
  408. else if($.fn.transition !== undefined && $module.transition('is supported')) {
  409. $currentMenu.transition({
  410. animation : settings.transition + ' in',
  411. duration : settings.duration,
  412. complete : callback,
  413. queue : false
  414. });
  415. }
  416. else if(settings.transition == 'slide down') {
  417. $currentMenu
  418. .hide()
  419. .clearQueue()
  420. .children()
  421. .clearQueue()
  422. .css('opacity', 0)
  423. .delay(50)
  424. .animate({
  425. opacity : 1
  426. }, settings.duration, 'easeOutQuad', module.event.resetStyle)
  427. .end()
  428. .slideDown(100, 'easeOutQuad', function() {
  429. $.proxy(module.event.resetStyle, this)();
  430. callback();
  431. })
  432. ;
  433. }
  434. else if(settings.transition == 'fade') {
  435. $currentMenu
  436. .hide()
  437. .clearQueue()
  438. .fadeIn(settings.duration, function() {
  439. $.proxy(module.event.resetStyle, this)();
  440. callback();
  441. })
  442. ;
  443. }
  444. else {
  445. module.error(error.transition, settings.transition);
  446. }
  447. }
  448. },
  449. hide: function(callback, $subMenu) {
  450. var
  451. $currentMenu = $subMenu || $menu
  452. ;
  453. callback = callback || function(){};
  454. if(module.is.visible($currentMenu) ) {
  455. module.verbose('Doing menu hide animation', $currentMenu);
  456. if($.fn.transition !== undefined && $module.transition('is supported')) {
  457. $currentMenu.transition({
  458. animation : settings.transition + ' out',
  459. duration : settings.duration,
  460. complete : callback,
  461. queue : false
  462. });
  463. }
  464. else if(settings.transition == 'none') {
  465. callback();
  466. }
  467. else if(settings.transition == 'slide down') {
  468. $currentMenu
  469. .show()
  470. .clearQueue()
  471. .children()
  472. .clearQueue()
  473. .css('opacity', 1)
  474. .animate({
  475. opacity : 0
  476. }, 100, 'easeOutQuad', module.event.resetStyle)
  477. .end()
  478. .delay(50)
  479. .slideUp(100, 'easeOutQuad', function() {
  480. $.proxy(module.event.resetStyle, this)();
  481. callback();
  482. })
  483. ;
  484. }
  485. else if(settings.transition == 'fade') {
  486. $currentMenu
  487. .show()
  488. .clearQueue()
  489. .fadeOut(150, function() {
  490. $.proxy(module.event.resetStyle, this)();
  491. callback();
  492. })
  493. ;
  494. }
  495. else {
  496. module.error(error.transition);
  497. }
  498. }
  499. }
  500. },
  501. show: function() {
  502. module.debug('Checking if dropdown can show');
  503. if( module.is.hidden() ) {
  504. module.hideOthers();
  505. module.set.active();
  506. module.animate.show(function() {
  507. if( module.can.click() ) {
  508. module.bind.intent();
  509. }
  510. module.set.visible();
  511. });
  512. $.proxy(settings.onShow, element)();
  513. }
  514. },
  515. hide: function() {
  516. if( !module.is.animated() && module.is.visible() ) {
  517. module.debug('Hiding dropdown');
  518. if( module.can.click() ) {
  519. module.unbind.intent();
  520. }
  521. module.remove.active();
  522. module.animate.hide(module.remove.visible);
  523. $.proxy(settings.onHide, element)();
  524. }
  525. },
  526. delay: {
  527. show: function() {
  528. module.verbose('Delaying show event to ensure user intent');
  529. clearTimeout(module.timer);
  530. module.timer = setTimeout(module.show, settings.delay.show);
  531. },
  532. hide: function() {
  533. module.verbose('Delaying hide event to ensure user intent');
  534. clearTimeout(module.timer);
  535. module.timer = setTimeout(module.hide, settings.delay.hide);
  536. }
  537. },
  538. hideOthers: function() {
  539. module.verbose('Finding other dropdowns to hide');
  540. $allModules
  541. .not($module)
  542. .has(selector.menu + ':visible')
  543. .dropdown('hide')
  544. ;
  545. },
  546. toggle: function() {
  547. module.verbose('Toggling menu visibility');
  548. if( module.is.hidden() ) {
  549. module.show();
  550. }
  551. else {
  552. module.hide();
  553. }
  554. },
  555. setting: function(name, value) {
  556. if( $.isPlainObject(name) ) {
  557. $.extend(true, settings, name);
  558. }
  559. else if(value !== undefined) {
  560. settings[name] = value;
  561. }
  562. else {
  563. return settings[name];
  564. }
  565. },
  566. internal: function(name, value) {
  567. if( $.isPlainObject(name) ) {
  568. $.extend(true, module, name);
  569. }
  570. else if(value !== undefined) {
  571. module[name] = value;
  572. }
  573. else {
  574. return module[name];
  575. }
  576. },
  577. debug: function() {
  578. if(settings.debug) {
  579. if(settings.performance) {
  580. module.performance.log(arguments);
  581. }
  582. else {
  583. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  584. module.debug.apply(console, arguments);
  585. }
  586. }
  587. },
  588. verbose: function() {
  589. if(settings.verbose && settings.debug) {
  590. if(settings.performance) {
  591. module.performance.log(arguments);
  592. }
  593. else {
  594. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  595. module.verbose.apply(console, arguments);
  596. }
  597. }
  598. },
  599. error: function() {
  600. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  601. module.error.apply(console, arguments);
  602. },
  603. performance: {
  604. log: function(message) {
  605. var
  606. currentTime,
  607. executionTime,
  608. previousTime
  609. ;
  610. if(settings.performance) {
  611. currentTime = new Date().getTime();
  612. previousTime = time || currentTime;
  613. executionTime = currentTime - previousTime;
  614. time = currentTime;
  615. performance.push({
  616. 'Element' : element,
  617. 'Name' : message[0],
  618. 'Arguments' : [].slice.call(message, 1) || '',
  619. 'Execution Time' : executionTime
  620. });
  621. }
  622. clearTimeout(module.performance.timer);
  623. module.performance.timer = setTimeout(module.performance.display, 100);
  624. },
  625. display: function() {
  626. var
  627. title = settings.name + ':',
  628. totalTime = 0
  629. ;
  630. time = false;
  631. clearTimeout(module.performance.timer);
  632. $.each(performance, function(index, data) {
  633. totalTime += data['Execution Time'];
  634. });
  635. title += ' ' + totalTime + 'ms';
  636. if(moduleSelector) {
  637. title += ' \'' + moduleSelector + '\'';
  638. }
  639. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  640. console.groupCollapsed(title);
  641. if(console.table) {
  642. console.table(performance);
  643. }
  644. else {
  645. $.each(performance, function(index, data) {
  646. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  647. });
  648. }
  649. console.groupEnd();
  650. }
  651. performance = [];
  652. }
  653. },
  654. invoke: function(query, passedArguments, context) {
  655. var
  656. maxDepth,
  657. found,
  658. response
  659. ;
  660. passedArguments = passedArguments || queryArguments;
  661. context = element || context;
  662. if(typeof query == 'string' && instance !== undefined) {
  663. query = query.split(/[\. ]/);
  664. maxDepth = query.length - 1;
  665. $.each(query, function(depth, value) {
  666. var camelCaseValue = (depth != maxDepth)
  667. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  668. : query
  669. ;
  670. if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
  671. instance = instance[camelCaseValue];
  672. }
  673. else if( instance[camelCaseValue] !== undefined ) {
  674. found = instance[camelCaseValue];
  675. return false;
  676. }
  677. else if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  678. instance = instance[value];
  679. }
  680. else if( instance[value] !== undefined ) {
  681. found = instance[value];
  682. return false;
  683. }
  684. else {
  685. module.error(error.method, query);
  686. return false;
  687. }
  688. });
  689. }
  690. if ( $.isFunction( found ) ) {
  691. response = found.apply(context, passedArguments);
  692. }
  693. else if(found !== undefined) {
  694. response = found;
  695. }
  696. if($.isArray(returnedValue)) {
  697. returnedValue.push(response);
  698. }
  699. else if(returnedValue !== undefined) {
  700. returnedValue = [returnedValue, response];
  701. }
  702. else if(response !== undefined) {
  703. returnedValue = response;
  704. }
  705. return found;
  706. }
  707. };
  708. if(methodInvoked) {
  709. if(instance === undefined) {
  710. module.initialize();
  711. }
  712. module.invoke(query);
  713. }
  714. else {
  715. if(instance !== undefined) {
  716. module.destroy();
  717. }
  718. module.initialize();
  719. }
  720. })
  721. ;
  722. return (returnedValue)
  723. ? returnedValue
  724. : this
  725. ;
  726. };
  727. $.fn.dropdown.settings = {
  728. name : 'Dropdown',
  729. namespace : 'dropdown',
  730. verbose : true,
  731. debug : true,
  732. performance : true,
  733. on : 'click',
  734. action : 'activate',
  735. delay: {
  736. show : 200,
  737. hide : 300,
  738. touch : 50
  739. },
  740. transition : 'slide down',
  741. duration : 250,
  742. onChange : function(value, text){},
  743. onShow : function(){},
  744. onHide : function(){},
  745. error : {
  746. action : 'You called a dropdown action that was not defined',
  747. method : 'The method you called is not defined.',
  748. transition : 'The requested transition was not found'
  749. },
  750. metadata: {
  751. text : 'text',
  752. value : 'value'
  753. },
  754. selector : {
  755. menu : '.menu',
  756. item : '.menu > .item',
  757. text : '> .text',
  758. input : '> input[type="hidden"]'
  759. },
  760. className : {
  761. active : 'active',
  762. placeholder : 'default',
  763. disabled : 'disabled',
  764. visible : 'visible',
  765. selection : 'selection'
  766. }
  767. };
  768. })( jQuery, window , document );