/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A collection of utilities for checkbox inputs
* @class Checkbox
* @namespace jsbeans
* @static
*/
jsbeans.Checkbox = {
/**
* Selects all checkboxes based on <code>checked</code> attribute of given object (so it can be a JSON too) and returns all selected items.<br/>
* @method selectAll
* @param object {DOM | Object | JSON} an object with a <code>checked</code> attribute
* @param [options] {JSON} an optional filter in JSON format to filter only checkboxes by their name or if the checkbox's id starts with a given string. An optional <code>forceSelection</code> attribute (boolean default to 'false') will force the selection of disabled checkboxes.
* <pre>
* name: <String>, // the value of attribute 'name'. It filters only those checkboxes with given name. Optional.
* startsWith: &l;String>, // It filters only those checkboxes with the first part of attribute 'id' matching this value. Optional.
* forceSelection: &l;boolean> // forces the selection of disabled checkboxes. Default false.
* </pre>
* @return {Array} checked checkboxes
* @static
*/
selectAll: function(object /*,options*/) {
var object = object;
var name = (arguments[1] && arguments[1].name) || null;
var startsWith = (arguments[1] && arguments[1].startsWith) || null;
var forceSelection = (arguments[1] && arguments[1].forceSelection && ("" + arguments[1].forceSelection) == "true") || false;
var inputs = document.getElementsByTagName("INPUT");
var res = [];
for (var i = 0, input; input = inputs[i]; i++) {
if (input.type && input.type.toLowerCase() == "checkbox") {
if (name != null && input.getAttribute("name") == name) {
if (forceSelection == false && input.disabled == true) {
continue;
}
input.checked = object.checked;
if (input.checked) {
res.push(input);
}
}
else if (startsWith != null && input.getAttribute("id") && input.getAttribute("id").indexOf(startsWith) == 0) {
if (forceSelection == false && input.disabled == true) {
continue;
}
input.checked = object.checked;
if (input.checked) {
res.push(input);
}
}
else if (name == null && startsWith == null) {
if (forceSelection == false && input.disabled == true) {
continue;
}
input.checked = object.checked;
if (input.checked) {
res.push(input);
}
}
}
}
inputs = null;
return res;
},
/**
* Returns all checked checkboxes based on {@param} <code class="param">options</code>.<br/>
* Note that if {@param} <code class="param">options</code> is empty all checked checkboxes in page will be returned.
* @method getSelected
* @param [options] {JSON} an optional filter in JSON format to filter only checkboxes by their name or if the checkbox's id starts with a given string.
* @return {Array} checked checkboxes
* @static
*/
getSelected: function(options) {
var name = options && options.name || null;
var startsWith = options && options.startsWith || null;
var inputs = document.getElementsByTagName("INPUT");
var res = [];
for (var i = 0, input; input = inputs[i]; i++) {
if (input.type && input.type.toLowerCase() == "checkbox") {
if (name != null && input.getAttribute("name") == name) {
if (input.checked) {
res.push(input);
}
}
else if (startsWith != null && input.getAttribute("id") && input.getAttribute("id").indexOf(startsWith) == 0) {
if (input.checked) {
res.push(input);
}
}
else if (name == null && startsWith == null) {
if (input.checked) {
res.push(input);
}
}
}
}
inputs = null;
return res;
}
};