jquery.checktree_yctin.noconflict.js 20 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/**
 * CheckTree(ycTIN Edition)  v0.1 (jQuery Plugins)
 *
 * @author Timmy Tin (ycTIN)
 * @license GPL
 * @version 0.1
 * @copyright Timmy Tin (ycTIN)
 * @website http://project.yctin.com/checktree/
 *
 * this plugins is improved CheckTree v0.2, Project 
 * Website: http://static.geewax.org/checktree/ 
 * Author: JJ Geewax <jj@geewax.org>
 * 
 * @edit by: James
 * @edit date: 2010-06-07
 */
// Create a closure
(function(jQuery){
	// Your base, I'm in it!
   /* 



		jQuery.fn['original'+methods+'Method'] = jQuery.fn.addClass;

		jQuery.fn.addClass = function(){
			// Execute the original method.
			var result = jQuery.fn.originalAddClassMethod.apply( this, arguments );

			// trigger a custom event
			jQuery(this).trigger('cssClassChanged');

			// return the original result
			return result;
		}
*/
    var methods = ['addClass', 'toggleClass', 'removeClass'];
	$.each(methods, function (index, method) {
		var originalMethod = $.fn[method];

		$.fn[method] = function () {
			 //var oldClass = this[0].className;
			  var result = originalMethod.apply(this, arguments);
			  //var newClass = this[0].className;

			  this.trigger("on"+method);

			  return result;
		};
	});
})(jQuery);

