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.

806 lines
24 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
  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. // no use detecting mouse events because touch devices emulate them
  52. if(hasTouch) {
  53. module.bind.touchEvents();
  54. }
  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, 50);
  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. event.stopPropagation();
  157. }
  158. },
  159. item: {
  160. mouseenter: function(event) {
  161. var
  162. $currentMenu = $(this).find(selector.menu),
  163. $otherMenus = $(this).siblings(selector.item).children(selector.menu)
  164. ;
  165. if( $currentMenu.size() > 0 ) {
  166. clearTimeout(module.itemTimer);
  167. module.itemTimer = setTimeout(function() {
  168. module.animate.hide(false, $otherMenus);
  169. module.verbose('Showing sub-menu', $currentMenu);
  170. module.animate.show(false, $currentMenu);
  171. }, settings.delay.show * 2);
  172. event.preventDefault();
  173. }
  174. },
  175. mouseleave: function(event) {
  176. var
  177. $currentMenu = $(this).find(selector.menu)
  178. ;
  179. if($currentMenu.size() > 0) {
  180. clearTimeout(module.itemTimer);
  181. module.itemTimer = setTimeout(function() {
  182. module.verbose('Hiding sub-menu', $currentMenu);
  183. module.animate.hide(false, $currentMenu);
  184. }, settings.delay.hide);
  185. }
  186. },
  187. click: function (event) {
  188. var
  189. $choice = $(this),
  190. text = $choice.data(metadata.text) || $choice.text(),
  191. value = $choice.data(metadata.value) || text.toLowerCase()
  192. ;
  193. if( $choice.find(selector.menu).size() === 0 ) {
  194. module.determine.selectAction(text, value);
  195. $.proxy(settings.onChange, element)(value, text);
  196. }
  197. }
  198. },
  199. resetStyle: function() {
  200. $(this).removeAttr('style');
  201. }
  202. },
  203. determine: {
  204. selectAction: function(text, value) {
  205. module.verbose('Determining action', settings.action);
  206. if( $.isFunction( module.action[settings.action] ) ) {
  207. module.verbose('Triggering preset action', settings.action, text, value);
  208. module.action[ settings.action ](text, value);
  209. }
  210. else if( $.isFunction(settings.action) ) {
  211. module.verbose('Triggering user action', settings.action, text, value);
  212. settings.action(text, value);
  213. }
  214. else {
  215. module.error(error.action, settings.action);
  216. }
  217. },
  218. intent: function(event, callback) {
  219. module.debug('Determining whether event occurred in dropdown', event.target);
  220. callback = callback || function(){};
  221. if( $(event.target).closest($menu).size() === 0 ) {
  222. module.verbose('Triggering event', callback);
  223. callback();
  224. return true;
  225. }
  226. else {
  227. module.verbose('Event occurred in dropdown, canceling callback');
  228. return false;
  229. }
  230. }
  231. },
  232. action: {
  233. nothing: function() {},
  234. hide: function() {
  235. module.hide();
  236. },
  237. activate: function(text, value) {
  238. value = value || text;
  239. module.set.selected(value);
  240. module.set.value(value);
  241. module.hide();
  242. },
  243. /* Deprecated */
  244. auto: function(text, value) {
  245. value = value || text;
  246. module.set.selected(value);
  247. module.set.value(value);
  248. module.hide();
  249. },
  250. /* Deprecated */
  251. changeText: function(text, value) {
  252. value = value || text;
  253. module.set.selected(value);
  254. module.hide();
  255. },
  256. /* Deprecated */
  257. updateForm: function(text, value) {
  258. value = value || text;
  259. module.set.selected(value);
  260. module.set.value(value);
  261. module.hide();
  262. }
  263. },
  264. get: {
  265. text: function() {
  266. return $text.text();
  267. },
  268. value: function() {
  269. return $input.val();
  270. },
  271. item: function(value) {
  272. var
  273. $selectedItem
  274. ;
  275. value = value || module.get.value() || module.get.text();
  276. if(value) {
  277. $item
  278. .each(function() {
  279. var
  280. $choice = $(this),
  281. optionText = $choice.data(metadata.text) || $choice.text(),
  282. optionValue = $choice.data(metadata.value) || optionText.toLowerCase()
  283. ;
  284. if( optionValue == value || optionText == value ) {
  285. $selectedItem = $(this);
  286. return false;
  287. }
  288. })
  289. ;
  290. }
  291. else {
  292. value = module.get.text();
  293. }
  294. return $selectedItem || false;
  295. }
  296. },
  297. set: {
  298. text: function(text) {
  299. module.debug('Changing text', text, $text);
  300. $text.removeClass(className.placeholder);
  301. $text.text(text);
  302. },
  303. value: function(value) {
  304. module.debug('Adding selected value to hidden input', value, $input);
  305. $input.val(value);
  306. },
  307. active: function() {
  308. $module.addClass(className.active);
  309. },
  310. visible: function() {
  311. $module.addClass(className.visible);
  312. },
  313. selected: function(value) {
  314. var
  315. $selectedItem = module.get.item(value),
  316. selectedText
  317. ;
  318. if($selectedItem) {
  319. module.debug('Setting selected menu item to', $selectedItem);
  320. selectedText = $selectedItem.data(metadata.text) || $selectedItem.text();
  321. $item
  322. .removeClass(className.active)
  323. ;
  324. $selectedItem
  325. .addClass(className.active)
  326. ;
  327. module.set.text(selectedText);
  328. }
  329. }
  330. },
  331. remove: {
  332. active: function() {
  333. $module.removeClass(className.active);
  334. },
  335. visible: function() {
  336. $module.removeClass(className.visible);
  337. }
  338. },
  339. is: {
  340. selection: function() {
  341. return $module.hasClass(className.selection);
  342. },
  343. animated: function($subMenu) {
  344. return ($subMenu)
  345. ? $subMenu.is(':animated') || $subMenu.transition('is animating')
  346. : $menu.is(':animated') || $menu.transition('is animating')
  347. ;
  348. },
  349. visible: function($subMenu) {
  350. return ($subMenu)
  351. ? $subMenu.is(':visible')
  352. : $menu.is(':visible')
  353. ;
  354. },
  355. hidden: function($subMenu) {
  356. return ($subMenu)
  357. ? $subMenu.is(':not(:visible)')
  358. : $menu.is(':not(:visible)')
  359. ;
  360. }
  361. },
  362. can: {
  363. click: function() {
  364. return (hasTouch || settings.on == 'click');
  365. },
  366. show: function() {
  367. return !$module.hasClass(className.disabled);
  368. }
  369. },
  370. animate: {
  371. show: function(callback, $subMenu) {
  372. var
  373. $currentMenu = $subMenu || $menu
  374. ;
  375. callback = callback || function(){};
  376. if( module.is.hidden($currentMenu) ) {
  377. module.verbose('Doing menu show animation', $currentMenu);
  378. if(settings.transition == 'none') {
  379. callback();
  380. }
  381. else if($.fn.transition !== undefined) {
  382. $currentMenu.transition({
  383. animation : settings.transition + ' in',
  384. duration : settings.duration,
  385. complete : callback,
  386. queue : false
  387. });
  388. }
  389. else if(settings.transition == 'slide down') {
  390. $currentMenu
  391. .hide()
  392. .clearQueue()
  393. .children()
  394. .clearQueue()
  395. .css('opacity', 0)
  396. .delay(50)
  397. .animate({
  398. opacity : 1
  399. }, settings.duration, 'easeOutQuad', module.event.resetStyle)
  400. .end()
  401. .slideDown(100, 'easeOutQuad', function() {
  402. $.proxy(module.event.resetStyle, this)();
  403. callback();
  404. })
  405. ;
  406. }
  407. else if(settings.transition == 'fade') {
  408. $currentMenu
  409. .hide()
  410. .clearQueue()
  411. .fadeIn(settings.duration, function() {
  412. $.proxy(module.event.resetStyle, this)();
  413. callback();
  414. })
  415. ;
  416. }
  417. else {
  418. module.error(error.transition, settings.transition);
  419. }
  420. }
  421. },
  422. hide: function(callback, $subMenu) {
  423. var
  424. $currentMenu = $subMenu || $menu
  425. ;
  426. callback = callback || function(){};
  427. if(module.is.visible($currentMenu) ) {
  428. module.verbose('Doing menu hide animation', $currentMenu);
  429. if($.fn.transition !== undefined) {
  430. $currentMenu.transition({
  431. animation : settings.transition + ' out',
  432. duration : settings.duration,
  433. complete : callback,
  434. queue : false
  435. });
  436. }
  437. else if(settings.transition == 'none') {
  438. callback();
  439. }
  440. else if(settings.transition == 'slide down') {
  441. $currentMenu
  442. .show()
  443. .clearQueue()
  444. .children()
  445. .clearQueue()
  446. .css('opacity', 1)
  447. .animate({
  448. opacity : 0
  449. }, 100, 'easeOutQuad', module.event.resetStyle)
  450. .end()
  451. .delay(50)
  452. .slideUp(100, 'easeOutQuad', function() {
  453. $.proxy(module.event.resetStyle, this)();
  454. callback();
  455. })
  456. ;
  457. }
  458. else if(settings.transition == 'fade') {
  459. $currentMenu
  460. .show()
  461. .clearQueue()
  462. .fadeOut(150, function() {
  463. $.proxy(module.event.resetStyle, this)();
  464. callback();
  465. })
  466. ;
  467. }
  468. else {
  469. module.error(error.transition);
  470. }
  471. }
  472. }
  473. },
  474. show: function() {
  475. module.debug('Checking if dropdown can show');
  476. if( module.is.hidden() ) {
  477. module.hideOthers();
  478. module.set.active();
  479. module.animate.show(function() {
  480. if( module.can.click() ) {
  481. module.bind.intent();
  482. }
  483. module.set.visible();
  484. });
  485. $.proxy(settings.onShow, element)();
  486. }
  487. },
  488. hide: function() {
  489. if( !module.is.animated() && module.is.visible() ) {
  490. module.debug('Hiding dropdown');
  491. if( module.can.click() ) {
  492. module.unbind.intent();
  493. }
  494. module.remove.active();
  495. module.animate.hide(module.remove.visible);
  496. $.proxy(settings.onHide, element)();
  497. }
  498. },
  499. delay: {
  500. show: function() {
  501. module.verbose('Delaying show event to ensure user intent');
  502. clearTimeout(module.timer);
  503. module.timer = setTimeout(module.show, settings.delay.show);
  504. },
  505. hide: function() {
  506. module.verbose('Delaying hide event to ensure user intent');
  507. clearTimeout(module.timer);
  508. module.timer = setTimeout(module.hide, settings.delay.hide);
  509. }
  510. },
  511. hideOthers: function() {
  512. module.verbose('Finding other dropdowns to hide');
  513. $allModules
  514. .not($module)
  515. .has(selector.menu + ':visible')
  516. .dropdown('hide')
  517. ;
  518. },
  519. toggle: function() {
  520. module.verbose('Toggling menu visibility');
  521. if( module.is.hidden() ) {
  522. module.show();
  523. }
  524. else {
  525. module.hide();
  526. }
  527. },
  528. setting: function(name, value) {
  529. if(value !== undefined) {
  530. if( $.isPlainObject(name) ) {
  531. $.extend(true, settings, name);
  532. }
  533. else {
  534. settings[name] = value;
  535. }
  536. }
  537. else {
  538. return settings[name];
  539. }
  540. },
  541. internal: function(name, value) {
  542. if(value !== undefined) {
  543. if( $.isPlainObject(name) ) {
  544. $.extend(true, module, name);
  545. }
  546. else {
  547. module[name] = value;
  548. }
  549. }
  550. else {
  551. return module[name];
  552. }
  553. },
  554. debug: function() {
  555. if(settings.debug) {
  556. if(settings.performance) {
  557. module.performance.log(arguments);
  558. }
  559. else {
  560. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  561. module.debug.apply(console, arguments);
  562. }
  563. }
  564. },
  565. verbose: function() {
  566. if(settings.verbose && settings.debug) {
  567. if(settings.performance) {
  568. module.performance.log(arguments);
  569. }
  570. else {
  571. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  572. module.verbose.apply(console, arguments);
  573. }
  574. }
  575. },
  576. error: function() {
  577. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  578. module.error.apply(console, arguments);
  579. },
  580. performance: {
  581. log: function(message) {
  582. var
  583. currentTime,
  584. executionTime,
  585. previousTime
  586. ;
  587. if(settings.performance) {
  588. currentTime = new Date().getTime();
  589. previousTime = time || currentTime;
  590. executionTime = currentTime - previousTime;
  591. time = currentTime;
  592. performance.push({
  593. 'Element' : element,
  594. 'Name' : message[0],
  595. 'Arguments' : [].slice.call(message, 1) || '',
  596. 'Execution Time' : executionTime
  597. });
  598. }
  599. clearTimeout(module.performance.timer);
  600. module.performance.timer = setTimeout(module.performance.display, 100);
  601. },
  602. display: function() {
  603. var
  604. title = settings.name + ':',
  605. totalTime = 0
  606. ;
  607. time = false;
  608. clearTimeout(module.performance.timer);
  609. $.each(performance, function(index, data) {
  610. totalTime += data['Execution Time'];
  611. });
  612. title += ' ' + totalTime + 'ms';
  613. if(moduleSelector) {
  614. title += ' \'' + moduleSelector + '\'';
  615. }
  616. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  617. console.groupCollapsed(title);
  618. if(console.table) {
  619. console.table(performance);
  620. }
  621. else {
  622. $.each(performance, function(index, data) {
  623. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  624. });
  625. }
  626. console.groupEnd();
  627. }
  628. performance = [];
  629. }
  630. },
  631. invoke: function(query, passedArguments, context) {
  632. var
  633. maxDepth,
  634. found,
  635. response
  636. ;
  637. passedArguments = passedArguments || queryArguments;
  638. context = element || context;
  639. if(typeof query == 'string' && instance !== undefined) {
  640. query = query.split(/[\. ]/);
  641. maxDepth = query.length - 1;
  642. $.each(query, function(depth, value) {
  643. var camelCaseValue = (depth != maxDepth)
  644. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  645. : query
  646. ;
  647. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  648. instance = instance[value];
  649. }
  650. else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
  651. instance = instance[camelCaseValue];
  652. }
  653. else if( instance[value] !== undefined ) {
  654. found = instance[value];
  655. return false;
  656. }
  657. else if( instance[camelCaseValue] !== undefined ) {
  658. found = instance[camelCaseValue];
  659. return false;
  660. }
  661. else {
  662. module.error(error.method);
  663. return false;
  664. }
  665. });
  666. }
  667. if ( $.isFunction( found ) ) {
  668. response = found.apply(context, passedArguments);
  669. }
  670. else if(found !== undefined) {
  671. response = found;
  672. }
  673. if($.isArray(returnedValue)) {
  674. returnedValue.push(response);
  675. }
  676. else if(returnedValue !== undefined) {
  677. returnedValue = [returnedValue, response];
  678. }
  679. else if(response !== undefined) {
  680. returnedValue = response;
  681. }
  682. return found;
  683. }
  684. };
  685. if(methodInvoked) {
  686. if(instance === undefined) {
  687. module.initialize();
  688. }
  689. module.invoke(query);
  690. }
  691. else {
  692. if(instance !== undefined) {
  693. module.destroy();
  694. }
  695. module.initialize();
  696. }
  697. })
  698. ;
  699. return (returnedValue)
  700. ? returnedValue
  701. : this
  702. ;
  703. };
  704. $.fn.dropdown.settings = {
  705. name : 'Dropdown',
  706. namespace : 'dropdown',
  707. verbose : true,
  708. debug : true,
  709. performance : true,
  710. on : 'click',
  711. action : 'activate',
  712. delay: {
  713. show: 200,
  714. hide: 300
  715. },
  716. transition : 'slide down',
  717. duration : 250,
  718. onChange : function(){},
  719. onShow : function(){},
  720. onHide : function(){},
  721. error : {
  722. action : 'You called a dropdown action that was not defined',
  723. method : 'The method you called is not defined.',
  724. transition : 'The requested transition was not found'
  725. },
  726. metadata: {
  727. text : 'text',
  728. value : 'value'
  729. },
  730. selector : {
  731. menu : '.menu',
  732. item : '.menu > .item',
  733. text : '> .text',
  734. input : '> input[type="hidden"]'
  735. },
  736. className : {
  737. active : 'active',
  738. placeholder : 'default',
  739. disabled : 'disabled',
  740. visible : 'visible',
  741. selection : 'selection'
  742. }
  743. };
  744. })( jQuery, window , document );