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.

505 lines
14 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*
  2. * # Semantic - Checkbox
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2014 Contributor
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. "use strict";
  13. $.fn.checkbox = function(parameters) {
  14. var
  15. $allModules = $(this),
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. query = arguments[0],
  20. methodInvoked = (typeof query == 'string'),
  21. queryArguments = [].slice.call(arguments, 1),
  22. returnedValue
  23. ;
  24. $allModules
  25. .each(function() {
  26. var
  27. settings = $.extend(true, {}, $.fn.checkbox.settings, parameters),
  28. className = settings.className,
  29. namespace = settings.namespace,
  30. selector = settings.selector,
  31. error = settings.error,
  32. eventNamespace = '.' + namespace,
  33. moduleNamespace = 'module-' + namespace,
  34. $module = $(this),
  35. $label = $(this).find(selector.label).first(),
  36. $input = $(this).find(selector.input),
  37. instance = $module.data(moduleNamespace),
  38. observer,
  39. element = this,
  40. module
  41. ;
  42. module = {
  43. initialize: function() {
  44. module.verbose('Initializing checkbox', settings);
  45. module.create.label();
  46. module.add.events();
  47. if( module.is.checked() ) {
  48. module.set.checked();
  49. if(settings.fireOnInit) {
  50. $.proxy(settings.onChecked, $input.get())();
  51. }
  52. }
  53. else {
  54. module.remove.checked();
  55. if(settings.fireOnInit) {
  56. $.proxy(settings.onUnchecked, $input.get())();
  57. }
  58. }
  59. module.observeChanges();
  60. module.instantiate();
  61. },
  62. instantiate: function() {
  63. module.verbose('Storing instance of module', module);
  64. instance = module;
  65. $module
  66. .data(moduleNamespace, module)
  67. ;
  68. },
  69. destroy: function() {
  70. module.verbose('Destroying module');
  71. module.remove.events();
  72. $module
  73. .removeData(moduleNamespace)
  74. ;
  75. },
  76. refresh: function() {
  77. $module = $(this);
  78. $label = $(this).find(selector.label).first();
  79. $input = $(this).find(selector.input);
  80. },
  81. observeChanges: function() {
  82. if('MutationObserver' in window) {
  83. observer = new MutationObserver(function(mutations) {
  84. module.debug('DOM tree modified, updating selector cache');
  85. module.refresh();
  86. });
  87. observer.observe(element, {
  88. childList : true,
  89. subtree : true
  90. });
  91. module.debug('Setting up mutation observer', observer);
  92. }
  93. },
  94. attachEvents: function(selector, event) {
  95. var
  96. $element = $(selector)
  97. ;
  98. event = $.isFunction(module[event])
  99. ? module[event]
  100. : module.toggle
  101. ;
  102. if($element.size() > 0) {
  103. module.debug('Attaching checkbox events to element', selector, event);
  104. $element
  105. .on('click' + eventNamespace, event)
  106. ;
  107. }
  108. else {
  109. module.error(error.notFound);
  110. }
  111. },
  112. event: {
  113. keydown: function(event) {
  114. var
  115. key = event.which,
  116. keyCode = {
  117. enter : 13,
  118. escape : 27
  119. }
  120. ;
  121. if( key == keyCode.escape) {
  122. module.verbose('Escape key pressed blurring field');
  123. $module
  124. .blur()
  125. ;
  126. }
  127. if(!event.ctrlKey && key == keyCode.enter) {
  128. module.verbose('Enter key pressed, toggling checkbox');
  129. $.proxy(module.toggle, this)();
  130. event.preventDefault();
  131. }
  132. }
  133. },
  134. is: {
  135. radio: function() {
  136. return $module.hasClass(className.radio);
  137. },
  138. checked: function() {
  139. return $input.prop('checked') !== undefined && $input.prop('checked');
  140. },
  141. unchecked: function() {
  142. return !module.is.checked();
  143. }
  144. },
  145. can: {
  146. change: function() {
  147. return !( $module.hasClass(className.disabled) || $module.hasClass(className.readOnly) || $input.prop('disabled') );
  148. },
  149. uncheck: function() {
  150. return (typeof settings.uncheckable === 'boolean')
  151. ? settings.uncheckable
  152. : !module.is.radio()
  153. ;
  154. }
  155. },
  156. set: {
  157. checked: function() {
  158. $module.addClass(className.checked);
  159. },
  160. tab: function() {
  161. if( $input.attr('tabindex') === undefined) {
  162. $input
  163. .attr('tabindex', 0)
  164. ;
  165. }
  166. }
  167. },
  168. create: {
  169. label: function() {
  170. if($input.prevAll(selector.label).size() > 0) {
  171. $input.prev(selector.label).detach().insertAfter($input);
  172. module.debug('Moving existing label', $label);
  173. }
  174. else if( !module.has.label() ) {
  175. $label = $('<label>').insertAfter($input);
  176. module.debug('Creating label', $label);
  177. }
  178. }
  179. },
  180. has: {
  181. label: function() {
  182. return ($label.size() > 0);
  183. }
  184. },
  185. add: {
  186. events: function() {
  187. module.verbose('Attaching checkbox events');
  188. $module
  189. .on('click' + eventNamespace, module.toggle)
  190. .on('keydown' + eventNamespace, selector.input, module.event.keydown)
  191. ;
  192. }
  193. },
  194. remove: {
  195. checked: function() {
  196. $module.removeClass(className.checked);
  197. },
  198. events: function() {
  199. module.debug('Removing events');
  200. $module
  201. .off(eventNamespace)
  202. .removeData(moduleNamespace)
  203. ;
  204. $input
  205. .off(eventNamespace, module.event.keydown)
  206. ;
  207. $label
  208. .off(eventNamespace)
  209. ;
  210. }
  211. },
  212. enable: function() {
  213. module.debug('Enabling checkbox functionality');
  214. $module.removeClass(className.disabled);
  215. $input.prop('disabled', false);
  216. $.proxy(settings.onEnabled, $input.get())();
  217. },
  218. disable: function() {
  219. module.debug('Disabling checkbox functionality');
  220. $module.addClass(className.disabled);
  221. $input.prop('disabled', 'disabled');
  222. $.proxy(settings.onDisabled, $input.get())();
  223. },
  224. check: function() {
  225. module.debug('Enabling checkbox', $input);
  226. $input
  227. .prop('checked', true)
  228. .trigger('change')
  229. ;
  230. module.set.checked();
  231. $.proxy(settings.onChange, $input.get())();
  232. $.proxy(settings.onChecked, $input.get())();
  233. },
  234. uncheck: function() {
  235. module.debug('Disabling checkbox');
  236. $input
  237. .prop('checked', false)
  238. .trigger('change')
  239. ;
  240. module.remove.checked();
  241. $.proxy(settings.onChange, $input.get())();
  242. $.proxy(settings.onUnchecked, $input.get())();
  243. },
  244. toggle: function(event) {
  245. if( !module.can.change() ) {
  246. console.log(module.can.change());
  247. module.debug('Checkbox is read-only or disabled, ignoring toggle');
  248. return;
  249. }
  250. module.verbose('Determining new checkbox state');
  251. if( module.is.unchecked() ) {
  252. module.check();
  253. }
  254. else if( module.is.checked() && module.can.uncheck() ) {
  255. module.uncheck();
  256. }
  257. },
  258. setting: function(name, value) {
  259. module.debug('Changing setting', name, value);
  260. if( $.isPlainObject(name) ) {
  261. $.extend(true, settings, name);
  262. }
  263. else if(value !== undefined) {
  264. settings[name] = value;
  265. }
  266. else {
  267. return settings[name];
  268. }
  269. },
  270. internal: function(name, value) {
  271. if( $.isPlainObject(name) ) {
  272. $.extend(true, module, name);
  273. }
  274. else if(value !== undefined) {
  275. module[name] = value;
  276. }
  277. else {
  278. return module[name];
  279. }
  280. },
  281. debug: function() {
  282. if(settings.debug) {
  283. if(settings.performance) {
  284. module.performance.log(arguments);
  285. }
  286. else {
  287. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  288. module.debug.apply(console, arguments);
  289. }
  290. }
  291. },
  292. verbose: function() {
  293. if(settings.verbose && settings.debug) {
  294. if(settings.performance) {
  295. module.performance.log(arguments);
  296. }
  297. else {
  298. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  299. module.verbose.apply(console, arguments);
  300. }
  301. }
  302. },
  303. error: function() {
  304. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  305. module.error.apply(console, arguments);
  306. },
  307. performance: {
  308. log: function(message) {
  309. var
  310. currentTime,
  311. executionTime,
  312. previousTime
  313. ;
  314. if(settings.performance) {
  315. currentTime = new Date().getTime();
  316. previousTime = time || currentTime;
  317. executionTime = currentTime - previousTime;
  318. time = currentTime;
  319. performance.push({
  320. 'Name' : message[0],
  321. 'Arguments' : [].slice.call(message, 1) || '',
  322. 'Element' : element,
  323. 'Execution Time' : executionTime
  324. });
  325. }
  326. clearTimeout(module.performance.timer);
  327. module.performance.timer = setTimeout(module.performance.display, 100);
  328. },
  329. display: function() {
  330. var
  331. title = settings.name + ':',
  332. totalTime = 0
  333. ;
  334. time = false;
  335. clearTimeout(module.performance.timer);
  336. $.each(performance, function(index, data) {
  337. totalTime += data['Execution Time'];
  338. });
  339. title += ' ' + totalTime + 'ms';
  340. if(moduleSelector) {
  341. title += ' \'' + moduleSelector + '\'';
  342. }
  343. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  344. console.groupCollapsed(title);
  345. if(console.table) {
  346. console.table(performance);
  347. }
  348. else {
  349. $.each(performance, function(index, data) {
  350. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  351. });
  352. }
  353. console.groupEnd();
  354. }
  355. performance = [];
  356. }
  357. },
  358. invoke: function(query, passedArguments, context) {
  359. var
  360. object = instance,
  361. maxDepth,
  362. found,
  363. response
  364. ;
  365. passedArguments = passedArguments || queryArguments;
  366. context = element || context;
  367. if(typeof query == 'string' && object !== undefined) {
  368. query = query.split(/[\. ]/);
  369. maxDepth = query.length - 1;
  370. $.each(query, function(depth, value) {
  371. var camelCaseValue = (depth != maxDepth)
  372. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  373. : query
  374. ;
  375. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  376. object = object[camelCaseValue];
  377. }
  378. else if( object[camelCaseValue] !== undefined ) {
  379. found = object[camelCaseValue];
  380. return false;
  381. }
  382. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  383. object = object[value];
  384. }
  385. else if( object[value] !== undefined ) {
  386. found = object[value];
  387. return false;
  388. }
  389. else {
  390. return false;
  391. }
  392. });
  393. }
  394. if ( $.isFunction( found ) ) {
  395. response = found.apply(context, passedArguments);
  396. }
  397. else if(found !== undefined) {
  398. response = found;
  399. }
  400. if($.isArray(returnedValue)) {
  401. returnedValue.push(response);
  402. }
  403. else if(returnedValue !== undefined) {
  404. returnedValue = [returnedValue, response];
  405. }
  406. else if(response !== undefined) {
  407. returnedValue = response;
  408. }
  409. return found;
  410. }
  411. };
  412. if(methodInvoked) {
  413. if(instance === undefined) {
  414. module.initialize();
  415. }
  416. module.invoke(query);
  417. }
  418. else {
  419. if(instance !== undefined) {
  420. module.destroy();
  421. }
  422. module.initialize();
  423. }
  424. })
  425. ;
  426. return (returnedValue !== undefined)
  427. ? returnedValue
  428. : this
  429. ;
  430. };
  431. $.fn.checkbox.settings = {
  432. name : 'Checkbox',
  433. namespace : 'checkbox',
  434. debug : false,
  435. verbose : true,
  436. performance : true,
  437. // delegated event context
  438. uncheckable : 'auto',
  439. fireOnInit : true,
  440. onChange : function(){},
  441. onChecked : function(){},
  442. onUnchecked : function(){},
  443. onEnabled : function(){},
  444. onDisabled : function(){},
  445. className : {
  446. checked : 'checked',
  447. disabled : 'disabled',
  448. radio : 'radio',
  449. readOnly : 'read-only'
  450. },
  451. error : {
  452. method : 'The method you called is not defined.'
  453. },
  454. selector : {
  455. input : 'input[type=checkbox], input[type=radio]',
  456. label : 'label'
  457. }
  458. };
  459. })( jQuery, window , document );