dojo.provide("my_ext.CheckboxTreeNode");

dojo.require("dijit.Tree");
dojo.require("dijit.form.CheckBox");

dojo.declare(
"my_ext.CheckboxTreeNode",
[dijit._TreeNode],
{    			   
    templateString:dojo.cache("dijit","templates/TreeNode.html","<div class=\"dijitTreeNode\" waiRole=\"presentation\"\n\t><div dojoAttachPoint=\"rowNode\" class=\"dijitTreeRow\" waiRole=\"presentation\" dojoAttachEvent=\"onmouseenter:_onMouseEnter, onmouseleave:_onMouseLeave, onclick:_onClick, ondblclick:_onDblClick\"\n\t\t><img src=\"${_blankGif}\" alt=\"\" dojoAttachPoint=\"expandoNode\" class=\"dijitTreeExpando\" waiRole=\"presentation\"\n\t\t><span dojoAttachPoint=\"expandoNodeText\" class=\"dijitExpandoText\" waiRole=\"presentation\"\n\t\t></span\n\t\t><span dojoAttachPoint=\"contentNode\"\n\t\t\tclass=\"dijitTreeContent\" waiRole=\"presentation\">\n\t\t\t<span dojoType=\"dijit.form.CheckBox\"></span><img src=\"${_blankGif}\" alt=\"\" dojoAttachPoint=\"iconNode\" class=\"dijitTreeIcon\" waiRole=\"presentation\"\n\t\t\t><span dojoAttachPoint=\"labelNode\" class=\"dijitTreeLabel\" wairole=\"treeitem\" tabindex=\"-1\" waiState=\"selected-false\" dojoAttachEvent=\"onfocus:_onLabelFocus, onblur:_onLabelBlur\"></span>\n\t\t</span\n\t></div>\n\t<div dojoAttachPoint=\"containerNode\" class=\"dijitTreeContainer\" waiRole=\"presentation\" style=\"display: none;\"></div>\n</div>\n"),
    widgetsInTemplate: true,

    // return the dijit.Checkbox inside the tree node
    getNodeCheckbox: function(){
		if (this._supportingWidgets.length > 0) {
			return this._supportingWidgets[0];
		}
		return null;
    },
      
    setNodeCheckboxValue: function(value){  
        this.getNodeCheckbox().attr('checked',value);
    },
    setNodeScaleLabelState: function(){  
         if (!this.tree.model.store.hasAttribute(this.item, 'legImage')) {
               var lbl = this.labelNode;
 			    if ((this.tree.model.store.hasAttribute(this.item, 'isinscale')) && (lbl.style))  {
			       var tsVl = this.tree.model.store.getValue(this.item, 'isinscale');
			       if (!tsVl) { 
			               lbl.style.color = "gray"; 
			        } else { 
			               lbl.style.color = "black"; 
			        }
	           }
		}
    },
    hideCheckbox: function(){  
         
        this.getNodeCheckbox().attr('style','display:none');
    },
    
    setNodeIconValue: function(value){  
	 var iiccn = this.iconNode;
	 if (iiccn)
	    iiccn.src = value;
    },
    postCreate: function(){
		this.inherited(arguments);

        // preload
        // get value from the store (JSON) of the property "checked" and set the checkbox
		//  store.isItem to exclude dummy root in case of ForestStoreModel
		if (this.tree.model.store.isItem(this.item)) {
			if (!this.tree.model.store.hasAttribute(this.item, 'checked')) {
				// valore di default
				this.tree.model.store.setValue(this.item,'checked', false);
			} else {
				var attrValue = this.tree.model.store.getValue(this.item, 'checked');
				var val = (attrValue === true || (attrValue.toLowerCase && attrValue.toLowerCase() === 'true'));		
				this.setNodeCheckboxValue(val) ;
			}
			if (!this.tree.model.store.hasAttribute(this.item, 'legImage')) {
			     // valore di default
			     //this.tree.model.store.setValue(this.item,'checked', false);
			    // These are raster, or non-feature layers 
			     //var attrValue = '../msresources/images/folderClosed.gif'; 
			    // this.setNodeIconValue(attrValue) ; 
               var lbl = this.labelNode;
 			    if ((this.tree.model.store.hasAttribute(this.item, 'isinscale')) && (lbl.style))  {
			       var tsVl = this.tree.model.store.getValue(this.item, 'isinscale');
			       if (!tsVl) { 
			               lbl.style.color = "gray"; 
			        } else { 
			                 lbl.style.color = "black"; 
			        }
	           }

			} else {
			   var attrValue = this.tree.model.store.getValue(this.item, 'legImage');
			   //var val = (attrValue === true || (attrValue.toLowerCase && attrValue.toLowerCase() === 'true'));		
			   if (attrValue != "") 
			       this.setNodeIconValue(attrValue) ;
			}
			
			if (this.tree.model.store.hasAttribute(this.item, 'toctype')) {
			   var ttValue = this.tree.model.store.getValue(this.item, 'toctype');
			    if (ttValue == "class") {
		            this.hideCheckbox();
			   
			   } 
			}
		}
		
		// if setCheckboxOnClick is true
		if (this.tree.setCheckboxOnClick == false) {
			// connect onChange of the checkbox to alter the model of the tree
			dojo.connect(this.getNodeCheckbox(),'onChange',this,
					function(){
						var checkboxWidget = this.getNodeCheckbox();
						var checkboxValue = checkboxWidget.attr('value');
						this.tree.model.store.setValue(this.item,"checked",(checkboxValue == false ? false : true));
						this.setNodeScaleLabelState();
						//console.log('onChange checkbox on ' + this.tree.model.getIdentity(this.item));
					});
		}
	
    },
          
    getCheckedNodesList: function(nodeArray){
        if ( this.getNodeCheckbox().isChecked() ) {
            nodeArray.push(this.item.label);
		}
            
        this.getChildren().forEach(getCheckedNodesList(nodeArray), this);            
    }            
       
});

