Blame view

src/main/webapp/libs/jquery-easyui-1.4.1/extension/jquery-easyui-etree/jquery.etree.js 4.35 KB
caiyongsong 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
/**
 * etree - jQuery EasyUI
 * 
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2011 stworthy [ stworthy@gmail.com ] 
 * 
 * Dependencies:
 *   tree
 *   messager
 * 
 */
(function($){
	function createTree(target){
		var opts = $.data(target, 'etree').options;
		
		$(target).tree($.extend({}, opts, {
			onDblClick: function(node){
				$(this).tree('beginEdit', node.target);
			},
			onBeforeEdit: function(node){
				if (opts.onBeforeEdit.call(target, node) == false) return false;
				$(this).tree('disableDnd');
			},
			onAfterEdit: function(node){
				$.ajax({
					url: opts.updateUrl,
					type: 'post',
					dataType: 'json',
					data: {
						id: node.id,
						text: node.text
					}
				});
				$(this).tree('enableDnd');
				opts.onAfterEdit.call(target, node);
			},
			onCancelEdit: function(node){
				$(this).tree('enableDnd');
				opts.onCancelEdit.call(target, node);
			},
			onDrop: function(targetNode, source, point){
				var targetId = $(target).tree('getNode', targetNode).id;
				$.ajax({
					url: opts.dndUrl,
					type: 'post',
					dataType: 'json',
					data: {
						id: source.id,
						targetId: targetId,
						point: point
					}
				});
				opts.onDrop.call(target, targetNode, source, point);
			}
		}));
	}
	
	$.fn.etree = function(options, param){
		if (typeof options == 'string'){
			var method = $.fn.etree.methods[options];
			if (method){
				return method(this, param);
			} else {
				return this.tree(options, param);
			}
		}
		
		options = options || {};
		return this.each(function(){
			var state = $.data(this, 'etree');
			if (state){
				$.extend(state.options, options);
			} else {
				$.data(this, 'etree', {
					options: $.extend({}, $.fn.etree.defaults, $.fn.etree.parseOptions(this), options)
				});
			}
			createTree(this);
		});
	};
	
	$.fn.etree.methods = {
		options: function(jq){
			return $.data(jq[0], 'etree').options;
		},
		create: function(jq){
			return jq.each(function(){
				var opts = $.data(this, 'etree').options;
				var tree = $(this);
				var node = tree.tree('getSelected');
				$.ajax({
					url: opts.createUrl,
					type: 'post',
					dataType: 'json',
					data: {
						parentId: (node ? node.id : 0)
					},
					success: function(data){
						tree.tree('append', {
							parent: (node ? node.target : null),
							data: [data]
						});
					}
				});
			});
		},
		edit: function(jq){
			return jq.each(function(){
				var opts = $.data(this, 'etree').options;
				var node = $(this).tree('getSelected');
				if (node){
					$(this).tree('beginEdit', node.target);
				} else {
					$.messager.show({
						title:opts.editMsg.norecord.title,
						msg:opts.editMsg.norecord.msg
					});
				}
			});
		},
		destroy: function(jq){
			return jq.each(function(){
				var opts = $.data(this, 'etree').options;
				var tree = $(this);
				var node = tree.tree('getSelected');
				if (node){
					$.messager.confirm(opts.destroyMsg.confirm.title,opts.destroyMsg.confirm.msg, function(r){
						if (r){
							if (opts.destroyUrl){
								$.post(opts.destroyUrl, {id:node.id}, function(){
									tree.tree('remove', node.target);
								});
							} else {
								tree.tree('remove', node.target);
							}
						}
					});
				} else {
					$.messager.show({
						title:opts.destroyMsg.norecord.title,
						msg:opts.destroyMsg.norecord.msg
					});
				}
			});
		}
	};
	
	$.fn.etree.parseOptions = function(target){
		var t = $(target);
		return $.extend({}, $.fn.tree.parseOptions(target), {
			createUrl: (t.attr('createUrl') ? t.attr('createUrl') : undefined),
			updateUrl: (t.attr('updateUrl') ? t.attr('updateUrl') : undefined),
			destroyUrl: (t.attr('destroyUrl') ? t.attr('destroyUrl') : undefined),
			dndUrl: (t.attr('dndUrl') ? t.attr('dndUrl') : undefined)
		});
	};
	
	$.fn.etree.defaults = $.extend({}, $.fn.tree.defaults, {
		editMsg:{
			norecord:{
				title:'Warning',
				msg:'No node is selected.'
			}
		},
		destroyMsg:{
			norecord:{
				title:'Warning',
				msg:'No node is selected.'
			},
			confirm:{
				title:'Confirm',
				msg:'Are you sure you want to delete?'
			}
		},
	
		dnd:true,
		url:null,	// return tree data
		createUrl:null,	// post parentId, return the created node data{id,text,...}
		updateUrl:null,	// post id,text, return updated node data.
		destroyUrl:null,	// post id, return {success:true}
		dndUrl:null	// post id,targetId,point, return {success:true}
	});
})(jQuery);