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.

601 lines
17 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 = ( $.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.show)
  61. .on('mouseleave' + eventNamespace, module.delayedHide)
  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. },
  119. intent: {
  120. test: function(event, callback) {
  121. module.debug('Determining whether event occurred in dropdown', event.target);
  122. callback = callback || function(){};
  123. if( $(event.target).closest($menu).size() === 0 ) {
  124. module.verbose('Triggering event', callback);
  125. callback();
  126. }
  127. else {
  128. module.verbose('Event occurred in dropdown, canceling callback');
  129. }
  130. },
  131. bind: function() {
  132. module.verbose('Binding hide intent event to document');
  133. $document
  134. .on(module.get.selectEvent(), module.event.test.hide)
  135. ;
  136. },
  137. unbind: function() {
  138. module.verbose('Removing hide intent event from document');
  139. $document
  140. .off(module.get.selectEvent())
  141. ;
  142. }
  143. },
  144. action: {
  145. determine: function(text, value) {
  146. if( $.isFunction( module.action[settings.action] ) ) {
  147. module.verbose('Triggering preset action', settings.action);
  148. module.action[ settings.action ](text, value);
  149. }
  150. else if( $.isFunction(settings.action) ) {
  151. module.verbose('Triggering user action', settings.action);
  152. settings.action(text, value);
  153. }
  154. else {
  155. module.error(errors.action);
  156. }
  157. },
  158. nothing: function() {},
  159. hide: function() {
  160. module.hide();
  161. },
  162. changeText: function(text, value) {
  163. module.set.text(text);
  164. module.hide();
  165. },
  166. form: function(text, value) {
  167. module.set.text(text);
  168. module.set.value(value);
  169. module.hide();
  170. }
  171. },
  172. get: {
  173. selectEvent: function() {
  174. return (isTouchDevice)
  175. ? 'touchstart'
  176. : 'click'
  177. ;
  178. },
  179. text: function() {
  180. return $text.text();
  181. },
  182. value: function() {
  183. return $input.val();
  184. },
  185. item: function(value) {
  186. var
  187. $selectedItem
  188. ;
  189. value = value || $input.val();
  190. $item
  191. .each(function() {
  192. if( $(this).data(metadata.value) == value ) {
  193. $selectedItem = $(this);
  194. }
  195. })
  196. ;
  197. return $selectedItem || false;
  198. }
  199. },
  200. set: {
  201. text: function(text) {
  202. module.debug('Changing text', text, $text);
  203. $text.text(text);
  204. },
  205. value: function(value) {
  206. module.debug('Adding selected value to hidden input', value, $input);
  207. $input.val(value);
  208. },
  209. selected: function(value) {
  210. var
  211. selectedValue = value || $input.val(),
  212. $selectedItem = module.get.item(value),
  213. selectedText
  214. ;
  215. if($selectedItem) {
  216. module.debug('Setting selected menu item to', $selectedItem);
  217. selectedText = $selectedItem.data(metadata.text) || $selectedItem.text();
  218. $item
  219. .removeClass(className.active)
  220. ;
  221. $selectedItem
  222. .addClass(className.active)
  223. ;
  224. module.set.text(selectedText);
  225. }
  226. }
  227. },
  228. is: {
  229. visible: function() {
  230. return $menu.is(':visible');
  231. },
  232. hidden: function() {
  233. return $menu.is(':not(:visible)');
  234. }
  235. },
  236. can: {
  237. click: function() {
  238. return (isTouchDevice || settings.on == 'click');
  239. },
  240. show: function() {
  241. return !$module.hasClass(className.disabled);
  242. }
  243. },
  244. animate: {
  245. show: function() {
  246. module.verbose('Doing menu showing animation');
  247. if(animation.show == 'none') {
  248. $menu
  249. .show()
  250. ;
  251. }
  252. else if(animation.show == 'fade') {
  253. $menu
  254. .fadeIn(150)
  255. ;
  256. }
  257. else if(animation.show == 'slide') {
  258. $menu
  259. .clearQueue()
  260. .children()
  261. .clearQueue()
  262. .css('opacity', 0)
  263. .delay(50)
  264. .animate({
  265. opacity : 1
  266. }, 200, 'easeOutQuad')
  267. .end()
  268. .slideDown(100, 'easeOutQuad')
  269. ;
  270. }
  271. else {
  272. module.error(errors.animation);
  273. }
  274. },
  275. hide: function() {
  276. module.verbose('Doing menu hiding animation');
  277. if(animation.hide == 'none') {
  278. $menu
  279. .hide()
  280. ;
  281. }
  282. else if(animation.hide == 'fade') {
  283. $menu
  284. .fadeOut(150)
  285. ;
  286. }
  287. else if(animation.hide == 'slide') {
  288. $menu
  289. .clearQueue()
  290. .children()
  291. .clearQueue()
  292. .css('opacity', 1)
  293. .animate({
  294. opacity : 0
  295. }, 100, 'easeOutQuad')
  296. .end()
  297. .delay(50)
  298. .slideUp(100, 'easeOutQuad')
  299. ;
  300. }
  301. else {
  302. module.error(errors.animation);
  303. }
  304. }
  305. },
  306. show: function() {
  307. module.debug('Checking if dropdown can show');
  308. clearTimeout(module.graceTimer);
  309. if( !module.is.visible() ) {
  310. module.hideOthers();
  311. $module
  312. .addClass(className.visible)
  313. ;
  314. module.animate.show();
  315. if( module.can.click() ) {
  316. module.intent.bind();
  317. }
  318. $.proxy(settings.onShow, $module.get() )();
  319. }
  320. },
  321. hide: function() {
  322. if( !module.is.hidden() ) {
  323. module.debug('Hiding dropdown');
  324. $module
  325. .removeClass(className.visible)
  326. ;
  327. if( module.can.click() ) {
  328. module.intent.unbind();
  329. }
  330. module.animate.hide();
  331. $.proxy(settings.onHide, $module.get() )();
  332. }
  333. },
  334. delayedHide: function() {
  335. module.verbose('User moused away setting timer to hide dropdown');
  336. module.graceTimer = setTimeout(module.hide, settings.gracePeriod);
  337. },
  338. hideOthers: function() {
  339. module.verbose('Finding other dropdowns to hide');
  340. $allModules
  341. .not($module)
  342. .has(settings.selector.menu + ':visible')
  343. .dropdown('hide')
  344. ;
  345. },
  346. toggle: function() {
  347. module.verbose('Toggling menu visibility');
  348. if( module.is.hidden() ) {
  349. module.show();
  350. }
  351. else {
  352. module.hide();
  353. }
  354. },
  355. setting: function(name, value) {
  356. module.debug('Changing setting', name, value);
  357. if(value !== undefined) {
  358. if( $.isPlainObject(name) ) {
  359. $.extend(true, settings, name);
  360. }
  361. else {
  362. settings[name] = value;
  363. }
  364. }
  365. else {
  366. return settings[name];
  367. }
  368. },
  369. internal: function(name, value) {
  370. module.debug('Changing internal', name, value);
  371. if(value !== undefined) {
  372. if( $.isPlainObject(name) ) {
  373. $.extend(true, module, name);
  374. }
  375. else {
  376. module[name] = value;
  377. }
  378. }
  379. else {
  380. return module[name];
  381. }
  382. },
  383. debug: function() {
  384. if(settings.debug) {
  385. if(settings.performance) {
  386. module.performance.log(arguments);
  387. }
  388. else {
  389. module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  390. }
  391. }
  392. },
  393. verbose: function() {
  394. if(settings.verbose && settings.debug) {
  395. if(settings.performance) {
  396. module.performance.log(arguments);
  397. }
  398. else {
  399. module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  400. }
  401. }
  402. },
  403. error: function() {
  404. module.error = Function.prototype.bind.call(console.log, console, settings.moduleName + ':');
  405. },
  406. performance: {
  407. log: function(message) {
  408. var
  409. currentTime,
  410. executionTime,
  411. previousTime
  412. ;
  413. if(settings.performance) {
  414. currentTime = new Date().getTime();
  415. previousTime = time || currentTime,
  416. executionTime = currentTime - previousTime;
  417. time = currentTime;
  418. performance.push({
  419. 'Element' : element,
  420. 'Name' : message[0],
  421. 'Arguments' : message[1] || '',
  422. 'Execution Time' : executionTime
  423. });
  424. }
  425. clearTimeout(module.performance.timer);
  426. module.performance.timer = setTimeout(module.performance.display, 100);
  427. },
  428. display: function() {
  429. var
  430. title = settings.moduleName + ':',
  431. totalTime = 0
  432. ;
  433. time = false;
  434. $.each(performance, function(index, data) {
  435. totalTime += data['Execution Time'];
  436. });
  437. title += ' ' + totalTime + 'ms';
  438. if(moduleSelector) {
  439. title += ' \'' + moduleSelector + '\'';
  440. }
  441. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  442. console.groupCollapsed(title);
  443. if(console.table) {
  444. console.table(performance);
  445. }
  446. else {
  447. $.each(performance, function(index, data) {
  448. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  449. });
  450. }
  451. console.groupEnd();
  452. }
  453. performance = [];
  454. }
  455. },
  456. invoke: function(query, passedArguments, context) {
  457. var
  458. maxDepth,
  459. found
  460. ;
  461. passedArguments = passedArguments || queryArguments;
  462. context = element || context;
  463. if(typeof query == 'string' && instance !== undefined) {
  464. query = query.split('.');
  465. maxDepth = query.length - 1;
  466. $.each(query, function(depth, value) {
  467. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  468. instance = instance[value];
  469. return true;
  470. }
  471. else if( instance[value] !== undefined ) {
  472. found = instance[value];
  473. return true;
  474. }
  475. module.error(errors.method);
  476. return false;
  477. });
  478. }
  479. if ( $.isFunction( found ) ) {
  480. instance.verbose('Executing invoked function', found);
  481. return found.apply(context, passedArguments);
  482. }
  483. return found || false;
  484. }
  485. };
  486. if(methodInvoked) {
  487. if(instance === undefined) {
  488. module.initialize();
  489. }
  490. invokedResponse = module.invoke(query);
  491. }
  492. else {
  493. if(instance !== undefined) {
  494. module.destroy();
  495. }
  496. module.initialize();
  497. }
  498. })
  499. ;
  500. return (invokedResponse)
  501. ? invokedResponse
  502. : this
  503. ;
  504. };
  505. $.fn.dropdown.settings = {
  506. moduleName : 'Dropdown',
  507. namespace : 'dropdown',
  508. verbose : true,
  509. debug : true,
  510. performance : true,
  511. on : 'click',
  512. gracePeriod : 300,
  513. action : 'hide',
  514. animation : {
  515. show: 'slide',
  516. hide: 'slide'
  517. },
  518. onChange : function(){},
  519. onShow : function(){},
  520. onHide : function(){},
  521. errors : {
  522. action : 'You called a dropdown action that was not defined',
  523. method : 'The method you called is not defined.',
  524. animation : 'The requested animation was not found'
  525. },
  526. metadata: {
  527. text : 'text',
  528. value : 'value'
  529. },
  530. selector : {
  531. menu : '.menu',
  532. item : '.menu > .item',
  533. text : '> .text',
  534. input : '> input[type="hidden"]'
  535. },
  536. className : {
  537. active : 'active',
  538. disabled : 'disabled',
  539. visible : 'visible'
  540. }
  541. };
  542. })( jQuery, window , document );