Skip to content

checkbox

Kechushou edited this page May 19, 2015 · 2 revisions

dom/form/checkbox

Description: Monitors checkbox and radio activity.

This module also does the following:

  • Updates radio or check state if the form is in update mode
  • Updates radio or check state if the input element has the "remember" attribute and the form is not in update mode

Functions

this.changeSelection(domElement)

Updates the model based on the state of domElement. Updates the "remember" cookie

this.serialize()

Returns a string capturing the input in the format of a post request.

this.summary(multiple)

Returns a string capturing the input to fit into a summary div on the main form page. Them "multiple" variable is a boolean that defines if the checkbox is apart of a fillDiv.

this.notSelected()

Concrete implementation of the dom/dom notSelected function. Used for the filter control to decide if the element should be included in the filter.

quickforms.form.updateRadios(formObj, name)

Updates the form model based on the state of the DOM.

define(['dom/form/form'],
function (){
	quickforms.CheckboxElement = function(dom,formObj,label) // Monitors checkbox activity
	{
		quickforms.DomElement.call(this,dom); // super call to get parent attributes
		var me = this;
		this.parent = formObj;
		this.label = label;
		this.hasBeenClicked = false;
		this.selectedField = 0;
		this.rememeber = dom.is("[remember]");
		this.multiple = dom.is("[multiple]");  // No need to insert into database if unchecked
		dom.on('change',function(eventData){
			me.changeSelection($(this));
			quickforms.form.updateRadios(me.parent,me.name);
		});
		if(dom.attr('type') == "radio")
		{
			var legendDom = dom.parents('fieldset').find('legend');
			this.label = legendDom.text() + ': '+this.label;
			formObj.radioGroups = formObj.radioGroups || {}
			formObj.radioGroups[this.name] = formObj.radioGroups[this.name] || [];
			formObj.radioGroups[this.name].push(this);
		}
		this.changeSelection = function(check)
		{
			me.hasBeenClicked = true;
			if(check.is(":checked") == true) {
			    me.selectedField = check.val();
				if(me.rememeber)
					setCookie(formObj.id+me.name,check.val(),quickforms.rememberLength);
				me.checked = true;
			}
			else{
				me.checked = false;
				me.selectedField = 0;

			}
			
		};
		this.serialize = function()
		{
			if(this.checked)
				return this.name+'='+this.selectedField;
			
			return '';
		};
		this.summary = function(multiple)
		{
			if(multiple && this.checked == true)
			{
				return this.label;
			}
			if(this.checked == true){
			
				return this.label + "<br />";
			}
			return '';
		};
		this.notSelected = function()
		{
			return !this.hasBeenClicked;
		};
	}
	quickforms.form.domParsers.push(function(formObj){
		
		var checks = formObj.dom.find('input[type="checkbox"],input[type="radio"]');
		
		checks.each(function(i,chek){
			chek = $(chek);
			if(!isNull(chek.attr('name')))
			{
				var checkLabel = chek.siblings('label[for="'+chek.attr('id')+'"]');
				
				var chekObj = new quickforms.CheckboxElement(chek,formObj,checkLabel.text());
				
				if((!isNull(formObj.updateId) && formObj.updateRow[chekObj.name] == chek.val())
                                        || (getCookie(formObj.id+chekObj.name) == chek.val() && isNull(formObj.updateId)) )
				{
					chek.attr('checked','checked');
					chekObj.changeSelection(chek);
				}
				chek.checkboxradio('refresh');
				formObj.addChild(chekObj);
				window.setTimeout(function(){formObj.finishedParsing();},1);
			}
		});
		for(var name in formObj.radioGroups)
		{
			quickforms.form.updateRadios(formObj,name);
		}
	});
	quickforms.form.updateRadios = function(formObj,name)
	{
		if(formObj.radioGroups)
		{
			var radGroup = formObj.radioGroups[name];
			if(!isNull(radGroup))
			{
				for(var i = 0; i< radGroup.length;i++)
				{
					radGroup[i].dom.checkboxradio('refresh');
					radGroup[i].changeSelection(radGroup[i].dom);
				}
			}
		}
	};
});
Clone this wiki locally