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.

528 lines
15 KiB

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