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.

506 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
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. module.error(error.method, query);
  391. return false;
  392. }
  393. });
  394. }
  395. if ( $.isFunction( found ) ) {
  396. response = found.apply(context, passedArguments);
  397. }
  398. else if(found !== undefined) {
  399. response = found;
  400. }
  401. if($.isArray(returnedValue)) {
  402. returnedValue.push(response);
  403. }
  404. else if(returnedValue !== undefined) {
  405. returnedValue = [returnedValue, response];
  406. }
  407. else if(response !== undefined) {
  408. returnedValue = response;
  409. }
  410. return found;
  411. }
  412. };
  413. if(methodInvoked) {
  414. if(instance === undefined) {
  415. module.initialize();
  416. }
  417. module.invoke(query);
  418. }
  419. else {
  420. if(instance !== undefined) {
  421. module.destroy();
  422. }
  423. module.initialize();
  424. }
  425. })
  426. ;
  427. return (returnedValue !== undefined)
  428. ? returnedValue
  429. : this
  430. ;
  431. };
  432. $.fn.checkbox.settings = {
  433. name : 'Checkbox',
  434. namespace : 'checkbox',
  435. debug : false,
  436. verbose : true,
  437. performance : true,
  438. // delegated event context
  439. uncheckable : 'auto',
  440. fireOnInit : true,
  441. onChange : function(){},
  442. onChecked : function(){},
  443. onUnchecked : function(){},
  444. onEnabled : function(){},
  445. onDisabled : function(){},
  446. className : {
  447. checked : 'checked',
  448. disabled : 'disabled',
  449. radio : 'radio',
  450. readOnly : 'read-only'
  451. },
  452. error : {
  453. method : 'The method you called is not defined'
  454. },
  455. selector : {
  456. input : 'input[type=checkbox], input[type=radio]',
  457. label : 'label'
  458. }
  459. };
  460. })( jQuery, window , document );