From 097d6bb22f8ae13922fdd7fad8cddd657f42686b Mon Sep 17 00:00:00 2001 From: Rubens Mariuzzo Date: Sun, 21 Sep 2014 20:49:30 -0400 Subject: [PATCH 1/3] Added code documentation and some refactor were made to make the code more legible. --- dist/jquery.checkboxes-1.0.5.min.js | 2 +- src/jquery.checkboxes.js | 341 ++++++++++++++++------------ 2 files changed, 193 insertions(+), 150 deletions(-) diff --git a/dist/jquery.checkboxes-1.0.5.min.js b/dist/jquery.checkboxes-1.0.5.min.js index 2efee2b..3a99463 100644 --- a/dist/jquery.checkboxes-1.0.5.min.js +++ b/dist/jquery.checkboxes-1.0.5.min.js @@ -1 +1 @@ -/*! checkboxes.js v1.0.5 | (c) 2013, 2014 Rubens Mariuzzo | http://github.com/rmariuzzo/checkboxes.js/LICENSE */!function(a){var b=function(a){this.$context=a};b.prototype.check=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!0)},b.prototype.uncheck=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!1)},b.prototype.toggle=function(){this.$context.find(":checkbox").filter(":not(:disabled)").each(function(){var b=a(this);b.prop("checked",!b.is(":checked"))})},b.prototype.max=function(a){if(0===a)this.$context.off("click.checkboxes");else if(a>0){var b=this;this.$context.on("click.checkboxes.max",":checkbox",function(){b.$context.find(":checked").length===a?b.$context.find(":checkbox:not(:checked)").prop("disabled",!0):b.$context.find(":checkbox:not(:checked)").prop("disabled",!1)})}},b.prototype.range=function(b){if(b){var c=this;this.$context.on("click.checkboxes.range",":checkbox",function(b){var d=a(b.target);if(b.shiftKey&&c.$last){var e=c.$context.find(":checkbox"),f=e.index(c.$last),g=e.index(d),h=Math.min(f,g),i=Math.max(f,g)+1;e.slice(h,i).filter(":not(:disabled)").prop("checked",d.prop("checked"))}c.$last=d})}else this.$context.off("click.checkboxes.range")};var c=a.fn.checkboxes;a.fn.checkboxes=function(c){var d=Array.prototype.slice.call(arguments,1);return this.each(function(){var e=a(this),f=e.data("checkboxes");f||e.data("checkboxes",f=new b(e,"object"==typeof c&&c)),"string"==typeof c&&f[c]&&f[c].apply(f,d)})},a.fn.checkboxes.Constructor=b,a.fn.checkboxes.noConflict=function(){return a.fn.checkboxes=c,this},a(document).on("click.checkboxes.data-api","[data-toggle^=checkboxes]",function(b){var c=a(b.target),d=c.attr("href"),e=a(c.data("context")||d&&d.replace(/.*(?=#[^\s]+$)/,"")),f=c.data("action");e&&f&&(c.is(":checkbox")||b.preventDefault(),e.checkboxes(f))}),a(document).on("ready.checkboxes.data-api",function(){a("[data-toggle^=checkboxes]").each(function(){var b=a(this),c=b.data();delete c.toggle;for(var d in c)b.checkboxes(d,c[d])})})}(window.jQuery); \ No newline at end of file +/*! checkboxes.js v1.0.5 | (c) 2013, 2014 Rubens Mariuzzo | http://github.com/rmariuzzo/checkboxes.js/LICENSE */"use strict";!function(a){var b=function(a){this.$context=a};b.prototype.check=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!0)},b.prototype.uncheck=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!1)},b.prototype.toggle=function(){this.$context.find(":checkbox").filter(":not(:disabled)").each(function(){var b=a(this);b.prop("checked",!b.is(":checked"))})},b.prototype.max=function(a){if(a>0){var b=this;this.$context.on("click.checkboxes.max",":checkbox",function(){b.$context.find(":checked").length===a?b.$context.find(":checkbox:not(:checked)").prop("disabled",!0):b.$context.find(":checkbox:not(:checked)").prop("disabled",!1)})}else this.$context.off("click.checkboxes")},b.prototype.range=function(b){if(b){var c=this;this.$context.on("click.checkboxes.range",":checkbox",function(b){var d=a(b.target);if(b.shiftKey&&c.$last){var e=c.$context.find(":checkbox"),f=e.index(c.$last),g=e.index(d),h=Math.min(f,g),i=Math.max(f,g)+1;e.slice(h,i).filter(":not(:disabled)").prop("checked",d.prop("checked"))}c.$last=d})}else this.$context.off("click.checkboxes.range")};var c=a.fn.checkboxes;a.fn.checkboxes=function(c){var d=Array.prototype.slice.call(arguments,1);return this.each(function(){var e=a(this),f=e.data("checkboxes");f||e.data("checkboxes",f=new b(e,"object"==typeof c&&c)),"string"==typeof c&&f[c]&&f[c].apply(f,d)})},a.fn.checkboxes.Constructor=b,a.fn.checkboxes.noConflict=function(){return a.fn.checkboxes=c,this};var d=function(b){var c=a(b.target),d=c.attr("href"),e=a(c.data("context")||d&&d.replace(/.*(?=#[^\s]+$)/,"")),f=c.data("action");e&&f&&(c.is(":checkbox")||b.preventDefault(),e.checkboxes(f))},e=function(){a("[data-toggle^=checkboxes]").each(function(){var b=a(this),c=b.data();delete c.toggle;for(var d in c)b.checkboxes(d,c[d])})};a(document).on("click.checkboxes.data-api","[data-toggle^=checkboxes]",d),a(document).on("ready.checkboxes.data-api",e)}(window.jQuery); \ No newline at end of file diff --git a/src/jquery.checkboxes.js b/src/jquery.checkboxes.js index 8dc0f8d..0002c01 100644 --- a/src/jquery.checkboxes.js +++ b/src/jquery.checkboxes.js @@ -1,149 +1,192 @@ -(function($) { - - ////////////////////////////////// - /* Checkboxes class definition. */ - ////////////////////////////////// - - var Checkboxes = function($context, options) { - this.$context = $context; - }; - - /** - * Check all checkboxes in context. - */ - Checkboxes.prototype.check = function() { - this.$context.find(':checkbox') - .filter(':not(:disabled)') - .prop('checked', true); - }; - - /** - * Uncheck all checkboxes in context. - */ - Checkboxes.prototype.uncheck = function() { - this.$context.find(':checkbox') - .filter(':not(:disabled)') - .prop('checked', false); - }; - - /** - * Toggle the state of all checkboxes in context. - */ - Checkboxes.prototype.toggle = function() { - this.$context.find(':checkbox').filter(':not(:disabled)').each(function() { - var $checkbox = $(this); - $checkbox.prop('checked', !$checkbox.is(':checked')); - }); - }; - - /** - * Set the maximum number of checkboxes that can be checked. - * - * @param max {number} The maximum number of checkbox allowed to be checked. - */ - Checkboxes.prototype.max = function(max) { - if (max === 0) { - // Disable max. - this.$context.off('click.checkboxes'); - } else if (max > 0) { - // Enable max. - var instance = this; - this.$context.on('click.checkboxes.max', ':checkbox', function(e) { - if (instance.$context.find(':checked').length === max) { - instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); - } else { - instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); - } - }); - } - }; - - /** - * Enable or disable range selection. - * - * @param enable {boolean} Indicate is range selection has to be enabled. - */ - Checkboxes.prototype.range = function(enable) { - if (enable) { - var instance = this; - this.$context.on('click.checkboxes.range', ':checkbox', function(e) { - var $checkbox = $(e.target); - if (e.shiftKey && instance.$last) { - var $checkboxes = instance.$context.find(':checkbox'), - from = $checkboxes.index(instance.$last), - to = $checkboxes.index($checkbox), - start = Math.min(from, to), - end = Math.max(from, to) + 1; - $checkboxes.slice(start, end) - .filter(':not(:disabled)') - .prop('checked', $checkbox.prop('checked')); - } - instance.$last = $checkbox; - }); - } else { - this.$context.off('click.checkboxes.range'); - } - }; - - /////////////////////////////////// - /* Checkboxes plugin definition. */ - /////////////////////////////////// - - var old = $.fn.checkboxes; - - $.fn.checkboxes = function(method) { - var methodArgs = Array.prototype.slice.call(arguments, 1); - return this.each(function() { - var $this = $(this), - data = $this.data('checkboxes'); - if (!data) { - $this.data('checkboxes', (data = new Checkboxes($this, typeof method === 'object' && method))); - } - if (typeof method === 'string' && data[method]) { - data[method].apply(data, methodArgs); - } - }); - }; - - $.fn.checkboxes.Constructor = Checkboxes; - - - ///////////////////////////// - /* Checkboxes no conflict. */ - ///////////////////////////// - - $.fn.checkboxes.noConflict = function() { - $.fn.checkboxes = old; - return this; - }; - - - ////////////////////////// - /* Checkboxes data-api. */ - ////////////////////////// - - $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', function(e) { - var el = $(e.target), - href = el.attr('href'), - $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))), - action = el.data('action'); - if ($context && action) { - if (!el.is(':checkbox')) { - e.preventDefault(); - } - $context.checkboxes(action); - } - }); - - $(document).on('ready.checkboxes.data-api', function() { - $('[data-toggle^=checkboxes]').each(function() { - var el = $(this), - actions = el.data(); - delete actions.toggle; - for (var action in actions) { - el.checkboxes(action, actions[action]); - } - }); - }); - -})(window.jQuery); \ No newline at end of file +'use strict'; + +(function ($) { + + ////////////////////////////////// + /* Checkboxes class definition. */ + ////////////////////////////////// + + /** + * Create a new checkbox context. + * + * @param {Object} context DOM context. + */ + var Checkboxes = function (context) { + this.$context = context; + }; + + /** + * Check all checkboxes in context. + */ + Checkboxes.prototype.check = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .prop('checked', true); + }; + + /** + * Uncheck all checkboxes in context. + */ + Checkboxes.prototype.uncheck = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .prop('checked', false); + }; + + /** + * Toggle the state of all checkboxes in context. + */ + Checkboxes.prototype.toggle = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .each(function () { + var $checkbox = $(this); + $checkbox.prop('checked', !$checkbox.is(':checked')); + }); + }; + + /** + * Set the maximum number of checkboxes that can be checked. + * + * @param {Number} max The maximum number of checkbox allowed to be checked. + */ + Checkboxes.prototype.max = function (max) { + if (max > 0) { + // Enable max. + var instance = this; + this.$context.on('click.checkboxes.max', ':checkbox', function () { + if (instance.$context.find(':checked').length === max) { + instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); + } else { + instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); + } + }); + } else { + // Disable max. + this.$context.off('click.checkboxes'); + } + }; + + /** + * Enable or disable range selection. + * + * @param {Boolean} enable Indicate is range selection has to be enabled. + */ + Checkboxes.prototype.range = function (enable) { + if (enable) { + var instance = this; + + this.$context.on('click.checkboxes.range', ':checkbox', function (event) { + var $checkbox = $(event.target); + + if (event.shiftKey && instance.$last) { + var $checkboxes = instance.$context.find(':checkbox'); + var from = $checkboxes.index(instance.$last); + var to = $checkboxes.index($checkbox); + var start = Math.min(from, to); + var end = Math.max(from, to) + 1; + + $checkboxes.slice(start, end) + .filter(':not(:disabled)') + .prop('checked', $checkbox.prop('checked')); + } + instance.$last = $checkbox; + }); + } else { + this.$context.off('click.checkboxes.range'); + } + }; + + /////////////////////////////////// + /* Checkboxes plugin definition. */ + /////////////////////////////////// + + // Keep old Checkboxes jQuery plugin, if any, to no override it. + var old = $.fn.checkboxes; + + /** + * Checkboxes jQuery plugin. + * + * @param {String} method Method to invoke. + * + * @return {Object} jQuery object. + */ + $.fn.checkboxes = function (method) { + // Get extra arguments as method arguments. + var methodArgs = Array.prototype.slice.call(arguments, 1); + + return this.each(function () { + var $this = $(this); + + // Check if we already have an instance. + var instance = $this.data('checkboxes'); + if (!instance) { + $this.data('checkboxes', (instance = new Checkboxes($this, typeof method === 'object' && method))); + } + + // Check if we need to invoke a public method. + if (typeof method === 'string' && instance[method]) { + instance[method].apply(instance, methodArgs); + } + }); + }; + + // Store a constructor reference. + $.fn.checkboxes.Constructor = Checkboxes; + + + ///////////////////////////// + /* Checkboxes no conflict. */ + ///////////////////////////// + + /** + * No conflictive Checkboxes jQuery plugin. + */ + $.fn.checkboxes.noConflict = function () { + $.fn.checkboxes = old; + return this; + }; + + + ////////////////////////// + /* Checkboxes data-api. */ + ////////////////////////// + + /** + * Handle data-api click. + * + * @param {Object} event Click event. + */ + var dataApiClickHandler = function (event) { + var el = $(event.target); + var href = el.attr('href'); + var $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); + var action = el.data('action'); + + if ($context && action) { + if (!el.is(':checkbox')) { + event.preventDefault(); + } + $context.checkboxes(action); + } + }; + + /** + * Handle data-api DOM ready. + */ + var dataApiDomReadyHandler = function () { + $('[data-toggle^=checkboxes]').each(function () { + var el = $(this), + actions = el.data(); + delete actions.toggle; + for (var action in actions) { + el.checkboxes(action, actions[action]); + } + }); + }; + + // Register data-api listeners. + $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', dataApiClickHandler); + $(document).on('ready.checkboxes.data-api', dataApiDomReadyHandler); + +})(window.jQuery); From 6e270e0dba5fc7c6c6151385ac8ca01f598f8958 Mon Sep 17 00:00:00 2001 From: Rubens Mariuzzo Date: Fri, 12 Dec 2014 11:13:56 -0400 Subject: [PATCH 2/3] Updating .editorconfig file. --- .editorconfig | 6 +- Gruntfile.js | 114 ++++++------ src/jquery.checkboxes.js | 372 +++++++++++++++++++-------------------- 3 files changed, 245 insertions(+), 247 deletions(-) diff --git a/.editorconfig b/.editorconfig index 1b9db3e..14a286d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,12 +8,8 @@ root = true end_of_line = lf insert_final_newline = true -# Tab indentation (no size specified) +# Tab indentation. [*.js] -indent_style = tab - -# Indentation override for all JS under lib directory -[lib/**.js] indent_style = space indent_size = 2 diff --git a/Gruntfile.js b/Gruntfile.js index 2ca3220..48497ef 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,69 +1,71 @@ -module.exports = function(grunt) { +'use strict'; - // Load all Grunt tasks. - require('load-grunt-tasks')(grunt); +module.exports = function (grunt) { - // Grunt configuration. - grunt.initConfig({ + // Load all Grunt tasks. + require('load-grunt-tasks')(grunt); - pkg: grunt.file.readJSON('package.json'), + // Grunt configuration. + grunt.initConfig({ - watch: { - scripts: { - files: [ - 'Gruntfile.js', - 'src/**/*.js', - 'tests/specs/**/*.js', - '!.grunt' - ], - tasks: ['jshint', 'jasmine'] - } - }, + pkg: grunt.file.readJSON('package.json'), - uglify: { - all: { - files: { - 'dist/jquery.checkboxes-<%= pkg.version %>.min.js': ['src/jquery.checkboxes.js'] - }, - options: { - banner: '/*! checkboxes.js v<%= pkg.version %> | ' + - '(c) 2013, <%= grunt.template.today("yyyy") %> Rubens Mariuzzo | ' + - 'http://github.com/rmariuzzo/checkboxes.js/LICENSE */', - } - } - }, + watch: { + scripts: { + files: [ + 'Gruntfile.js', + 'src/**/*.js', + 'tests/specs/**/*.js', + '!.grunt' + ], + tasks: ['jshint', 'jasmine'] + } + }, - jshint: { - all: { - src: [ - 'Gruntfile.js', - 'src/**/*.js', - 'tests/spec/**/*.js' - ], - options: { - jshintrc: true - } - } + uglify: { + all: { + files: { + 'dist/jquery.checkboxes-<%= pkg.version %>.min.js': ['src/jquery.checkboxes.js'] }, + options: { + banner: '/*! checkboxes.js v<%= pkg.version %> | ' + + '(c) 2013, <%= grunt.template.today("yyyy") %> Rubens Mariuzzo | ' + + 'http://github.com/rmariuzzo/checkboxes.js/LICENSE */', + } + } + }, + + jshint: { + all: { + src: [ + 'Gruntfile.js', + 'src/**/*.js', + 'tests/spec/**/*.js' + ], + options: { + jshintrc: true + } + } + }, - jasmine: { - all: { - src: 'src/**/*.js', - options: { - specs: 'tests/specs/*_spec.js', - vendor: [ - 'bower_components/jquery/dist/jquery.min.js', - 'bower_components/jasmine-jquery/lib/jasmine-jquery.js' - ] - } - } + jasmine: { + all: { + src: 'src/**/*.js', + options: { + specs: 'tests/specs/*_spec.js', + vendor: [ + 'bower_components/jquery/dist/jquery.min.js', + 'bower_components/jasmine-jquery/lib/jasmine-jquery.js' + ] } + } + } - }); + }); - grunt.registerTask('default', ['jshint', 'watch']); - grunt.registerTask('build', ['jshint', 'jasmine', 'uglify']); - grunt.registerTask('test', ['jasmine']); - grunt.registerTask('travis', ['jshint', 'jasmine']); + grunt.registerTask('default', ['jshint', 'watch']); + grunt.registerTask('build', ['jshint', 'jasmine', 'uglify']); + grunt.registerTask('test', ['jasmine']); + grunt.registerTask('travis', ['jshint', 'jasmine']); }; diff --git a/src/jquery.checkboxes.js b/src/jquery.checkboxes.js index 0002c01..ce7026d 100644 --- a/src/jquery.checkboxes.js +++ b/src/jquery.checkboxes.js @@ -2,191 +2,191 @@ (function ($) { - ////////////////////////////////// - /* Checkboxes class definition. */ - ////////////////////////////////// - - /** - * Create a new checkbox context. - * - * @param {Object} context DOM context. - */ - var Checkboxes = function (context) { - this.$context = context; - }; - - /** - * Check all checkboxes in context. - */ - Checkboxes.prototype.check = function () { - this.$context.find(':checkbox') - .filter(':not(:disabled)') - .prop('checked', true); - }; - - /** - * Uncheck all checkboxes in context. - */ - Checkboxes.prototype.uncheck = function () { - this.$context.find(':checkbox') - .filter(':not(:disabled)') - .prop('checked', false); - }; - - /** - * Toggle the state of all checkboxes in context. - */ - Checkboxes.prototype.toggle = function () { - this.$context.find(':checkbox') - .filter(':not(:disabled)') - .each(function () { - var $checkbox = $(this); - $checkbox.prop('checked', !$checkbox.is(':checked')); - }); - }; - - /** - * Set the maximum number of checkboxes that can be checked. - * - * @param {Number} max The maximum number of checkbox allowed to be checked. - */ - Checkboxes.prototype.max = function (max) { - if (max > 0) { - // Enable max. - var instance = this; - this.$context.on('click.checkboxes.max', ':checkbox', function () { - if (instance.$context.find(':checked').length === max) { - instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); - } else { - instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); - } - }); - } else { - // Disable max. - this.$context.off('click.checkboxes'); - } - }; - - /** - * Enable or disable range selection. - * - * @param {Boolean} enable Indicate is range selection has to be enabled. - */ - Checkboxes.prototype.range = function (enable) { - if (enable) { - var instance = this; - - this.$context.on('click.checkboxes.range', ':checkbox', function (event) { - var $checkbox = $(event.target); - - if (event.shiftKey && instance.$last) { - var $checkboxes = instance.$context.find(':checkbox'); - var from = $checkboxes.index(instance.$last); - var to = $checkboxes.index($checkbox); - var start = Math.min(from, to); - var end = Math.max(from, to) + 1; - - $checkboxes.slice(start, end) - .filter(':not(:disabled)') - .prop('checked', $checkbox.prop('checked')); - } - instance.$last = $checkbox; - }); - } else { - this.$context.off('click.checkboxes.range'); - } - }; - - /////////////////////////////////// - /* Checkboxes plugin definition. */ - /////////////////////////////////// - - // Keep old Checkboxes jQuery plugin, if any, to no override it. - var old = $.fn.checkboxes; - - /** - * Checkboxes jQuery plugin. - * - * @param {String} method Method to invoke. - * - * @return {Object} jQuery object. - */ - $.fn.checkboxes = function (method) { - // Get extra arguments as method arguments. - var methodArgs = Array.prototype.slice.call(arguments, 1); - - return this.each(function () { - var $this = $(this); - - // Check if we already have an instance. - var instance = $this.data('checkboxes'); - if (!instance) { - $this.data('checkboxes', (instance = new Checkboxes($this, typeof method === 'object' && method))); - } - - // Check if we need to invoke a public method. - if (typeof method === 'string' && instance[method]) { - instance[method].apply(instance, methodArgs); - } - }); - }; - - // Store a constructor reference. - $.fn.checkboxes.Constructor = Checkboxes; - - - ///////////////////////////// - /* Checkboxes no conflict. */ - ///////////////////////////// - - /** - * No conflictive Checkboxes jQuery plugin. - */ - $.fn.checkboxes.noConflict = function () { - $.fn.checkboxes = old; - return this; - }; - - - ////////////////////////// - /* Checkboxes data-api. */ - ////////////////////////// - - /** - * Handle data-api click. - * - * @param {Object} event Click event. - */ - var dataApiClickHandler = function (event) { - var el = $(event.target); - var href = el.attr('href'); - var $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); - var action = el.data('action'); - - if ($context && action) { - if (!el.is(':checkbox')) { - event.preventDefault(); - } - $context.checkboxes(action); - } - }; - - /** - * Handle data-api DOM ready. - */ - var dataApiDomReadyHandler = function () { - $('[data-toggle^=checkboxes]').each(function () { - var el = $(this), - actions = el.data(); - delete actions.toggle; - for (var action in actions) { - el.checkboxes(action, actions[action]); - } - }); - }; - - // Register data-api listeners. - $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', dataApiClickHandler); - $(document).on('ready.checkboxes.data-api', dataApiDomReadyHandler); + //////////////////////// + /* Checkboxes object. */ + //////////////////////// + + /** + * Create a new checkbox context. + * + * @param {Object} context DOM context. + */ + var Checkboxes = function (context) { + this.$context = context; + }; + + /** + * Check all checkboxes in context. + */ + Checkboxes.prototype.check = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .prop('checked', true); + }; + + /** + * Uncheck all checkboxes in context. + */ + Checkboxes.prototype.uncheck = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .prop('checked', false); + }; + + /** + * Toggle the state of all checkboxes in context. + */ + Checkboxes.prototype.toggle = function () { + this.$context.find(':checkbox') + .filter(':not(:disabled)') + .each(function () { + var $checkbox = $(this); + $checkbox.prop('checked', !$checkbox.is(':checked')); + }); + }; + + /** + * Set the maximum number of checkboxes that can be checked. + * + * @param {Number} max The maximum number of checkbox allowed to be checked. + */ + Checkboxes.prototype.max = function (max) { + if (max > 0) { + // Enable max. + var instance = this; + this.$context.on('click.checkboxes.max', ':checkbox', function () { + if (instance.$context.find(':checked').length === max) { + instance.$context.find(':checkbox:not(:checked)').prop('disabled', true); + } else { + instance.$context.find(':checkbox:not(:checked)').prop('disabled', false); + } + }); + } else { + // Disable max. + this.$context.off('click.checkboxes'); + } + }; + + /** + * Enable or disable range selection. + * + * @param {Boolean} enable Indicate is range selection has to be enabled. + */ + Checkboxes.prototype.range = function (enable) { + if (enable) { + var instance = this; + + this.$context.on('click.checkboxes.range', ':checkbox', function (event) { + var $checkbox = $(event.target); + + if (event.shiftKey && instance.$last) { + var $checkboxes = instance.$context.find(':checkbox'); + var from = $checkboxes.index(instance.$last); + var to = $checkboxes.index($checkbox); + var start = Math.min(from, to); + var end = Math.max(from, to) + 1; + + $checkboxes.slice(start, end) + .filter(':not(:disabled)') + .prop('checked', $checkbox.prop('checked')); + } + instance.$last = $checkbox; + }); + } else { + this.$context.off('click.checkboxes.range'); + } + }; + + /////////////////////////////// + /* Checkboxes jQuery plugin. */ + /////////////////////////////// + + // Keep old Checkboxes jQuery plugin, if any, to no override it. + var old = $.fn.checkboxes; + + /** + * Checkboxes jQuery plugin. + * + * @param {String} method Method to invoke. + * + * @return {Object} jQuery object. + */ + $.fn.checkboxes = function (method) { + // Get extra arguments as method arguments. + var methodArgs = Array.prototype.slice.call(arguments, 1); + + return this.each(function () { + var $this = $(this); + + // Check if we already have an instance. + var instance = $this.data('checkboxes'); + if (!instance) { + $this.data('checkboxes', (instance = new Checkboxes($this, typeof method === 'object' && method))); + } + + // Check if we need to invoke a public method. + if (typeof method === 'string' && instance[method]) { + instance[method].apply(instance, methodArgs); + } + }); + }; + + // Store a constructor reference. + $.fn.checkboxes.Constructor = Checkboxes; + + + //////////////////////////////////// + /* Checkboxes jQuery no conflict. */ + //////////////////////////////////// + + /** + * No conflictive Checkboxes jQuery plugin. + */ + $.fn.checkboxes.noConflict = function () { + $.fn.checkboxes = old; + return this; + }; + + + ////////////////////////// + /* Checkboxes data-api. */ + ////////////////////////// + + /** + * Handle data-api click. + * + * @param {Object} event Click event. + */ + var dataApiClickHandler = function (event) { + var el = $(event.target); + var href = el.attr('href'); + var $context = $(el.data('context') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))); + var action = el.data('action'); + + if ($context && action) { + if (!el.is(':checkbox')) { + event.preventDefault(); + } + $context.checkboxes(action); + } + }; + + /** + * Handle data-api DOM ready. + */ + var dataApiDomReadyHandler = function () { + $('[data-toggle^=checkboxes]').each(function () { + var el = $(this), + actions = el.data(); + delete actions.toggle; + for (var action in actions) { + el.checkboxes(action, actions[action]); + } + }); + }; + + // Register data-api listeners. + $(document).on('click.checkboxes.data-api', '[data-toggle^=checkboxes]', dataApiClickHandler); + $(document).on('ready.checkboxes.data-api', dataApiDomReadyHandler); })(window.jQuery); From fcb72340ca65c8b6c31c663507f99bbe77355d30 Mon Sep 17 00:00:00 2001 From: Rubens Mariuzzo Date: Fri, 12 Dec 2014 11:19:05 -0400 Subject: [PATCH 3/3] Preparing version 1.0.6 (clean task added). --- Gruntfile.js | 4 +++- ...checkboxes-1.0.5.min.js => jquery.checkboxes-1.0.6.min.js} | 2 +- package.json | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) rename dist/{jquery.checkboxes-1.0.5.min.js => jquery.checkboxes-1.0.6.min.js} (96%) diff --git a/Gruntfile.js b/Gruntfile.js index 48497ef..a3455c0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,6 +10,8 @@ module.exports = function (grunt) { pkg: grunt.file.readJSON('package.json'), + clean: ['dist'], + watch: { scripts: { files: [ @@ -64,7 +66,7 @@ module.exports = function (grunt) { }); grunt.registerTask('default', ['jshint', 'watch']); - grunt.registerTask('build', ['jshint', 'jasmine', 'uglify']); + grunt.registerTask('build', ['clean', 'jshint', 'jasmine', 'uglify']); grunt.registerTask('test', ['jasmine']); grunt.registerTask('travis', ['jshint', 'jasmine']); diff --git a/dist/jquery.checkboxes-1.0.5.min.js b/dist/jquery.checkboxes-1.0.6.min.js similarity index 96% rename from dist/jquery.checkboxes-1.0.5.min.js rename to dist/jquery.checkboxes-1.0.6.min.js index 3a99463..b8ab064 100644 --- a/dist/jquery.checkboxes-1.0.5.min.js +++ b/dist/jquery.checkboxes-1.0.6.min.js @@ -1 +1 @@ -/*! checkboxes.js v1.0.5 | (c) 2013, 2014 Rubens Mariuzzo | http://github.com/rmariuzzo/checkboxes.js/LICENSE */"use strict";!function(a){var b=function(a){this.$context=a};b.prototype.check=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!0)},b.prototype.uncheck=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!1)},b.prototype.toggle=function(){this.$context.find(":checkbox").filter(":not(:disabled)").each(function(){var b=a(this);b.prop("checked",!b.is(":checked"))})},b.prototype.max=function(a){if(a>0){var b=this;this.$context.on("click.checkboxes.max",":checkbox",function(){b.$context.find(":checked").length===a?b.$context.find(":checkbox:not(:checked)").prop("disabled",!0):b.$context.find(":checkbox:not(:checked)").prop("disabled",!1)})}else this.$context.off("click.checkboxes")},b.prototype.range=function(b){if(b){var c=this;this.$context.on("click.checkboxes.range",":checkbox",function(b){var d=a(b.target);if(b.shiftKey&&c.$last){var e=c.$context.find(":checkbox"),f=e.index(c.$last),g=e.index(d),h=Math.min(f,g),i=Math.max(f,g)+1;e.slice(h,i).filter(":not(:disabled)").prop("checked",d.prop("checked"))}c.$last=d})}else this.$context.off("click.checkboxes.range")};var c=a.fn.checkboxes;a.fn.checkboxes=function(c){var d=Array.prototype.slice.call(arguments,1);return this.each(function(){var e=a(this),f=e.data("checkboxes");f||e.data("checkboxes",f=new b(e,"object"==typeof c&&c)),"string"==typeof c&&f[c]&&f[c].apply(f,d)})},a.fn.checkboxes.Constructor=b,a.fn.checkboxes.noConflict=function(){return a.fn.checkboxes=c,this};var d=function(b){var c=a(b.target),d=c.attr("href"),e=a(c.data("context")||d&&d.replace(/.*(?=#[^\s]+$)/,"")),f=c.data("action");e&&f&&(c.is(":checkbox")||b.preventDefault(),e.checkboxes(f))},e=function(){a("[data-toggle^=checkboxes]").each(function(){var b=a(this),c=b.data();delete c.toggle;for(var d in c)b.checkboxes(d,c[d])})};a(document).on("click.checkboxes.data-api","[data-toggle^=checkboxes]",d),a(document).on("ready.checkboxes.data-api",e)}(window.jQuery); \ No newline at end of file +/*! checkboxes.js v1.0.6 | (c) 2013, 2014 Rubens Mariuzzo | http://github.com/rmariuzzo/checkboxes.js/LICENSE */"use strict";!function(a){var b=function(a){this.$context=a};b.prototype.check=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!0)},b.prototype.uncheck=function(){this.$context.find(":checkbox").filter(":not(:disabled)").prop("checked",!1)},b.prototype.toggle=function(){this.$context.find(":checkbox").filter(":not(:disabled)").each(function(){var b=a(this);b.prop("checked",!b.is(":checked"))})},b.prototype.max=function(a){if(a>0){var b=this;this.$context.on("click.checkboxes.max",":checkbox",function(){b.$context.find(":checked").length===a?b.$context.find(":checkbox:not(:checked)").prop("disabled",!0):b.$context.find(":checkbox:not(:checked)").prop("disabled",!1)})}else this.$context.off("click.checkboxes")},b.prototype.range=function(b){if(b){var c=this;this.$context.on("click.checkboxes.range",":checkbox",function(b){var d=a(b.target);if(b.shiftKey&&c.$last){var e=c.$context.find(":checkbox"),f=e.index(c.$last),g=e.index(d),h=Math.min(f,g),i=Math.max(f,g)+1;e.slice(h,i).filter(":not(:disabled)").prop("checked",d.prop("checked"))}c.$last=d})}else this.$context.off("click.checkboxes.range")};var c=a.fn.checkboxes;a.fn.checkboxes=function(c){var d=Array.prototype.slice.call(arguments,1);return this.each(function(){var e=a(this),f=e.data("checkboxes");f||e.data("checkboxes",f=new b(e,"object"==typeof c&&c)),"string"==typeof c&&f[c]&&f[c].apply(f,d)})},a.fn.checkboxes.Constructor=b,a.fn.checkboxes.noConflict=function(){return a.fn.checkboxes=c,this};var d=function(b){var c=a(b.target),d=c.attr("href"),e=a(c.data("context")||d&&d.replace(/.*(?=#[^\s]+$)/,"")),f=c.data("action");e&&f&&(c.is(":checkbox")||b.preventDefault(),e.checkboxes(f))},e=function(){a("[data-toggle^=checkboxes]").each(function(){var b=a(this),c=b.data();delete c.toggle;for(var d in c)b.checkboxes(d,c[d])})};a(document).on("click.checkboxes.data-api","[data-toggle^=checkboxes]",d),a(document).on("ready.checkboxes.data-api",e)}(window.jQuery); \ No newline at end of file diff --git a/package.json b/package.json index 4fa05a4..2c2bbc0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "checkboxes.js", - "version": "1.0.5", + "version": "1.0.6", "homepage": "https://github.com/rmariuzzo/checkboxes.js", "keywords": "checkbox checkboxes", "license": "MIT", @@ -11,6 +11,7 @@ }, "devDependencies": { "grunt": "~0.4.1", + "grunt-contrib-clean": "^0.6.0", "grunt-contrib-jasmine": "~0.6.5", "grunt-contrib-jshint": "~0.10.0", "grunt-contrib-uglify": "~0.2.2",