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.

216 lines
5.9 KiB

  1. /* ******************************
  2. Semantic Module: Checkbox
  3. Author: Jack Lukic
  4. Notes: First Commit March 25, 2013
  5. Simple plug-in which maintains the state for ui checkbox
  6. This can be done without javascript, only in instances
  7. where each checkbox is assigned a unique ID. This provides a separate
  8. programmatic option when that is not possible.
  9. ****************************** */
  10. ;(function ( $, window, document, undefined ) {
  11. $.fn.checkbox = function(parameters) {
  12. var
  13. $allModules = $(this),
  14. settings = $.extend(true, {}, $.fn.checkbox.settings, parameters),
  15. // make arguments available
  16. query = arguments[0],
  17. passedArguments = [].slice.call(arguments, 1),
  18. invokedResponse
  19. ;
  20. $allModules
  21. .each(function() {
  22. var
  23. $module = $(this),
  24. $input = $(this).find(settings.selector.input),
  25. selector = $module.selector || '',
  26. element = this,
  27. instance = $module.data('module-' + settings.namespace),
  28. methodInvoked = (typeof query == 'string'),
  29. className = settings.className,
  30. namespace = settings.namespace,
  31. errors = settings.errors,
  32. module
  33. ;
  34. module = {
  35. initialize: function() {
  36. if(settings.context && selector !== '') {
  37. module.verbose('Initializing checkbox with delegated events', $module);
  38. $(element, settings.context)
  39. .on(selector, 'click.' + namespace, module.toggle)
  40. ;
  41. }
  42. else {
  43. module.verbose('Initializing checkbox with bound events', $module);
  44. $module
  45. .on('click.' + namespace, module.toggle)
  46. ;
  47. }
  48. },
  49. destroy: function() {
  50. module.verbose('Destroying previous module for', $module);
  51. $module
  52. .off(namespace)
  53. ;
  54. },
  55. toggle: function() {
  56. if( $input.prop('checked') === undefined || !$input.prop('checked') ) {
  57. module.enable();
  58. }
  59. else {
  60. module.disable();
  61. }
  62. },
  63. enable: function() {
  64. module.debug('Enabling checkbox');
  65. $module
  66. .addClass(className.active)
  67. ;
  68. $input
  69. .prop('checked', true)
  70. ;
  71. $.proxy(settings.onChange, $input.get())();
  72. $.proxy(settings.onEnable, $input.get())();
  73. },
  74. disable: function() {
  75. module.debug('Disabling checkbox');
  76. $module
  77. .removeClass(className.active)
  78. ;
  79. $input
  80. .prop('checked', false)
  81. ;
  82. $.proxy(settings.onChange, $input.get())();
  83. $.proxy(settings.onDisable, $input.get())();
  84. },
  85. /* standard module */
  86. setting: function(name, value) {
  87. if(value === undefined) {
  88. return settings[name];
  89. }
  90. settings[name] = value;
  91. },
  92. verbose: function() {
  93. if(settings.verbose) {
  94. module.debug.apply(this, arguments);
  95. }
  96. },
  97. debug: function() {
  98. var
  99. output = [],
  100. message = settings.moduleName + ': ' + arguments[0],
  101. variables = [].slice.call( arguments, 1 ),
  102. log = console.info || console.log || function(){}
  103. ;
  104. log = Function.prototype.bind.call(log, console);
  105. if(settings.debug) {
  106. output.push(message);
  107. log.apply(console, output.concat(variables) );
  108. }
  109. },
  110. error: function() {
  111. var
  112. output = [],
  113. errorMessage = settings.moduleName + ': ' + arguments[0],
  114. variables = [].slice.call( arguments, 1 ),
  115. log = console.warn || console.log || function(){}
  116. ;
  117. log = Function.prototype.bind.call(log, console);
  118. if(settings.debug) {
  119. output.push(errorMessage);
  120. output.concat(variables);
  121. log.apply(console, output.concat(variables) );
  122. }
  123. },
  124. invoke: function(query, context, passedArguments) {
  125. var
  126. maxDepth,
  127. found
  128. ;
  129. passedArguments = passedArguments || [].slice.call( arguments, 2 );
  130. if(typeof query == 'string' && instance !== undefined) {
  131. query = query.split('.');
  132. maxDepth = query.length - 1;
  133. $.each(query, function(depth, value) {
  134. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  135. instance = instance[value];
  136. return true;
  137. }
  138. else if( instance[value] !== undefined ) {
  139. found = instance[value];
  140. return true;
  141. }
  142. module.error(errors.method);
  143. return false;
  144. });
  145. }
  146. if ( $.isFunction( found ) ) {
  147. return found.apply(context, passedArguments);
  148. }
  149. // return retrieved variable or chain
  150. return found;
  151. }
  152. };
  153. // check for invoking internal method
  154. if(methodInvoked) {
  155. invokedResponse = module.invoke(query, this, passedArguments);
  156. }
  157. // otherwise initialize
  158. else {
  159. module.initialize();
  160. }
  161. })
  162. ;
  163. // chain or return queried method
  164. return (invokedResponse !== undefined)
  165. ? invokedResponse
  166. : this
  167. ;
  168. };
  169. $.fn.checkbox.settings = {
  170. // module info
  171. moduleName : 'Checkbox Module',
  172. verbose : false,
  173. debug : true,
  174. namespace : 'checkbox',
  175. // delegated event context
  176. context : false,
  177. onChange : function(){},
  178. onEnable : function(){},
  179. onDisable : function(){},
  180. // errors
  181. errors : {
  182. method : 'The method you called is not defined.'
  183. },
  184. selector : {
  185. input : 'input'
  186. },
  187. className : {
  188. active : 'active'
  189. }
  190. };
  191. })( jQuery, window , document );