123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- define("dojox/data/CdfStore", ["dojo", "dojox", "dojo/data/util/sorter"], function(dojo, dojox) {
- dojox.data.ASYNC_MODE = 0;
- dojox.data.SYNC_MODE = 1;
- dojo.declare("dojox.data.CdfStore", null, {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- identity: "jsxid",
-
-
-
- url: "",
-
-
-
-
-
-
- xmlStr:"",
-
-
-
- data:null,
-
-
-
- label: "",
-
-
-
- mode:dojox.data.ASYNC_MODE,
-
- constructor: function(/* Object */ args){
-
-
-
- if(args){
- this.url = args.url;
- this.xmlStr = args.xmlStr || args.str;
- if(args.data){
- this.xmlStr = this._makeXmlString(args.data);
- }
- this.identity = args.identity || this.identity;
- this.label = args.label || this.label;
- this.mode = args.mode !== undefined ? args.mode : this.mode;
- }
- this._modifiedItems = {};
-
- this.byId = this.fetchItemByIdentity;
- },
-
-
- getValue: function(/* jsx3.xml.Entity */ item, /* String */ property, /* value? */ defaultValue){
-
-
-
- return item.getAttribute(property) || defaultValue;
- },
- getValues: function(/* jsx3.xml.Entity */ item, /* String */ property){
-
-
-
-
-
- var v = this.getValue(item, property, []);
- return dojo.isArray(v) ? v : [v];
- },
- getAttributes: function(/* jsx3.xml.Entity */ item){
-
-
-
- return item.getAttributeNames();
- },
- hasAttribute: function(/* jsx3.xml.Entity */ item, /* String */ property){
-
-
-
- return (this.getValue(item, property) !== undefined);
- },
-
- hasProperty: function(/* jsx3.xml.Entity */ item, /* String */ property){
-
-
- return this.hasAttribute(item, property);
- },
-
- containsValue: function(/* jsx3.xml.Entity */ item, /* String */ property, /* anything */ value){
-
-
-
- var values = this.getValues(item, property);
- for(var i = 0; i < values.length; i++){
- if(values[i] === null){ continue; }
- if((typeof value === "string")){
- if(values[i].toString && values[i].toString() === value){
- return true;
- }
- }else if(values[i] === value){
- return true;
- }
- }
- return false;
- },
- isItem: function(/* anything */ something){
-
-
-
- if(something.getClass && something.getClass().equals(jsx3.xml.Entity.jsxclass)){
- return true;
- }
- return false;
- },
- isItemLoaded: function(/* anything */ something){
-
-
-
- return this.isItem(something);
- },
- loadItem: function(/* object */ keywordArgs){
-
-
-
-
- },
- getFeatures: function(){
-
-
-
- return {
- "dojo.data.api.Read": true,
- "dojo.data.api.Write": true,
- "dojo.data.api.Identity":true
- };
- },
- getLabel: function(/* jsx3.xml.Entity */ item){
-
-
-
- if((this.label !== "") && this.isItem(item)){
- var label = this.getValue(item,this.label);
- if(label){
- return label.toString();
- }
- }
- return undefined;
- },
- getLabelAttributes: function(/* jsx3.xml.Entity */ item){
-
-
-
-
-
- if(this.label !== ""){
- return [this.label];
- }
- return null;
- },
-
- fetch: function(/* Object? */ request){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- request = request || {};
- if(!request.store){
- request.store = this;
- }
- if(request.mode !== undefined){
- this.mode = request.mode;
- }
- var self = this;
-
- var errorHandler = function(errorData){
- if(request.onError){
- var scope = request.scope || dojo.global;
- request.onError.call(scope, errorData, request);
- }else{
- console.error("cdfStore Error:", errorData);
- }
- };
-
- var fetchHandler = function(items, requestObject){
- requestObject = requestObject || request;
- var oldAbortFunction = requestObject.abort || null;
- var aborted = false;
-
- var startIndex = requestObject.start?requestObject.start:0;
- var endIndex = (requestObject.count && (requestObject.count !== Infinity))?(startIndex + requestObject.count):items.length;
-
- requestObject.abort = function(){
- aborted = true;
- if(oldAbortFunction){
- oldAbortFunction.call(requestObject);
- }
- };
-
- var scope = requestObject.scope || dojo.global;
- if(!requestObject.store){
- requestObject.store = self;
- }
- if(requestObject.onBegin){
- requestObject.onBegin.call(scope, items.length, requestObject);
- }
- if(requestObject.sort){
- items.sort(dojo.data.util.sorter.createSortFunction(requestObject.sort, self));
- }
-
- if(requestObject.onItem){
- for(var i = startIndex; (i < items.length) && (i < endIndex); ++i){
- var item = items[i];
- if(!aborted){
- requestObject.onItem.call(scope, item, requestObject);
- }
- }
- }
- if(requestObject.onComplete && !aborted){
- if(!requestObject.onItem){
- items = items.slice(startIndex, endIndex);
- if(requestObject.byId){
- items = items[0];
- }
- }
- requestObject.onComplete.call(scope, items, requestObject);
- }else{
- items = items.slice(startIndex, endIndex);
- if(requestObject.byId){
- items = items[0];
- }
- }
- return items;
- };
-
- if(!this.url && !this.data && !this.xmlStr){
- errorHandler(new Error("No URL or data specified."));
- return false;
- }
- var localRequest = request || "*";
-
- if(this.mode == dojox.data.SYNC_MODE){
-
- var res = this._loadCDF();
- if(res instanceof Error){
- if(request.onError){
- request.onError.call(request.scope || dojo.global, res, request);
- }else{
- console.error("CdfStore Error:", res);
- }
- return res;
- }
- this.cdfDoc = res;
-
- var items = this._getItems(this.cdfDoc, localRequest);
- if(items && items.length > 0){
- items = fetchHandler(items, request);
- }else{
- items = fetchHandler([], request);
- }
- return items;
-
- }else{
-
-
- var dfd = this._loadCDF();
- dfd.addCallbacks(dojo.hitch(this, function(cdfDoc){
- var items = this._getItems(this.cdfDoc, localRequest);
- if(items && items.length > 0){
- fetchHandler(items, request);
- }else{
- fetchHandler([], request);
- }
- }),
- dojo.hitch(this, function(err){
- errorHandler(err, request);
- }));
-
- return dfd;
- }
- },
-
- _loadCDF: function(){
-
-
-
-
- var dfd = new dojo.Deferred();
- if(this.cdfDoc){
- if(this.mode == dojox.data.SYNC_MODE){
- return this.cdfDoc;
- }else{
- setTimeout(dojo.hitch(this, function(){
- dfd.callback(this.cdfDoc);
- }), 0);
- return dfd;
- }
- }
-
- this.cdfDoc = jsx3.xml.CDF.Document.newDocument();
- this.cdfDoc.subscribe("response", this, function(evt){
- dfd.callback(this.cdfDoc);
- });
- this.cdfDoc.subscribe("error", this, function(err){
- dfd.errback(err);
- });
-
- this.cdfDoc.setAsync(!this.mode);
- if(this.url){
- this.cdfDoc.load(this.url);
- }else if(this.xmlStr){
- this.cdfDoc.loadXML(this.xmlStr);
- if(this.cdfDoc.getError().code){
- return new Error(this.cdfDoc.getError().description);
- }
- }
-
- if(this.mode == dojox.data.SYNC_MODE){
- return this.cdfDoc;
- }else{
- return dfd;
- }
- },
-
- _getItems: function(/* jsx3.xml.Entity */cdfDoc, /* Object */request){
-
-
-
-
- var itr = cdfDoc.selectNodes(request.query, false, 1);
- var items = [];
- while(itr.hasNext()){
- items.push(itr.next());
- }
- return items;
- },
- close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
-
-
- },
- newItem: function(/* object? */ keywordArgs, /* object? || String? */parentInfo){
-
-
-
-
- keywordArgs = (keywordArgs || {});
- if(keywordArgs.tagName){
-
-
- if(keywordArgs.tagName!="record"){
-
- console.warn("Only record inserts are supported at this time");
- }
- delete keywordArgs.tagName;
- }
- keywordArgs.jsxid = keywordArgs.jsxid || this.cdfDoc.getKey();
- if(this.isItem(parentInfo)){
- parentInfo = this.getIdentity(parentInfo);
- }
- var item = this.cdfDoc.insertRecord(keywordArgs, parentInfo);
- this._makeDirty(item);
-
- return item;
- },
-
- deleteItem: function(/* jsx3.xml.Entity */ item){
-
-
-
- this.cdfDoc.deleteRecord(this.getIdentity(item));
- this._makeDirty(item);
- return true;
- },
-
- setValue: function(/* jsx3.xml.Entity */ item, /* String */ property, /* almost anything */ value){
-
-
-
- this._makeDirty(item);
- item.setAttribute(property, value);
- return true;
- },
-
- setValues: function(/* jsx3.xml.Entity */ item, /* String */ property, /*array*/ values){
-
-
-
-
- this._makeDirty(item);
- console.warn("cdfStore.setValues only partially implemented.");
- return item.setAttribute(property, values);
-
- },
-
- unsetAttribute: function(/* jsx3.xml.Entity */ item, /* String */ property){
-
-
-
- this._makeDirty(item);
- item.removeAttribute(property);
- return true;
- },
-
- revert: function(){
-
-
-
-
-
-
-
- delete this.cdfDoc;
- this._modifiedItems = {};
- return true;
- },
-
- isDirty: function(/* jsx3.xml.Entity ? */ item){
-
-
-
-
- if(item){
- return !!this._modifiedItems[this.getIdentity(item)];
- }else{
- var _dirty = false;
- for(var nm in this._modifiedItems){ _dirty = true; break; }
- return _dirty;
- }
- },
-
- _makeDirty: function(item){
-
-
-
- var id = this.getIdentity(item);
- this._modifiedItems[id] = item;
- },
-
-
- _makeXmlString: function(obj){
-
-
-
-
- var parseObj = function(obj, name){
- var xmlStr = "";
- var nm;
- if(dojo.isArray(obj)){
- for(var i=0;i<obj.length;i++){
- xmlStr += parseObj(obj[i], name);
- }
- }else if(dojo.isObject(obj)){
- xmlStr += '<'+name+' ';
- for(nm in obj){
- if(!dojo.isObject(obj[nm])){
- xmlStr += nm+'="'+obj[nm]+'" ';
- }
- }
- xmlStr +='>';
- for(nm in obj){
- if(dojo.isObject(obj[nm])){
- xmlStr += parseObj(obj[nm], nm);
- }
- }
- xmlStr += '</'+name+'>';
- }
- return xmlStr;
- };
- return parseObj(obj, "data");
- },
-
- getIdentity: function(/* jsx3.xml.Entity */ item){
-
-
-
- return this.getValue(item, this.identity);
- },
- getIdentityAttributes: function(/* jsx3.xml.Entity */ item){
-
-
-
- return [this.identity];
- },
- fetchItemByIdentity: function(/* Object || String */ args){
-
-
-
-
-
-
- if(dojo.isString(args)){
- var id = args;
- args = {query:"//record[@jsxid='"+id+"']", mode: dojox.data.SYNC_MODE};
- }else{
- if(args){
- args.query = "//record[@jsxid='"+args.identity+"']";
- }
- if(!args.mode){args.mode = this.mode;}
- }
- args.byId = true;
- return this.fetch(args);
- },
- byId: function(/* Object || String */ args){
-
- }
-
- });
- return dojox.data.CdfStore;
- });
|