(function(jQuery){
    jQuery.fn.checkTree = function(settings){
        settings = jQuery.extend({            
			/* Callbacks
             The callbacks should be functions that take one argument. The checkbox tree
             will return the jQuery wrapped LI element of the item that was checked/expanded.
             */
            onExpand: null,
            onCollapse: null,
            onCheck: null,
            onUnCheck: null,
            onHalfCheck: null,
            onLabelHoverOver: null,
            onLabelHoverOut: null,
			
            /* Valid choices: 'expand', 'check' */
            labelAction: "check",
            attrHalfChecked: "checked",
            collapseAll: false,
            debug: false
        }, settings);

        var $tree = this;

        $tree.clear = function(){
            $tree.find(":checkbox").attr("checked", "");
        };

		$tree.getValue = function(){
			var checked = [];
			$tree.find(":checkbox").filter(":checked").each(function(){
				checked.push(jQuery(this).val());
			});
			return checked.join(',');
		};

		$tree.setValue = function(fieldValues){
			/*
			$field = $tree.find(":checkbox").each(function(){
				var value = jQuery(this).val();
				if($.inArray(value, fieldValue)!=-1){
					jQuery(this).attr('checked','true');
				}else{
					jQuery(this).attr('checked','');
				}
			});*/
			
			$tree.find(":checkbox").val(fieldValues);
			
		};

        $tree.update = function(){
            $tree.find("li").each(function(){
                var $checkbox = jQuery(this).children('.checkbox');
                if (jQuery(this).children(':checkbox').attr("checked")) 
                    $checkbox.addClass('checked');
                else {
                    $checkbox.removeClass('checked');
                }
            });
            $tree.find("li").each(function(){
                if (jQuery(this).is(":has(ul)")) {
                    var $checkbox = jQuery(this).children('.checkbox');
                    var $input_checkbox = jQuery(this).contents().find('.checkbox');
                    var $input_checkbox_checked = jQuery(this).contents().find('.checked');
                    if ($input_checkbox_checked.length == 0) {
                        $checkbox.removeClass("checked half_checked").siblings(":checkbox").attr("checked", "");
                    } else if ($input_checkbox.length == $input_checkbox_checked.length) {
                        $checkbox.removeClass("half_checked").addClass('checked').siblings(":checkbox").attr("checked", "checked");
                    } else {
                        $checkbox.removeClass("checked").addClass('half_checked').siblings(":checkbox").attr("checked", settings.attrHalfChecked);
                    }
                }
            });
        };

		//-------------------------------------------------------------

        $tree.find("li").find(":checkbox").change(function(){
            var $all = jQuery(this).siblings("ul").find(":checkbox");
            var $checked = $all.filter(":checked");
            if ($all.length == $checked.length) {
                jQuery(this).attr("checked", "checked").siblings(".checkbox").removeClass("half_checked").addClass("checked");
                if (settings.onCheck) 
                    settings.onCheck(jQuery(this).parent());
            } else if ($checked.length == 0) {
                jQuery(this).attr("checked", "").siblings(".checkbox").removeClass("checked").removeClass("half_checked");
                if (settings.onUnCheck) 
                    settings.onUnCheck(jQuery(this).parent());
            } else {
                if (settings.onHalfCheck && !jQuery(this).siblings(".checkbox").hasClass("half_checked")) 
                    settings.onHalfCheck(jQuery(this).parent());
                jQuery(this).attr("checked", settings.attrHalfChecked).siblings(".checkbox").removeClass("checked").addClass("half_checked");
            }
        }).hide().end().each(function(){
            if (settings.collapseAll) {
                jQuery(this).find("ul").each(function(){
                    if (!jQuery(this).siblings(".expanded").length) 
                        jQuery(this).hide();
                });
            }
            var $label = jQuery(this).children("label").clone();
			var $inputCheckbox = jQuery(this).children(":checkbox");
            var $checkbox = jQuery('<div class="checkbox"></div>');
            var $arrow = jQuery('<div class="arrow"></div>');
            if (jQuery(this).is(":has(ul)")) {
                //if (jQuery(this).children("ul").is(":hidden")){ // bug when container was hiden
				if (jQuery(this).children("ul").css("diaplay")== "hidden"){	
                    $arrow.addClass("collapsed");
                } else{
                    $arrow.addClass("expanded");
				}
                $arrow.click(function(){
                    jQuery(this).siblings("ul").toggle();
                    if (jQuery(this).hasClass("collapsed")) {
                        jQuery(this).addClass("expanded").removeClass("collapsed");
                        if (settings.onExpand){
                            settings.onExpand(jQuery(this).parent());
						}
                    } else {
                        jQuery(this).addClass("collapsed").removeClass("expanded");
                        if (settings.onCollapse){
                            settings.onCollapse(jQuery(this).parent());
						}
                    }
                });
            }
            $checkbox.click(function(){
                jQuery(this).toggleClass("checked").removeClass("half_checked");
				jQuery(this).siblings(":checkbox").attr("checked", (jQuery(this).hasClass("checked") ? "checked" : ""));
                if (jQuery(this).hasClass("checked")) {
                    if (settings.onCheck) 
                        settings.onCheck(jQuery(this).parent());
                    jQuery(this).siblings("ul").find(".checkbox").not(".checked").removeClass("half_checked").addClass("checked").each(function(){
                        if (settings.onCheck) 
                            settings.onCheck(jQuery(this).parent());
                    }).siblings(":checkbox").attr("checked", "checked");
                } else {
                    if (settings.onUnCheck) 
                        settings.onUnCheck(jQuery(this).parent());
                    jQuery(this).siblings("ul").find(".checkbox").removeClass("half_checked").filter(".checked").each(function(){
                        if (settings.onUnCheck) 
                            settings.onUnCheck(jQuery(this).parent());
						jQuery(this).removeClass("checked");
                    }).siblings(":checkbox").attr("checked", "");
                }
                jQuery(this).parents("ul").siblings(":checkbox").change();
            });
            if (jQuery(this).children('.checkbox').hasClass('checked')) 
                $checkbox.addClass('checked');
            else if (jQuery(this).children(':checkbox').attr("checked")) {
                $checkbox.addClass('checked');
                jQuery(this).parents("ul").siblings(":checkbox").change();
            } else if (jQuery(this).children('.checkbox').hasClass('half_checked')) 
                $checkbox.addClass('half_checked');
            jQuery(this).children(".arrow").remove();
            jQuery(this).children(".checkbox").remove();
            jQuery(this).children("label").remove();
			if (jQuery(this).is(":has(ul)")){
				jQuery(this).prepend($label).prepend($checkbox).prepend($arrow);
			}else{
				jQuery(this).prepend($label).prepend($checkbox);
			}
        }).find("label").click(function(){
            switch (settings.labelAction) {
                case 'expand':
                    jQuery(this).siblings(".arrow").click();
                    break;
                case 'check':
                    jQuery(this).siblings(".checkbox").click();
                    break;
            }
        }).hover(function(){
            jQuery(this).addClass("hover");
            if (settings.onLabelHoverOver) 
                settings.onLabelHoverOver(jQuery(this).parent());
        }, function(){
            jQuery(this).removeClass("hover");
            if (settings.onLabelHoverOut) 
                settings.onLabelHoverOut(jQuery(this).parent());
        }).end();
        return $tree;
    };
})(jQuery);


	(function(jQuery){
		jQuery.fn.pagePermission = function(settings){
			settings = jQuery.extend({
				defaultPermission : {
					"PermisstionSave" : "0",
					"PermisstionEdit" : "0",
					"PermisstionDelete" : "0"
				}
			} , settings);
			var $permission = this;
			$permission.getPermission = function(){
				var mapData = {};
				$permission.find(".checkbox.permis_action_object").each(function(){
					var self = $(this) , permis = null;
					var key = self.attr("keycode");
					if(mapData[key]){
						permis = mapData[key];
					}else{
						permis = jQuery.extend({} , settings.defaultPermission);
						mapData[key] = permis;
					}
					if( self.hasClass("checked") && !self.hasClass("disabled") ){
						permis[self.attr("action_type")] = 1;
					}
				});
				return mapData;
			}
			$permission.getPermissionString = function(){
				return JSON.stringify($permission.getPermission()).replace(/"/g, "'");
			}
			var datasource = {};
			if( settings && settings.input_datasource ){
				var strdata = settings.input_datasource.val();
				if ( !(typeof strdata !== "string" || !strdata )) {
					strdata = jQuery.trim( strdata ).replace(/'/g, '"');
					datasource = window.JSON.parse( strdata );
				}
			}

			$permission.find("li").find(":checkbox").each(function(){
				var self = jQuery(this);
				var parent = self.parent();
				var checkBox = parent.find(".checkbox");
				checkBox.attr("action_type" , "view");
				if(self.attr("activepermis") == "1"){
					
					var chkChild = parent.find("ul :checkbox");
					if( chkChild.length == 0 ){
						var objHtml = jQuery(createCheckListPermission(self));

						checkBox.click(function(){
							if(checkBox.hasClass("checked")){
								objHtml.find(".checkbox").each(function(){
									var selfCheck = jQuery(this);
									//selfCheck.addClass("checked");  // <---͡ view   permiss  ʤҷ --->
									handleVertical(selfCheck.closest("ul") , selfCheck.attr("action_type"));
								});
							}else{
								objHtml.find(".checkbox").each(function(){
									var selfCheck = jQuery(this);
									selfCheck.removeClass("checked");
									handleVertical(selfCheck.closest("ul") , selfCheck.attr("action_type"));
								});
							}

						});
						objHtml.find(".checkbox").click(function(){
							var selfCheck = jQuery(this);
							if(checkBox.hasClass("checked")){
								selfCheck.toggleClass("checked");
							}
							handleVertical(selfCheck.closest("ul") , selfCheck.attr("action_type"));

							/*jQuery(this).closest("ul").children("li.head-control").find("[action_type="+jQuery(this).attr("action_type")+"]").change(function(){
								var $all = jQuery(this).siblings("ul").find(":checkbox");
								var $checked = $all.filter(":checked");
								if ($all.length == $checked.length) {
									jQuery(this).attr("checked", "checked").siblings(".checkbox").removeClass("half_checked").addClass("checked");
									if (settings.onCheck) 
										settings.onCheck(jQuery(this).parent());
								} else if ($checked.length == 0) {
									jQuery(this).attr("checked", "").siblings(".checkbox").removeClass("checked").removeClass("half_checked");
									if (settings.onUnCheck) 
										settings.onUnCheck(jQuery(this).parent());
								} else {
									if (settings.onHalfCheck && !jQuery(this).siblings(".checkbox").hasClass("half_checked")) 
										settings.onHalfCheck(jQuery(this).parent());
									jQuery(this).attr("checked", settings.attrHalfChecked).siblings(".checkbox").removeClass("checked").addClass("half_checked");
								}
							});*/
						});
						
						objHtml.insertAfter(checkBox);

						objHtml.find(".checkbox.permis_action_object").each(function(){
							var selfChk = jQuery(this);
							var permis = datasource[selfChk.attr("keycode")];
							if( permis ){
								if(permis[selfChk.attr("action_type")] == 1){
									selfChk.addClass("checked");
									handleVertical(selfChk.closest("ul") , selfChk.attr("action_type"));
								}
							}
						});
						objHtml.closest("li").children(".checkbox[action_type=view][checktype!=head]").bind("onaddClass ontoggleClass onremoveClass" , function(){
							var selfChk = jQuery(this);
							handleVertical(selfChk.closest("ul") , selfChk.attr("action_type"));
						});
						
						var ulnode = parent.parent();
						if( ulnode.children("li.head-control").length == 0 ){
							var objHtmlHead = jQuery(createHeadCheckListPermission());
							objHtmlHead.find(".checkbox").click(function(){
								var self = jQuery(this);
								var isHandleChild = false;
								if( self.is("[action_type=view]") ){
									self.toggleClass("checked").removeClass("half_checked");
									isHandleChild = true;
									var isChecked = self.hasClass("checked");
									self.closest("li").siblings().find(".checkbox[action_type="+self.attr("action_type")+"]").each(function(){
										var selfChild = jQuery(this);
										
										if( isChecked ){
											selfChild.addClass("checked");
											selfChild.siblings(":checkbox").attr("checked" , "checked");
										}else{
											selfChild.removeClass("checked").siblings(":checkbox").attr("checked" , "");
										}
										selfChild.parent().find(".checkbox[action_type!=view]").each(function(){
											var selfChkPermis = jQuery(this);
											if( isChecked ){
												//selfChkPermis.addClass("checked") // <---͡ view  Ƿ head лԴ  --->
											}else{
												selfChkPermis.removeClass("checked")
											}
											handleVertical(selfChkPermis.closest("ul") , selfChkPermis.attr("action_type"));
										});
									});
								
									handleVertical(self.closest("ul") , self.attr("action_type"));
									self.parents("ul").siblings(":checkbox").change();
								}else{
									var viewSelf = self.siblings("[action_type=view]");
									if( viewSelf.hasClass("checked") || viewSelf.hasClass("half_checked")){
										self.toggleClass("checked").removeClass("half_checked");
										isHandleChild = true;
										if(self.hasClass("checked")){
											self.closest("li").siblings().find(".checkbox[action_type="+self.attr("action_type")+"]").each(function(){
												var selfChild = jQuery(this);
												if( selfChild.attr("action_type") != "view" && selfChild.parent().siblings("[action_type=view]").hasClass("checked")){
													selfChild.addClass("checked");
												}
											});
										}else{
											self.closest("li").siblings().find(".checkbox[action_type="+self.attr("action_type")+"]").removeClass("checked");
										}
										
									}
								}
								
								/*if( isHandleChild ){
									var root = self.closest("ul").siblings(".checkbox");
									if(self.hasClass("checked")){
										self.closest("li").siblings().find(".checkbox[action_type="+self.attr("action_type")+"]").each(function(){
											var selfChild = $(this);
											if( selfChild.attr("action_type") == "view" ){
												selfChild.addClass("checked");
												root.addClass("checked");
											}else	if( selfChild.attr("action_type") != "view" && selfChild.parent().siblings("[action_type=view]").hasClass("checked")){
												selfChild.addClass("checked");
											}
										});
									}else{
										self.closest("li").siblings().find(".checkbox[action_type="+self.attr("action_type")+"]").removeClass("checked");
									}
									handleVertical(self.closest("ul") , self.attr("action_type"));
	//								jQuery(root).parents("ul").siblings(":checkbox").change();
								}*/
							});
							ulnode.prepend(objHtmlHead);
						}
					}
				}
			});
			function handleVertical(rootElement , type){
				var typeFilter = "[action_type="+type+"]";
				var checkTarget = rootElement.children("li.head-control").find(typeFilter);
				var checkAllAmount = rootElement.find(".checkbox[checktype!=head]"+typeFilter).length;
				var checkedAmount = rootElement.find(".checkbox.checked[checktype!=head]"+typeFilter).length;
				if( checkAllAmount == checkedAmount ){
					checkTarget.removeClass("half_checked").addClass("checked");
				}else if(checkedAmount == 0){
					checkTarget.removeClass("checked").removeClass("half_checked");
				}else{
					checkTarget.addClass("half_checked").removeClass("checked");
				}
			}
			function handleHorizontal(rootElement){

			}
			function createCheckListPermission(element){
				var value;
				if( element instanceof jQuery ){
					value = element.val();
				}else{
					value = element.value;
				}
				var statusEdit = element.attr("edit")==0?"disabled":"checkbox";
				var statusSave = element.attr("save")==0?"disabled":"checkbox";
				var statusDelete = element.attr("delete")==0?"disabled":"checkbox";
				//var html = "";
				var html = "<span class=\"permis_action\">";
					html += "<label class=\"desc-head\"></label>";
					html += "<div class=\"checkbox permis_action_object "+statusEdit+" \" action_type=\"PermisstionEdit\" keycode=\""+value+"\"></div>";
					//html += "<input type=\"hidden\" name=\"__permis\" id=\"permis_e_"+value+"\" action_type=\"edit\" value=\""+value+"\" class=\"permis_action_object\">&nbsp;";
					html += "<label class=\"desc-head\"></label>";
					html += "<div class=\"checkbox permis_action_object "+statusSave+" \" action_type=\"PermisstionSave\" keycode=\""+value+"\"></div>";
					//html += "<input type=\"hidden\" name=\"__permis\" id=\"permis_d_"+value+"\" action_type=\"add\" value=\""+value+"\" class=\"permis_action_object\">&nbsp;";
					html += "<label class=\"desc-head\"></label>";
					html += "<div class=\"checkbox permis_action_object "+statusDelete+" \" action_type=\"PermisstionDelete\" keycode=\""+value+"\"></div>";
					//html += "<input type=\"hidden\" name=\"__permis\" id=\"permis_a_"+value+"\" upid=\"\" action_type=\"delete\" value=\""+value+"\" class=\"permis_action_object\">&nbsp;";
					html += "</span>";
				 return html;
			}
			function createHeadCheckListPermission(){
				var html = "<li class=\"head-control\">";
				html += "<span class=\"permis_action\">";
				html += "<div class=\"checkbox\" action_type=\"view\" checktype=\"head\"></div>";
				html += "<label class=\"desc-head\">"+swap2String("View")+"</label>";
				html += "<div class=\"checkbox\" action_type=\"PermisstionEdit\" checktype=\"head\"></div>";
				html += "<label class=\"desc-head\">"+swap2String("Update")+"</label>";
				html += "<div class=\"checkbox\" action_type=\"PermisstionSave\" checktype=\"head\"></div>";
				html += "<label class=\"desc-head\">"+swap2String("Insert")+"</label>";
				html += "<div class=\"checkbox\" action_type=\"PermisstionDelete\" checktype=\"head\"></div>";
				html += "<label class=\"desc-head\">"+swap2String("Delete")+"</label>";
				html += "</span></li>";
				 return html;
			}
			return $permission;
		}
	})(jQuery);