summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/grid/enhanced/plugins/GridSource.xd.js
blob: 4398814e084c99d6feb508212b5196d8f56b0734 (plain)
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
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.grid.enhanced.plugins.GridSource"],
["require", "dojo.dnd.Source"],
["require", "dojox.grid.enhanced.plugins.DnD"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.grid.enhanced.plugins.GridSource"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.grid.enhanced.plugins.GridSource"] = true;
dojo.provide("dojox.grid.enhanced.plugins.GridSource");

dojo.require("dojo.dnd.Source");
dojo.require("dojox.grid.enhanced.plugins.DnD");

(function(){
var _joinToArray = function(arrays){
	var a = arrays[0];
	for(var i = 1; i < arrays.length; ++i){
		a = a.concat(arrays[i]);
	}
	return a;
};
dojo.declare("dojox.grid.enhanced.plugins.GridSource", dojo.dnd.Source, {
	// summary:
	//		A special source that can accept grid contents.
	//		Only for non-grid widgets or domNodes.
	accept: ["grid/cells", "grid/rows", "grid/cols", "text"],
	
	// insertNodesForGrid:
	//		If you'd like to insert some sort of nodes into your dnd source, turn this on,
	//		and override getCellContent/getRowContent/getColumnContent
	//		to populate the dnd data in your desired format.
	insertNodesForGrid: false,
	
	markupFactory: function(params, node){
		return new dojox.grid.enhanced.plugins.GridSource(node, params);
	},
	checkAcceptance: function(source, nodes){
		if(source instanceof dojox.grid.enhanced.plugins.GridDnDSource){
			if(nodes[0]){
				var item = source.getItem(nodes[0].id);
				if(item && (dojo.indexOf(item.type, "grid/rows") >= 0 || dojo.indexOf(item.type, "grid/cells") >= 0) &&
					!source.dndPlugin._allDnDItemsLoaded()){
					return false;
				}
			}
			this.sourcePlugin = source.dndPlugin;
		}
		return this.inherited(arguments);
	},
	onDraggingOver: function(){
		if(this.sourcePlugin){
			this.sourcePlugin._isSource = true;
		}
	},
	onDraggingOut: function(){
		if(this.sourcePlugin){
			this.sourcePlugin._isSource = false;
		}
	},
	onDropExternal: function(source, nodes, copy){
		if(source instanceof dojox.grid.enhanced.plugins.GridDnDSource){
			var ranges = dojo.map(nodes, function(node){
				return source.getItem(node.id).data;
			});
			var item = source.getItem(nodes[0].id);
			var grid = item.dndPlugin.grid;
			var type = item.type[0];
			var range;
			try{
				switch(type){
					case "grid/cells":
						nodes[0].innerHTML = this.getCellContent(grid, ranges[0].min, ranges[0].max) || "";
						this.onDropGridCells(grid, ranges[0].min, ranges[0].max);
						break;
					case "grid/rows":
						range = _joinToArray(ranges);
						nodes[0].innerHTML = this.getRowContent(grid, range) || "";
						this.onDropGridRows(grid, range);
						break;
					case "grid/cols":
						range = _joinToArray(ranges);
						nodes[0].innerHTML = this.getColumnContent(grid, range) || "";
						this.onDropGridColumns(grid, range);
						break;
				}
				if(this.insertNodesForGrid){
					this.selectNone();
					this.insertNodes(true, [nodes[0]], this.before, this.current);
				}
				item.dndPlugin.onDragOut(!copy);
			}catch(e){
				console.warn("GridSource.onDropExternal() error:",e);
			}
		}else{
			this.inherited(arguments);
		}
	},
	getCellContent: function(grid, leftTopCell, rightBottomCell){
		// summary:
		//		Fill node innerHTML for dnd grid cells.
		// sample code:
		//		var cells = grid.layout.cells;
		//		var store = grid.store;
		//		var cache = grid._by_idx;
		//		var res = "Grid Cells from " + grid.id + ":<br/>";
		//		for(var r = leftTopCell.row; r <= rightBottomCell.row; ++r){
		//			for(var c = leftTopCell.col; c <= rightBottomCell.col; ++c){
		//				res += store.getValue(cache[r].item, cells[c].field) + ", ";
		//			}
		//			res = res.substring(0, res.length - 2) + ";<br/>";
		//		}
		//		return res;
	},
	getRowContent: function(grid, rowIndexes){
		// summary:
		//		Fill node innerHTML for dnd grid rows.
		// sample code:
		//		var cells = grid.layout.cells;
		//		var store = grid.store;
		//		var cache = grid._by_idx;
		//		var res = "Grid Rows from " + grid.id + ":<br/>";
		//		for(var i = 0; i < rowIndexes.length; ++i){
		//			var r = rowIndexes[i];
		//			res += "Row " + r + ": ";
		//			for(var j = 0; j < cells.length; ++j){
		//				if(!cells[j].hidden){
		//					res += store.getValue(cache[r].item, cells[j].field) + ", ";
		//				}
		//			}
		//			res = res.substring(0, res.length - 2) + ";<br/>";
		//		}
		//		return res;
	},
	getColumnContent: function(grid, colIndexes){
		// summary:
		//		Fill node innerHTML for dnd grid columns.
		// sample code:
		//		var cells = grid.layout.cells;
		//		var res = "Grid Columns from " + grid.id + ":";
		//		for(var i = 0; i < colIndexes.length; ++i){
		//			var c = colIndexes[i];
		//			res += (cells[c].name || cells[c].field) + ", ";
		//		}
		//		return res.substring(0, res.length - 2);
	},
	onDropGridCells: function(grid, leftTopCell, rightBottomCell){
		
	},
	onDropGridRows: function(grid, rowIndexes){
		
	},
	onDropGridColumns: function(grid, colIndexes){
		
	}
});
})();

}

}};});