dojo.provide("my_ext.CheckboxTree");

dojo.declare(
"my_ext.CheckboxTree",
[dijit.Tree],
{
	// setCheckboxOnClick: Boolean
	//		If true, clicking a node will change the checkbox state
	//		If true, openOnClick will be set to false
	setCheckboxOnClick: false,
	
	postMixInProperties: function(){
		
		if (this.setCheckboxOnClick == true) {
			// if setCheckboxOnClick is true, openOnClick must be false
			openOnClick = false;
			
			dojo.connect(this, "onClick", this, function(/* dojo.data */ item, /*TreeNode*/ node, /*Event*/ evt) {
				var isChecked = this.model.store.getValue(item, 'checked');
				isChecked = (isChecked === true || (isChecked.toLowerCase && isChecked.toLowerCase() === 'true'));				
				this.model.store.setValue(item, 'checked', !isChecked);
				
				// log
				//console.log('onClick on ' + this.model.store.getValue(item, 'name'));
			});
		}

		this.inherited(arguments);
	},

    _createTreeNode: function(/*Object*/ args){
		// summary:
		//		creates a TreeNode
		// description:
		//		Developers can override this method to define their own TreeNode class;
		//		However it will probably be removed in a future release in favor of a way
		//		of just specifying a widget for the label, rather than one that contains
		//		the children too.
		return new my_ext.CheckboxTreeNode(args);
	},
	
    _onItemChange: function(/*Item*/ item){
        this.inherited(arguments);
		
		//---
        var model = this.model,
			identity = model.getIdentity(item),
			nodes = this._itemNodesMap[identity];

        if(nodes){
			var newValue = this.model.store.getValue(item,"checked");
			dojo.forEach(nodes,function(node){
				node.setNodeCheckboxValue(newValue);
			});
		}
    }

});


dojo.provide("my_ext.CheckboxTreeStoreModel");

dojo.declare(
"my_ext.CheckboxTreeStoreModel",
[dijit.tree.TreeStoreModel],
{
	onChange: function(/*dojo.data.Item*/ item){
		var currStore = this.store;
		var newValue = currStore.getValue(item,"checked");

		this.getChildren(item,function(children){      
			dojo.forEach(children,function(child){
				currStore.setValue(child,"checked",newValue);
			});
		});    
	}
});


dojo.provide("my_ext.CheckboxForestStoreModel");

dojo.declare(
"my_ext.CheckboxForestStoreModel",
[dijit.tree.ForestStoreModel],
{
	onChange: function(/*dojo.data.Item*/ item){
		var currStore = this.store;
		var newValue = currStore.getValue(item,"checked");

		this.getChildren(item,function(children){      
			dojo.forEach(children,function(child){
				currStore.setValue(child,"checked",newValue);
			});
		});    
	}
});
