JsonView_wev8.js
9.43 KB
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
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.JsonView
* @extends Ext.View
* Shortcut class to create a JSON + {@link Ext.Updater} template view. Usage:
<pre><code>
var view = new Ext.JsonView("my-element",
'<div id="{id}">{foo} - {bar}</div>', // auto create template
{ multiSelect: true, jsonRoot: "data" }
);
// listen for node click?
view.on("click", function(vw, index, node, e){
alert('Node "' + node.id + '" at index: ' + index + " was clicked.");
});
// direct load of JSON data
view.load("foobar.php");
// Example from my blog list
var tpl = new Ext.Template(
'<div class="entry">' +
'<a class="entry-title" href="{link}">{title}</a>' +
"<h4>{date} by {author} | {comments} Comments</h4>{description}" +
"</div><hr />"
);
var moreView = new Ext.JsonView("entry-list", tpl, {
jsonRoot: "posts"
});
moreView.on("beforerender", this.sortEntries, this);
moreView.load({
url: "/blog/get-posts.php",
params: "allposts=true",
text: "Loading Blog Entries..."
});
</code></pre>
* @constructor
* Create a new JsonView
* @param {Mixed} container The container element where the view is to be rendered.
* @param {Template} tpl The rendering template
* @param {Object} config The config object
*/
Ext.JsonView = function(container, tpl, config){
Ext.JsonView.superclass.constructor.call(this, container, tpl, config);
var um = this.el.getUpdater();
um.setRenderer(this);
um.on("update", this.onLoad, this);
um.on("failure", this.onLoadException, this);
/**
* @event beforerender
* Fires before rendering of the downloaded JSON data.
* @param {Ext.JsonView} this
* @param {Object} data The JSON data loaded
*/
/**
* @event load
* Fires when data is loaded.
* @param {Ext.JsonView} this
* @param {Object} data The JSON data loaded
* @param {Object} response The raw Connect response object
*/
/**
* @event loadexception
* Fires when loading fails.
* @param {Ext.JsonView} this
* @param {Object} response The raw Connect response object
*/
this.addEvents({
'beforerender' : true,
'load' : true,
'loadexception' : true
});
};
Ext.extend(Ext.JsonView, Ext.View, {
/**
* The root property in the loaded JSON object that contains the data
* @type {String}
*/
jsonRoot : "",
/**
* Refreshes the view.
*/
refresh : function(){
this.clearSelections();
this.el.update("");
var html = [];
var o = this.jsonData;
if(o && o.length > 0){
for(var i = 0, len = o.length; i < len; i++){
var data = this.prepareData(o[i], i, o);
html[html.length] = this.tpl.apply(data);
}
}else{
html.push(this.emptyText);
}
this.el.update(html.join(""));
this.nodes = this.el.dom.childNodes;
this.updateIndexes(0);
},
/**
* Performs an async HTTP request, and loads the JSON from the response. If <i>params</i> are specified it uses POST, otherwise it uses GET.
* @param {Object/String/Function} url The URL for this request, or a function to call to get the URL, or a config object containing any of the following options:
<pre><code>
view.load({
url: "your-url.php",
params: {param1: "foo", param2: "bar"}, // or a URL encoded string
callback: yourFunction,
scope: yourObject, //(optional scope)
discardUrl: false,
nocache: false,
text: "Loading...",
timeout: 30,
scripts: false
});
</code></pre>
* The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
* are shorthand for <i>disableCaching</i>, <i>indicatorText</i> and <i>loadScripts</i> and are used to set their associated property on this Updater instance.
* @param {String/Object} params (optional) The parameters to pass, as either a URL encoded string "param1=1&param2=2" or an object {param1: 1, param2: 2}
* @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
* @param {Boolean} discardUrl (optional) By default when you execute an update the defaultUrl is changed to the last used URL. If true, it will not store the URL.
*/
load : function(){
var um = this.el.getUpdater();
um.update.apply(um, arguments);
},
render : function(el, response){
this.clearSelections();
this.el.update("");
var o;
try{
o = Ext.util.JSON.decode(response.responseText);
if(this.jsonRoot){
o = eval("o." + this.jsonRoot);
}
} catch(e){
}
/**
* The current JSON data or null
*/
this.jsonData = o;
this.beforeRender();
this.refresh();
},
/**
* Get the number of records in the current JSON dataset
* @return {Number}
*/
getCount : function(){
return this.jsonData ? this.jsonData.length : 0;
},
/**
* Returns the JSON object for the specified node(s)
* @param {HTMLElement/Array} node The node or an array of nodes
* @return {Object/Array} If you pass in an array, you get an array back, otherwise
* you get the JSON object for the node
*/
getNodeData : function(node){
if(Ext.isArray(node)){
var data = [];
for(var i = 0, len = node.length; i < len; i++){
data.push(this.getNodeData(node[i]));
}
return data;
}
return this.jsonData[this.indexOf(node)] || null;
},
beforeRender : function(){
this.snapshot = this.jsonData;
if(this.sortInfo){
this.sort.apply(this, this.sortInfo);
}
this.fireEvent("beforerender", this, this.jsonData);
},
onLoad : function(el, o){
this.fireEvent("load", this, this.jsonData, o);
},
onLoadException : function(el, o){
this.fireEvent("loadexception", this, o);
},
/**
* Filter the data by a specific property.
* @param {String} property A property on your JSON objects
* @param {String/RegExp} value Either string that the property values
* should start with, or a RegExp to test against the property
*/
filter : function(property, value){
if(this.jsonData){
var data = [];
var ss = this.snapshot;
if(typeof value == "string"){
var vlen = value.length;
if(vlen == 0){
this.clearFilter();
return;
}
value = value.toLowerCase();
for(var i = 0, len = ss.length; i < len; i++){
var o = ss[i];
if(o[property].substr(0, vlen).toLowerCase() == value){
data.push(o);
}
}
} else if(value.exec){ // regex?
for(var i = 0, len = ss.length; i < len; i++){
var o = ss[i];
if(value.test(o[property])){
data.push(o);
}
}
} else{
return;
}
this.jsonData = data;
this.refresh();
}
},
/**
* Filter by a function. The passed function will be called with each
* object in the current dataset. If the function returns true the value is kept,
* otherwise it is filtered.
* @param {Function} fn
* @param {Object} scope (optional) The scope of the function (defaults to this JsonView)
*/
filterBy : function(fn, scope){
if(this.jsonData){
var data = [];
var ss = this.snapshot;
for(var i = 0, len = ss.length; i < len; i++){
var o = ss[i];
if(fn.call(scope || this, o)){
data.push(o);
}
}
this.jsonData = data;
this.refresh();
}
},
/**
* Clears the current filter.
*/
clearFilter : function(){
if(this.snapshot && this.jsonData != this.snapshot){
this.jsonData = this.snapshot;
this.refresh();
}
},
/**
* Sorts the data for this view and refreshes it.
* @param {String} property A property on your JSON objects to sort on
* @param {String} direction (optional) "desc" or "asc" (defaults to "asc")
* @param {Function} sortType (optional) A function to call to convert the data to a sortable value.
*/
sort : function(property, dir, sortType){
this.sortInfo = Array.prototype.slice.call(arguments, 0);
if(this.jsonData){
var p = property;
var dsc = dir && dir.toLowerCase() == "desc";
var f = function(o1, o2){
var v1 = sortType ? sortType(o1[p]) : o1[p];
var v2 = sortType ? sortType(o2[p]) : o2[p];
;
if(v1 < v2){
return dsc ? +1 : -1;
} else if(v1 > v2){
return dsc ? -1 : +1;
} else{
return 0;
}
};
this.jsonData.sort(f);
this.refresh();
if(this.jsonData != this.snapshot){
this.snapshot.sort(f);
}
}
}
});