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.

570 lines
16 KiB

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