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