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.

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