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.

620 lines
17 KiB

  1. /* ******************************
  2. Semantic Module: Checkbox
  3. Author: Jack Lukic
  4. Notes: First Commit MAy 25, 2013
  5. Simple plug-in which maintains the state for ui dropdown
  6. ****************************** */
  7. ;(function ( $, window, document, undefined ) {
  8. $.fn.dropdown = function(parameters) {
  9. var
  10. $allModules = $(this),
  11. $document = $(document),
  12. settings = ( $.isPlainObject(parameters) )
  13. ? $.extend(true, {}, $.fn.dropdown.settings, parameters)
  14. : $.fn.dropdown.settings,
  15. eventNamespace = '.' + settings.namespace,
  16. moduleNamespace = 'module-' + settings.namespace,
  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. invokedResponse,
  24. allModules
  25. ;
  26. $allModules
  27. .each(function() {
  28. var
  29. $module = $(this),
  30. $menu = $(this).find(settings.selector.menu),
  31. $item = $(this).find(settings.selector.item),
  32. $text = $(this).find(settings.selector.text),
  33. $input = $(this).find(settings.selector.input),
  34. isTouchDevice = ('ontouchstart' in document.documentElement),
  35. selector = $module.selector || '',
  36. element = this,
  37. instance = $module.data('module-' + settings.namespace),
  38. className = settings.className,
  39. metadata = settings.metadata,
  40. namespace = settings.namespace,
  41. animation = settings.animation,
  42. errors = settings.errors,
  43. module
  44. ;
  45. module = {
  46. initialize: function() {
  47. module.verbose('Initializing dropdown with bound events', $module);
  48. if(isTouchDevice) {
  49. $module
  50. .on('touchstart' + eventNamespace, module.event.test.toggle)
  51. ;
  52. }
  53. else if(settings.on == 'click') {
  54. $module
  55. .on('click' + eventNamespace, module.event.test.toggle)
  56. ;
  57. }
  58. else if(settings.on == 'hover') {
  59. $module
  60. .on('mouseenter' + eventNamespace, module.delay.show)
  61. .on('mouseleave' + eventNamespace, module.delay.hide)
  62. ;
  63. }
  64. else {
  65. $module
  66. .on(settings.on + eventNamespace, module.toggle)
  67. ;
  68. }
  69. if(settings.action == 'form') {
  70. module.set.selected();
  71. }
  72. $item
  73. .on(module.get.selectEvent() + eventNamespace, module.event.item.click)
  74. ;
  75. $module
  76. .data(moduleNamespace, module)
  77. ;
  78. },
  79. destroy: function() {
  80. module.verbose('Destroying previous module for', $module);
  81. $module
  82. .off(namespace)
  83. ;
  84. },
  85. event: {
  86. stopPropagation: function(event) {
  87. event.stopPropagation();
  88. },
  89. test: {
  90. toggle: function(event) {
  91. module.intent.test(event, module.toggle);
  92. event.stopPropagation();
  93. },
  94. hide: function(event) {
  95. module.intent.test(event, module.hide);
  96. event.stopPropagation();
  97. }
  98. },
  99. item: {
  100. click: function (event) {
  101. var
  102. $choice = $(this),
  103. text = $choice.data(metadata.text) || $choice.text(),
  104. value = $choice.data(metadata.value) || text
  105. ;
  106. module.verbose('Adding active state to selected item');
  107. $item
  108. .removeClass(className.active)
  109. ;
  110. $choice
  111. .addClass(className.active)
  112. ;
  113. module.action.determine(text, value);
  114. $.proxy(settings.onChange, $module.get())(value, text);
  115. event.stopPropagation();
  116. }
  117. },
  118. resetStyle: function() {
  119. $(this).removeAttr('style');
  120. }
  121. },
  122. intent: {
  123. test: function(event, callback) {
  124. module.debug('Determining whether event occurred in dropdown', event.target);
  125. callback = callback || function(){};
  126. if( $(event.target).closest($menu).size() === 0 ) {
  127. module.verbose('Triggering event', callback);
  128. callback();
  129. }
  130. else {
  131. module.verbose('Event occurred in dropdown, canceling callback');
  132. }
  133. },
  134. bind: function() {
  135. module.verbose('Binding hide intent event to document');
  136. $document
  137. .on(module.get.selectEvent(), module.event.test.hide)
  138. ;
  139. },
  140. unbind: function() {
  141. module.verbose('Removing hide intent event from document');
  142. $document
  143. .off(module.get.selectEvent())
  144. ;
  145. }
  146. },
  147. action: {
  148. determine: function(text, value) {
  149. if( $.isFunction( module.action[settings.action] ) ) {
  150. module.verbose('Triggering preset action', settings.action);
  151. module.action[ settings.action ](text, value);
  152. }
  153. else if( $.isFunction(settings.action) ) {
  154. module.verbose('Triggering user action', settings.action);
  155. settings.action(text, value);
  156. }
  157. else {
  158. module.error(errors.action);
  159. }
  160. },
  161. none: function() {},
  162. hide: function() {
  163. module.hide();
  164. },
  165. changeText: function(text, value) {
  166. module.set.text(text);
  167. module.hide();
  168. },
  169. form: function(text, value) {
  170. module.set.text(text);
  171. module.set.value(value);
  172. module.hide();
  173. }
  174. },
  175. get: {
  176. selectEvent: function() {
  177. return (isTouchDevice)
  178. ? 'touchstart'
  179. : 'click'
  180. ;
  181. },
  182. text: function() {
  183. return $text.text();
  184. },
  185. value: function() {
  186. return $input.val();
  187. },
  188. item: function(value) {
  189. var
  190. $selectedItem
  191. ;
  192. value = value || $input.val();
  193. $item
  194. .each(function() {
  195. if( $(this).data(metadata.value) == value ) {
  196. $selectedItem = $(this);
  197. }
  198. })
  199. ;
  200. return $selectedItem || false;
  201. }
  202. },
  203. set: {
  204. text: function(text) {
  205. module.debug('Changing text', text, $text);
  206. $text.removeClass(className.placeholder);
  207. $text.text(text);
  208. },
  209. value: function(value) {
  210. module.debug('Adding selected value to hidden input', value, $input);
  211. $input.val(value);
  212. },
  213. selected: function(value) {
  214. var
  215. selectedValue = value || $input.val(),
  216. $selectedItem = module.get.item(value),
  217. selectedText
  218. ;
  219. if($selectedItem) {
  220. module.debug('Setting selected menu item to', $selectedItem);
  221. selectedText = $selectedItem.data(metadata.text) || $selectedItem.text();
  222. $item
  223. .removeClass(className.active)
  224. ;
  225. $selectedItem
  226. .addClass(className.active)
  227. ;
  228. module.set.text(selectedText);
  229. }
  230. }
  231. },
  232. is: {
  233. visible: function() {
  234. return $menu.is(':visible');
  235. },
  236. hidden: function() {
  237. return $menu.is(':not(:visible)');
  238. }
  239. },
  240. can: {
  241. click: function() {
  242. return (isTouchDevice || settings.on == 'click');
  243. },
  244. show: function() {
  245. return !$module.hasClass(className.disabled);
  246. }
  247. },
  248. animate: {
  249. show: function() {
  250. module.verbose('Doing menu showing animation');
  251. if(animation.show == 'none') {
  252. $menu
  253. .show()
  254. ;
  255. }
  256. else if(animation.show == 'fade') {
  257. $menu
  258. .hide()
  259. .clearQueue()
  260. .fadeIn(150, module.event.resetStyle)
  261. ;
  262. }
  263. else if(animation.show == 'slide') {
  264. $menu
  265. .hide()
  266. .clearQueue()
  267. .children()
  268. .clearQueue()
  269. .css('opacity', 0)
  270. .delay(50)
  271. .animate({
  272. opacity : 1
  273. }, 200, 'easeOutQuad', module.event.resetStyle)
  274. .end()
  275. .slideDown(100, 'easeOutQuad', module.event.resetStyle)
  276. ;
  277. }
  278. else {
  279. module.error(errors.animation);
  280. }
  281. },
  282. hide: function() {
  283. module.verbose('Doing menu hiding animation');
  284. if(animation.hide == 'none') {
  285. $menu
  286. .hide()
  287. ;
  288. }
  289. else if(animation.hide == 'fade') {
  290. $menu
  291. .show()
  292. .clearQueue()
  293. .fadeOut(150, module.event.resetStyle)
  294. ;
  295. }
  296. else if(animation.hide == 'slide') {
  297. $menu
  298. .show()
  299. .clearQueue()
  300. .children()
  301. .clearQueue()
  302. .css('opacity', 1)
  303. .animate({
  304. opacity : 0
  305. }, 100, 'easeOutQuad', module.event.resetStyle)
  306. .end()
  307. .delay(50)
  308. .slideUp(100, 'easeOutQuad', module.event.resetStyle)
  309. ;
  310. }
  311. else {
  312. module.error(errors.animation);
  313. }
  314. }
  315. },
  316. show: function() {
  317. module.debug('Checking if dropdown can show');
  318. if( !module.is.visible() ) {
  319. module.hideOthers();
  320. $module
  321. .addClass(className.visible)
  322. ;
  323. module.animate.show();
  324. if( module.can.click() ) {
  325. module.intent.bind();
  326. }
  327. $.proxy(settings.onShow, $module.get() )();
  328. }
  329. },
  330. hide: function() {
  331. if( !module.is.hidden() ) {
  332. module.debug('Hiding dropdown');
  333. $module
  334. .removeClass(className.visible)
  335. ;
  336. if( module.can.click() ) {
  337. module.intent.unbind();
  338. }
  339. module.animate.hide();
  340. $.proxy(settings.onHide, $module.get() )();
  341. }
  342. },
  343. delay: {
  344. show: function() {
  345. module.verbose('Delaying show event to ensure user intent');
  346. clearTimeout(module.graceTimer);
  347. module.graceTimer = setTimeout(module.show, settings.delay.show);
  348. },
  349. hide: function() {
  350. module.verbose('Delaying hide event to ensure user intent');
  351. clearTimeout(module.graceTimer);
  352. module.graceTimer = setTimeout(module.hide, settings.delay.hide);
  353. }
  354. },
  355. hideOthers: function() {
  356. module.verbose('Finding other dropdowns to hide');
  357. $allModules
  358. .not($module)
  359. .has(settings.selector.menu + ':visible')
  360. .dropdown('hide')
  361. ;
  362. },
  363. toggle: function() {
  364. module.verbose('Toggling menu visibility');
  365. if( module.is.hidden() ) {
  366. module.show();
  367. }
  368. else {
  369. module.hide();
  370. }
  371. },
  372. setting: function(name, value) {
  373. if(value !== undefined) {
  374. if( $.isPlainObject(name) ) {
  375. $.extend(true, settings, name);
  376. }
  377. else {
  378. settings[name] = value;
  379. }
  380. }
  381. else {
  382. return settings[name];
  383. }
  384. },
  385. internal: function(name, value) {
  386. if(value !== undefined) {
  387. if( $.isPlainObject(name) ) {
  388. $.extend(true, module, name);
  389. }
  390. else {
  391. module[name] = value;
  392. }
  393. }
  394. else {
  395. return module[name];
  396. }
  397. },
  398. debug: function() {
  399. if(settings.debug) {
  400. if(settings.performance) {
  401. module.performance.log(arguments);
  402. }
  403. else {
  404. module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  405. }
  406. }
  407. },
  408. verbose: function() {
  409. if(settings.verbose && settings.debug) {
  410. if(settings.performance) {
  411. module.performance.log(arguments);
  412. }
  413. else {
  414. module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  415. }
  416. }
  417. },
  418. error: function() {
  419. module.error = Function.prototype.bind.call(console.error, console, settings.moduleName + ':');
  420. },
  421. performance: {
  422. log: function(message) {
  423. var
  424. currentTime,
  425. executionTime,
  426. previousTime
  427. ;
  428. if(settings.performance) {
  429. currentTime = new Date().getTime();
  430. previousTime = time || currentTime,
  431. executionTime = currentTime - previousTime;
  432. time = currentTime;
  433. performance.push({
  434. 'Element' : element,
  435. 'Name' : message[0],
  436. 'Arguments' : message[1] || '',
  437. 'Execution Time' : executionTime
  438. });
  439. }
  440. clearTimeout(module.performance.timer);
  441. module.performance.timer = setTimeout(module.performance.display, 100);
  442. },
  443. display: function() {
  444. var
  445. title = settings.moduleName + ':',
  446. totalTime = 0
  447. ;
  448. time = false;
  449. $.each(performance, function(index, data) {
  450. totalTime += data['Execution Time'];
  451. });
  452. title += ' ' + totalTime + 'ms';
  453. if(moduleSelector) {
  454. title += ' \'' + moduleSelector + '\'';
  455. }
  456. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  457. console.groupCollapsed(title);
  458. if(console.table) {
  459. console.table(performance);
  460. }
  461. else {
  462. $.each(performance, function(index, data) {
  463. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  464. });
  465. }
  466. console.groupEnd();
  467. }
  468. performance = [];
  469. }
  470. },
  471. invoke: function(query, passedArguments, context) {
  472. var
  473. maxDepth,
  474. found
  475. ;
  476. passedArguments = passedArguments || queryArguments;
  477. context = element || context;
  478. if(typeof query == 'string' && instance !== undefined) {
  479. query = query.split('.');
  480. maxDepth = query.length - 1;
  481. $.each(query, function(depth, value) {
  482. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  483. instance = instance[value];
  484. }
  485. else if( instance[value] !== undefined ) {
  486. found = instance[value];
  487. }
  488. else {
  489. module.error(errors.method);
  490. }
  491. });
  492. }
  493. if ( $.isFunction( found ) ) {
  494. instance.verbose('Executing invoked function', found);
  495. return found.apply(context, passedArguments);
  496. }
  497. return found || false;
  498. }
  499. };
  500. if(methodInvoked) {
  501. if(instance === undefined) {
  502. module.initialize();
  503. }
  504. invokedResponse = module.invoke(query);
  505. }
  506. else {
  507. if(instance !== undefined) {
  508. module.destroy();
  509. }
  510. module.initialize();
  511. }
  512. })
  513. ;
  514. return (invokedResponse)
  515. ? invokedResponse
  516. : this
  517. ;
  518. };
  519. $.fn.dropdown.settings = {
  520. moduleName : 'Dropdown',
  521. namespace : 'dropdown',
  522. verbose : true,
  523. debug : true,
  524. performance : true,
  525. on : 'click',
  526. action : 'hide',
  527. delay: {
  528. show: 50,
  529. hide: 300
  530. },
  531. animation : {
  532. show: 'slide',
  533. hide: 'slide'
  534. },
  535. onChange : function(){},
  536. onShow : function(){},
  537. onHide : function(){},
  538. errors : {
  539. action : 'You called a dropdown action that was not defined',
  540. method : 'The method you called is not defined.',
  541. animation : 'The requested animation was not found'
  542. },
  543. metadata: {
  544. text : 'text',
  545. value : 'value'
  546. },
  547. selector : {
  548. menu : '.menu',
  549. item : '.menu > .item',
  550. text : '> .text',
  551. input : '> input[type="hidden"]'
  552. },
  553. className : {
  554. active : 'active',
  555. placeholder : 'default',
  556. disabled : 'disabled',
  557. visible : 'visible'
  558. }
  559. };
  560. })( jQuery, window , document );