summaryrefslogtreecommitdiff
path: root/lib/dojo/data/ItemFileWriteStore.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dojo/data/ItemFileWriteStore.js')
-rw-r--r--lib/dojo/data/ItemFileWriteStore.js814
1 files changed, 2 insertions, 812 deletions
diff --git a/lib/dojo/data/ItemFileWriteStore.js b/lib/dojo/data/ItemFileWriteStore.js
index 2c0f3b326..25651cf92 100644
--- a/lib/dojo/data/ItemFileWriteStore.js
+++ b/lib/dojo/data/ItemFileWriteStore.js
@@ -4,815 +4,5 @@
see: http://dojotoolkit.org/license for details
*/
-
-if(!dojo._hasResource["dojo.data.ItemFileWriteStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
-dojo._hasResource["dojo.data.ItemFileWriteStore"] = true;
-dojo.provide("dojo.data.ItemFileWriteStore");
-dojo.require("dojo.data.ItemFileReadStore");
-
-
-dojo.declare("dojo.data.ItemFileWriteStore", dojo.data.ItemFileReadStore, {
- constructor: function(/* object */ keywordParameters){
- // keywordParameters: {typeMap: object)
- // The structure of the typeMap object is as follows:
- // {
- // type0: function || object,
- // type1: function || object,
- // ...
- // typeN: function || object
- // }
- // Where if it is a function, it is assumed to be an object constructor that takes the
- // value of _value as the initialization parameters. It is serialized assuming object.toString()
- // serialization. If it is an object, then it is assumed
- // to be an object of general form:
- // {
- // type: function, //constructor.
- // deserialize: function(value) //The function that parses the value and constructs the object defined by type appropriately.
- // serialize: function(object) //The function that converts the object back into the proper file format form.
- // }
-
- // ItemFileWriteStore extends ItemFileReadStore to implement these additional dojo.data APIs
- this._features['dojo.data.api.Write'] = true;
- this._features['dojo.data.api.Notification'] = true;
-
- // For keeping track of changes so that we can implement isDirty and revert
- this._pending = {
- _newItems:{},
- _modifiedItems:{},
- _deletedItems:{}
- };
-
- if(!this._datatypeMap['Date'].serialize){
- this._datatypeMap['Date'].serialize = function(obj){
- return dojo.date.stamp.toISOString(obj, {zulu:true});
- };
- }
- //Disable only if explicitly set to false.
- if(keywordParameters && (keywordParameters.referenceIntegrity === false)){
- this.referenceIntegrity = false;
- }
-
- // this._saveInProgress is set to true, briefly, from when save() is first called to when it completes
- this._saveInProgress = false;
- },
-
- referenceIntegrity: true, //Flag that defaultly enabled reference integrity tracking. This way it can also be disabled pogrammatially or declaratively.
-
- _assert: function(/* boolean */ condition){
- if(!condition){
- throw new Error("assertion failed in ItemFileWriteStore");
- }
- },
-
- _getIdentifierAttribute: function(){
- var identifierAttribute = this.getFeatures()['dojo.data.api.Identity'];
- // this._assert((identifierAttribute === Number) || (dojo.isString(identifierAttribute)));
- return identifierAttribute;
- },
-
-
-/* dojo.data.api.Write */
-
- newItem: function(/* Object? */ keywordArgs, /* Object? */ parentInfo){
- // summary: See dojo.data.api.Write.newItem()
-
- this._assert(!this._saveInProgress);
-
- if(!this._loadFinished){
- // We need to do this here so that we'll be able to find out what
- // identifierAttribute was specified in the data file.
- this._forceLoad();
- }
-
- if(typeof keywordArgs != "object" && typeof keywordArgs != "undefined"){
- throw new Error("newItem() was passed something other than an object");
- }
- var newIdentity = null;
- var identifierAttribute = this._getIdentifierAttribute();
- if(identifierAttribute === Number){
- newIdentity = this._arrayOfAllItems.length;
- }else{
- newIdentity = keywordArgs[identifierAttribute];
- if(typeof newIdentity === "undefined"){
- throw new Error("newItem() was not passed an identity for the new item");
- }
- if(dojo.isArray(newIdentity)){
- throw new Error("newItem() was not passed an single-valued identity");
- }
- }
-
- // make sure this identity is not already in use by another item, if identifiers were
- // defined in the file. Otherwise it would be the item count,
- // which should always be unique in this case.
- if(this._itemsByIdentity){
- this._assert(typeof this._itemsByIdentity[newIdentity] === "undefined");
- }
- this._assert(typeof this._pending._newItems[newIdentity] === "undefined");
- this._assert(typeof this._pending._deletedItems[newIdentity] === "undefined");
-
- var newItem = {};
- newItem[this._storeRefPropName] = this;
- newItem[this._itemNumPropName] = this._arrayOfAllItems.length;
- if(this._itemsByIdentity){
- this._itemsByIdentity[newIdentity] = newItem;
- //We have to set the identifier now, otherwise we can't look it
- //up at calls to setValueorValues in parentInfo handling.
- newItem[identifierAttribute] = [newIdentity];
- }
- this._arrayOfAllItems.push(newItem);
-
- //We need to construct some data for the onNew call too...
- var pInfo = null;
-
- // Now we need to check to see where we want to assign this thingm if any.
- if(parentInfo && parentInfo.parent && parentInfo.attribute){
- pInfo = {
- item: parentInfo.parent,
- attribute: parentInfo.attribute,
- oldValue: undefined
- };
-
- //See if it is multi-valued or not and handle appropriately
- //Generally, all attributes are multi-valued for this store
- //So, we only need to append if there are already values present.
- var values = this.getValues(parentInfo.parent, parentInfo.attribute);
- if(values && values.length > 0){
- var tempValues = values.slice(0, values.length);
- if(values.length === 1){
- pInfo.oldValue = values[0];
- }else{
- pInfo.oldValue = values.slice(0, values.length);
- }
- tempValues.push(newItem);
- this._setValueOrValues(parentInfo.parent, parentInfo.attribute, tempValues, false);
- pInfo.newValue = this.getValues(parentInfo.parent, parentInfo.attribute);
- }else{
- this._setValueOrValues(parentInfo.parent, parentInfo.attribute, newItem, false);
- pInfo.newValue = newItem;
- }
- }else{
- //Toplevel item, add to both top list as well as all list.
- newItem[this._rootItemPropName]=true;
- this._arrayOfTopLevelItems.push(newItem);
- }
-
- this._pending._newItems[newIdentity] = newItem;
-
- //Clone over the properties to the new item
- for(var key in keywordArgs){
- if(key === this._storeRefPropName || key === this._itemNumPropName){
- // Bummer, the user is trying to do something like
- // newItem({_S:"foo"}). Unfortunately, our superclass,
- // ItemFileReadStore, is already using _S in each of our items
- // to hold private info. To avoid a naming collision, we
- // need to move all our private info to some other property
- // of all the items/objects. So, we need to iterate over all
- // the items and do something like:
- // item.__S = item._S;
- // item._S = undefined;
- // But first we have to make sure the new "__S" variable is
- // not in use, which means we have to iterate over all the
- // items checking for that.
- throw new Error("encountered bug in ItemFileWriteStore.newItem");
- }
- var value = keywordArgs[key];
- if(!dojo.isArray(value)){
- value = [value];
- }
- newItem[key] = value;
- if(this.referenceIntegrity){
- for(var i = 0; i < value.length; i++){
- var val = value[i];
- if(this.isItem(val)){
- this._addReferenceToMap(val, newItem, key);
- }
- }
- }
- }
- this.onNew(newItem, pInfo); // dojo.data.api.Notification call
- return newItem; // item
- },
-
- _removeArrayElement: function(/* Array */ array, /* anything */ element){
- var index = dojo.indexOf(array, element);
- if(index != -1){
- array.splice(index, 1);
- return true;
- }
- return false;
- },
-
- deleteItem: function(/* item */ item){
- // summary: See dojo.data.api.Write.deleteItem()
- this._assert(!this._saveInProgress);
- this._assertIsItem(item);
-
- // Remove this item from the _arrayOfAllItems, but leave a null value in place
- // of the item, so as not to change the length of the array, so that in newItem()
- // we can still safely do: newIdentity = this._arrayOfAllItems.length;
- var indexInArrayOfAllItems = item[this._itemNumPropName];
- var identity = this.getIdentity(item);
-
- //If we have reference integrity on, we need to do reference cleanup for the deleted item
- if(this.referenceIntegrity){
- //First scan all the attributes of this items for references and clean them up in the map
- //As this item is going away, no need to track its references anymore.
-
- //Get the attributes list before we generate the backup so it
- //doesn't pollute the attributes list.
- var attributes = this.getAttributes(item);
-
- //Backup the map, we'll have to restore it potentially, in a revert.
- if(item[this._reverseRefMap]){
- item["backup_" + this._reverseRefMap] = dojo.clone(item[this._reverseRefMap]);
- }
-
- //TODO: This causes a reversion problem. This list won't be restored on revert since it is
- //attached to the 'value'. item, not ours. Need to back tese up somehow too.
- //Maybe build a map of the backup of the entries and attach it to the deleted item to be restored
- //later. Or just record them and call _addReferenceToMap on them in revert.
- dojo.forEach(attributes, function(attribute){
- dojo.forEach(this.getValues(item, attribute), function(value){
- if(this.isItem(value)){
- //We have to back up all the references we had to others so they can be restored on a revert.
- if(!item["backupRefs_" + this._reverseRefMap]){
- item["backupRefs_" + this._reverseRefMap] = [];
- }
- item["backupRefs_" + this._reverseRefMap].push({id: this.getIdentity(value), attr: attribute});
- this._removeReferenceFromMap(value, item, attribute);
- }
- }, this);
- }, this);
-
- //Next, see if we have references to this item, if we do, we have to clean them up too.
- var references = item[this._reverseRefMap];
- if(references){
- //Look through all the items noted as references to clean them up.
- for(var itemId in references){
- var containingItem = null;
- if(this._itemsByIdentity){
- containingItem = this._itemsByIdentity[itemId];
- }else{
- containingItem = this._arrayOfAllItems[itemId];
- }
- //We have a reference to a containing item, now we have to process the
- //attributes and clear all references to the item being deleted.
- if(containingItem){
- for(var attribute in references[itemId]){
- var oldValues = this.getValues(containingItem, attribute) || [];
- var newValues = dojo.filter(oldValues, function(possibleItem){
- return !(this.isItem(possibleItem) && this.getIdentity(possibleItem) == identity);
- }, this);
- //Remove the note of the reference to the item and set the values on the modified attribute.
- this._removeReferenceFromMap(item, containingItem, attribute);
- if(newValues.length < oldValues.length){
- this._setValueOrValues(containingItem, attribute, newValues, true);
- }
- }
- }
- }
- }
- }
-
- this._arrayOfAllItems[indexInArrayOfAllItems] = null;
-
- item[this._storeRefPropName] = null;
- if(this._itemsByIdentity){
- delete this._itemsByIdentity[identity];
- }
- this._pending._deletedItems[identity] = item;
-
- //Remove from the toplevel items, if necessary...
- if(item[this._rootItemPropName]){
- this._removeArrayElement(this._arrayOfTopLevelItems, item);
- }
- this.onDelete(item); // dojo.data.api.Notification call
- return true;
- },
-
- setValue: function(/* item */ item, /* attribute-name-string */ attribute, /* almost anything */ value){
- // summary: See dojo.data.api.Write.set()
- return this._setValueOrValues(item, attribute, value, true); // boolean
- },
-
- setValues: function(/* item */ item, /* attribute-name-string */ attribute, /* array */ values){
- // summary: See dojo.data.api.Write.setValues()
- return this._setValueOrValues(item, attribute, values, true); // boolean
- },
-
- unsetAttribute: function(/* item */ item, /* attribute-name-string */ attribute){
- // summary: See dojo.data.api.Write.unsetAttribute()
- return this._setValueOrValues(item, attribute, [], true);
- },
-
- _setValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ newValueOrValues, /*boolean?*/ callOnSet){
- this._assert(!this._saveInProgress);
-
- // Check for valid arguments
- this._assertIsItem(item);
- this._assert(dojo.isString(attribute));
- this._assert(typeof newValueOrValues !== "undefined");
-
- // Make sure the user isn't trying to change the item's identity
- var identifierAttribute = this._getIdentifierAttribute();
- if(attribute == identifierAttribute){
- throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier.");
- }
-
- // To implement the Notification API, we need to make a note of what
- // the old attribute value was, so that we can pass that info when
- // we call the onSet method.
- var oldValueOrValues = this._getValueOrValues(item, attribute);
-
- var identity = this.getIdentity(item);
- if(!this._pending._modifiedItems[identity]){
- // Before we actually change the item, we make a copy of it to
- // record the original state, so that we'll be able to revert if
- // the revert method gets called. If the item has already been
- // modified then there's no need to do this now, since we already
- // have a record of the original state.
- var copyOfItemState = {};
- for(var key in item){
- if((key === this._storeRefPropName) || (key === this._itemNumPropName) || (key === this._rootItemPropName)){
- copyOfItemState[key] = item[key];
- }else if(key === this._reverseRefMap){
- copyOfItemState[key] = dojo.clone(item[key]);
- }else{
- copyOfItemState[key] = item[key].slice(0, item[key].length);
- }
- }
- // Now mark the item as dirty, and save the copy of the original state
- this._pending._modifiedItems[identity] = copyOfItemState;
- }
-
- // Okay, now we can actually change this attribute on the item
- var success = false;
-
- if(dojo.isArray(newValueOrValues) && newValueOrValues.length === 0){
-
- // If we were passed an empty array as the value, that counts
- // as "unsetting" the attribute, so we need to remove this
- // attribute from the item.
- success = delete item[attribute];
- newValueOrValues = undefined; // used in the onSet Notification call below
-
- if(this.referenceIntegrity && oldValueOrValues){
- var oldValues = oldValueOrValues;
- if(!dojo.isArray(oldValues)){
- oldValues = [oldValues];
- }
- for(var i = 0; i < oldValues.length; i++){
- var value = oldValues[i];
- if(this.isItem(value)){
- this._removeReferenceFromMap(value, item, attribute);
- }
- }
- }
- }else{
- var newValueArray;
- if(dojo.isArray(newValueOrValues)){
- var newValues = newValueOrValues;
- // Unfortunately, it's not safe to just do this:
- // newValueArray = newValues;
- // Instead, we need to copy the array, which slice() does very nicely.
- // This is so that our internal data structure won't
- // get corrupted if the user mucks with the values array *after*
- // calling setValues().
- newValueArray = newValueOrValues.slice(0, newValueOrValues.length);
- }else{
- newValueArray = [newValueOrValues];
- }
-
- //We need to handle reference integrity if this is on.
- //In the case of set, we need to see if references were added or removed
- //and update the reference tracking map accordingly.
- if(this.referenceIntegrity){
- if(oldValueOrValues){
- var oldValues = oldValueOrValues;
- if(!dojo.isArray(oldValues)){
- oldValues = [oldValues];
- }
- //Use an associative map to determine what was added/removed from the list.
- //Should be O(n) performant. First look at all the old values and make a list of them
- //Then for any item not in the old list, we add it. If it was already present, we remove it.
- //Then we pass over the map and any references left it it need to be removed (IE, no match in
- //the new values list).
- var map = {};
- dojo.forEach(oldValues, function(possibleItem){
- if(this.isItem(possibleItem)){
- var id = this.getIdentity(possibleItem);
- map[id.toString()] = true;
- }
- }, this);
- dojo.forEach(newValueArray, function(possibleItem){
- if(this.isItem(possibleItem)){
- var id = this.getIdentity(possibleItem);
- if(map[id.toString()]){
- delete map[id.toString()];
- }else{
- this._addReferenceToMap(possibleItem, item, attribute);
- }
- }
- }, this);
- for(var rId in map){
- var removedItem;
- if(this._itemsByIdentity){
- removedItem = this._itemsByIdentity[rId];
- }else{
- removedItem = this._arrayOfAllItems[rId];
- }
- this._removeReferenceFromMap(removedItem, item, attribute);
- }
- }else{
- //Everything is new (no old values) so we have to just
- //insert all the references, if any.
- for(var i = 0; i < newValueArray.length; i++){
- var value = newValueArray[i];
- if(this.isItem(value)){
- this._addReferenceToMap(value, item, attribute);
- }
- }
- }
- }
- item[attribute] = newValueArray;
- success = true;
- }
-
- // Now we make the dojo.data.api.Notification call
- if(callOnSet){
- this.onSet(item, attribute, oldValueOrValues, newValueOrValues);
- }
- return success; // boolean
- },
-
- _addReferenceToMap: function(/*item*/ refItem, /*item*/ parentItem, /*string*/ attribute){
- // summary:
- // Method to add an reference map entry for an item and attribute.
- // description:
- // Method to add an reference map entry for an item and attribute. //
- // refItem:
- // The item that is referenced.
- // parentItem:
- // The item that holds the new reference to refItem.
- // attribute:
- // The attribute on parentItem that contains the new reference.
-
- var parentId = this.getIdentity(parentItem);
- var references = refItem[this._reverseRefMap];
-
- if(!references){
- references = refItem[this._reverseRefMap] = {};
- }
- var itemRef = references[parentId];
- if(!itemRef){
- itemRef = references[parentId] = {};
- }
- itemRef[attribute] = true;
- },
-
- _removeReferenceFromMap: function(/* item */ refItem, /* item */ parentItem, /*strin*/ attribute){
- // summary:
- // Method to remove an reference map entry for an item and attribute.
- // description:
- // Method to remove an reference map entry for an item and attribute. This will
- // also perform cleanup on the map such that if there are no more references at all to
- // the item, its reference object and entry are removed.
- //
- // refItem:
- // The item that is referenced.
- // parentItem:
- // The item holding a reference to refItem.
- // attribute:
- // The attribute on parentItem that contains the reference.
- var identity = this.getIdentity(parentItem);
- var references = refItem[this._reverseRefMap];
- var itemId;
- if(references){
- for(itemId in references){
- if(itemId == identity){
- delete references[itemId][attribute];
- if(this._isEmpty(references[itemId])){
- delete references[itemId];
- }
- }
- }
- if(this._isEmpty(references)){
- delete refItem[this._reverseRefMap];
- }
- }
- },
-
- _dumpReferenceMap: function(){
- // summary:
- // Function to dump the reverse reference map of all items in the store for debug purposes.
- // description:
- // Function to dump the reverse reference map of all items in the store for debug purposes.
- var i;
- for(i = 0; i < this._arrayOfAllItems.length; i++){
- var item = this._arrayOfAllItems[i];
- if(item && item[this._reverseRefMap]){
- console.log("Item: [" + this.getIdentity(item) + "] is referenced by: " + dojo.toJson(item[this._reverseRefMap]));
- }
- }
- },
-
- _getValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute){
- var valueOrValues = undefined;
- if(this.hasAttribute(item, attribute)){
- var valueArray = this.getValues(item, attribute);
- if(valueArray.length == 1){
- valueOrValues = valueArray[0];
- }else{
- valueOrValues = valueArray;
- }
- }
- return valueOrValues;
- },
-
- _flatten: function(/* anything */ value){
- if(this.isItem(value)){
- var item = value;
- // Given an item, return an serializable object that provides a
- // reference to the item.
- // For example, given kermit:
- // var kermit = store.newItem({id:2, name:"Kermit"});
- // we want to return
- // {_reference:2}
- var identity = this.getIdentity(item);
- var referenceObject = {_reference: identity};
- return referenceObject;
- }else{
- if(typeof value === "object"){
- for(var type in this._datatypeMap){
- var typeMap = this._datatypeMap[type];
- if(dojo.isObject(typeMap) && !dojo.isFunction(typeMap)){
- if(value instanceof typeMap.type){
- if(!typeMap.serialize){
- throw new Error("ItemFileWriteStore: No serializer defined for type mapping: [" + type + "]");
- }
- return {_type: type, _value: typeMap.serialize(value)};
- }
- } else if(value instanceof typeMap){
- //SImple mapping, therefore, return as a toString serialization.
- return {_type: type, _value: value.toString()};
- }
- }
- }
- return value;
- }
- },
-
- _getNewFileContentString: function(){
- // summary:
- // Generate a string that can be saved to a file.
- // The result should look similar to:
- // http://trac.dojotoolkit.org/browser/dojo/trunk/tests/data/countries.json
- var serializableStructure = {};
-
- var identifierAttribute = this._getIdentifierAttribute();
- if(identifierAttribute !== Number){
- serializableStructure.identifier = identifierAttribute;
- }
- if(this._labelAttr){
- serializableStructure.label = this._labelAttr;
- }
- serializableStructure.items = [];
- for(var i = 0; i < this._arrayOfAllItems.length; ++i){
- var item = this._arrayOfAllItems[i];
- if(item !== null){
- var serializableItem = {};
- for(var key in item){
- if(key !== this._storeRefPropName && key !== this._itemNumPropName && key !== this._reverseRefMap && key !== this._rootItemPropName){
- var attribute = key;
- var valueArray = this.getValues(item, attribute);
- if(valueArray.length == 1){
- serializableItem[attribute] = this._flatten(valueArray[0]);
- }else{
- var serializableArray = [];
- for(var j = 0; j < valueArray.length; ++j){
- serializableArray.push(this._flatten(valueArray[j]));
- serializableItem[attribute] = serializableArray;
- }
- }
- }
- }
- serializableStructure.items.push(serializableItem);
- }
- }
- var prettyPrint = true;
- return dojo.toJson(serializableStructure, prettyPrint);
- },
-
- _isEmpty: function(something){
- // summary:
- // Function to determine if an array or object has no properties or values.
- // something:
- // The array or object to examine.
- var empty = true;
- if(dojo.isObject(something)){
- var i;
- for(i in something){
- empty = false;
- break;
- }
- }else if(dojo.isArray(something)){
- if(something.length > 0){
- empty = false;
- }
- }
- return empty; //boolean
- },
-
- save: function(/* object */ keywordArgs){
- // summary: See dojo.data.api.Write.save()
- this._assert(!this._saveInProgress);
-
- // this._saveInProgress is set to true, briefly, from when save is first called to when it completes
- this._saveInProgress = true;
-
- var self = this;
- var saveCompleteCallback = function(){
- self._pending = {
- _newItems:{},
- _modifiedItems:{},
- _deletedItems:{}
- };
-
- self._saveInProgress = false; // must come after this._pending is cleared, but before any callbacks
- if(keywordArgs && keywordArgs.onComplete){
- var scope = keywordArgs.scope || dojo.global;
- keywordArgs.onComplete.call(scope);
- }
- };
- var saveFailedCallback = function(err){
- self._saveInProgress = false;
- if(keywordArgs && keywordArgs.onError){
- var scope = keywordArgs.scope || dojo.global;
- keywordArgs.onError.call(scope, err);
- }
- };
-
- if(this._saveEverything){
- var newFileContentString = this._getNewFileContentString();
- this._saveEverything(saveCompleteCallback, saveFailedCallback, newFileContentString);
- }
- if(this._saveCustom){
- this._saveCustom(saveCompleteCallback, saveFailedCallback);
- }
- if(!this._saveEverything && !this._saveCustom){
- // Looks like there is no user-defined save-handler function.
- // That's fine, it just means the datastore is acting as a "mock-write"
- // store -- changes get saved in memory but don't get saved to disk.
- saveCompleteCallback();
- }
- },
-
- revert: function(){
- // summary: See dojo.data.api.Write.revert()
- this._assert(!this._saveInProgress);
-
- var identity;
- for(identity in this._pending._modifiedItems){
- // find the original item and the modified item that replaced it
- var copyOfItemState = this._pending._modifiedItems[identity];
- var modifiedItem = null;
- if(this._itemsByIdentity){
- modifiedItem = this._itemsByIdentity[identity];
- }else{
- modifiedItem = this._arrayOfAllItems[identity];
- }
-
- // Restore the original item into a full-fledged item again, we want to try to
- // keep the same object instance as if we don't it, causes bugs like #9022.
- copyOfItemState[this._storeRefPropName] = this;
- for(key in modifiedItem){
- delete modifiedItem[key];
- }
- dojo.mixin(modifiedItem, copyOfItemState);
- }
- var deletedItem;
- for(identity in this._pending._deletedItems){
- deletedItem = this._pending._deletedItems[identity];
- deletedItem[this._storeRefPropName] = this;
- var index = deletedItem[this._itemNumPropName];
-
- //Restore the reverse refererence map, if any.
- if(deletedItem["backup_" + this._reverseRefMap]){
- deletedItem[this._reverseRefMap] = deletedItem["backup_" + this._reverseRefMap];
- delete deletedItem["backup_" + this._reverseRefMap];
- }
- this._arrayOfAllItems[index] = deletedItem;
- if(this._itemsByIdentity){
- this._itemsByIdentity[identity] = deletedItem;
- }
- if(deletedItem[this._rootItemPropName]){
- this._arrayOfTopLevelItems.push(deletedItem);
- }
- }
- //We have to pass through it again and restore the reference maps after all the
- //undeletes have occurred.
- for(identity in this._pending._deletedItems){
- deletedItem = this._pending._deletedItems[identity];
- if(deletedItem["backupRefs_" + this._reverseRefMap]){
- dojo.forEach(deletedItem["backupRefs_" + this._reverseRefMap], function(reference){
- var refItem;
- if(this._itemsByIdentity){
- refItem = this._itemsByIdentity[reference.id];
- }else{
- refItem = this._arrayOfAllItems[reference.id];
- }
- this._addReferenceToMap(refItem, deletedItem, reference.attr);
- }, this);
- delete deletedItem["backupRefs_" + this._reverseRefMap];
- }
- }
-
- for(identity in this._pending._newItems){
- var newItem = this._pending._newItems[identity];
- newItem[this._storeRefPropName] = null;
- // null out the new item, but don't change the array index so
- // so we can keep using _arrayOfAllItems.length.
- this._arrayOfAllItems[newItem[this._itemNumPropName]] = null;
- if(newItem[this._rootItemPropName]){
- this._removeArrayElement(this._arrayOfTopLevelItems, newItem);
- }
- if(this._itemsByIdentity){
- delete this._itemsByIdentity[identity];
- }
- }
-
- this._pending = {
- _newItems:{},
- _modifiedItems:{},
- _deletedItems:{}
- };
- return true; // boolean
- },
-
- isDirty: function(/* item? */ item){
- // summary: See dojo.data.api.Write.isDirty()
- if(item){
- // return true if the item is dirty
- var identity = this.getIdentity(item);
- return new Boolean(this._pending._newItems[identity] ||
- this._pending._modifiedItems[identity] ||
- this._pending._deletedItems[identity]).valueOf(); // boolean
- }else{
- // return true if the store is dirty -- which means return true
- // if there are any new items, dirty items, or modified items
- if(!this._isEmpty(this._pending._newItems) ||
- !this._isEmpty(this._pending._modifiedItems) ||
- !this._isEmpty(this._pending._deletedItems)){
- return true;
- }
- return false; // boolean
- }
- },
-
-/* dojo.data.api.Notification */
-
- onSet: function(/* item */ item,
- /*attribute-name-string*/ attribute,
- /*object | array*/ oldValue,
- /*object | array*/ newValue){
- // summary: See dojo.data.api.Notification.onSet()
-
- // No need to do anything. This method is here just so that the
- // client code can connect observers to it.
- },
-
- onNew: function(/* item */ newItem, /*object?*/ parentInfo){
- // summary: See dojo.data.api.Notification.onNew()
-
- // No need to do anything. This method is here just so that the
- // client code can connect observers to it.
- },
-
- onDelete: function(/* item */ deletedItem){
- // summary: See dojo.data.api.Notification.onDelete()
-
- // No need to do anything. This method is here just so that the
- // client code can connect observers to it.
- },
-
- close: function(/* object? */ request){
- // summary:
- // Over-ride of base close function of ItemFileReadStore to add in check for store state.
- // description:
- // Over-ride of base close function of ItemFileReadStore to add in check for store state.
- // If the store is still dirty (unsaved changes), then an error will be thrown instead of
- // clearing the internal state for reload from the url.
-
- //Clear if not dirty ... or throw an error
- if(this.clearOnClose){
- if(!this.isDirty()){
- this.inherited(arguments);
- }else{
- //Only throw an error if the store was dirty and we were loading from a url (cannot reload from url until state is saved).
- throw new Error("dojo.data.ItemFileWriteStore: There are unsaved changes present in the store. Please save or revert the changes before invoking close.");
- }
- }
- }
-});
-
-}
+//>>built
+define("dojo/data/ItemFileWriteStore",["../_base/lang","../_base/declare","../_base/array","../_base/json","../_base/window","./ItemFileReadStore","../date/stamp"],function(_1,_2,_3,_4,_5,_6,_7){return _2("dojo.data.ItemFileWriteStore",_6,{constructor:function(_8){this._features["dojo.data.api.Write"]=true;this._features["dojo.data.api.Notification"]=true;this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};if(!this._datatypeMap["Date"].serialize){this._datatypeMap["Date"].serialize=function(_9){return _7.toISOString(_9,{zulu:true});};}if(_8&&(_8.referenceIntegrity===false)){this.referenceIntegrity=false;}this._saveInProgress=false;},referenceIntegrity:true,_assert:function(_a){if(!_a){throw new Error("assertion failed in ItemFileWriteStore");}},_getIdentifierAttribute:function(){return this.getFeatures()["dojo.data.api.Identity"];},newItem:function(_b,_c){this._assert(!this._saveInProgress);if(!this._loadFinished){this._forceLoad();}if(typeof _b!="object"&&typeof _b!="undefined"){throw new Error("newItem() was passed something other than an object");}var _d=null;var _e=this._getIdentifierAttribute();if(_e===Number){_d=this._arrayOfAllItems.length;}else{_d=_b[_e];if(typeof _d==="undefined"){throw new Error("newItem() was not passed an identity for the new item");}if(_1.isArray(_d)){throw new Error("newItem() was not passed an single-valued identity");}}if(this._itemsByIdentity){this._assert(typeof this._itemsByIdentity[_d]==="undefined");}this._assert(typeof this._pending._newItems[_d]==="undefined");this._assert(typeof this._pending._deletedItems[_d]==="undefined");var _f={};_f[this._storeRefPropName]=this;_f[this._itemNumPropName]=this._arrayOfAllItems.length;if(this._itemsByIdentity){this._itemsByIdentity[_d]=_f;_f[_e]=[_d];}this._arrayOfAllItems.push(_f);var _10=null;if(_c&&_c.parent&&_c.attribute){_10={item:_c.parent,attribute:_c.attribute,oldValue:undefined};var _11=this.getValues(_c.parent,_c.attribute);if(_11&&_11.length>0){var _12=_11.slice(0,_11.length);if(_11.length===1){_10.oldValue=_11[0];}else{_10.oldValue=_11.slice(0,_11.length);}_12.push(_f);this._setValueOrValues(_c.parent,_c.attribute,_12,false);_10.newValue=this.getValues(_c.parent,_c.attribute);}else{this._setValueOrValues(_c.parent,_c.attribute,_f,false);_10.newValue=_f;}}else{_f[this._rootItemPropName]=true;this._arrayOfTopLevelItems.push(_f);}this._pending._newItems[_d]=_f;for(var key in _b){if(key===this._storeRefPropName||key===this._itemNumPropName){throw new Error("encountered bug in ItemFileWriteStore.newItem");}var _13=_b[key];if(!_1.isArray(_13)){_13=[_13];}_f[key]=_13;if(this.referenceIntegrity){for(var i=0;i<_13.length;i++){var val=_13[i];if(this.isItem(val)){this._addReferenceToMap(val,_f,key);}}}}this.onNew(_f,_10);return _f;},_removeArrayElement:function(_14,_15){var _16=_3.indexOf(_14,_15);if(_16!=-1){_14.splice(_16,1);return true;}return false;},deleteItem:function(_17){this._assert(!this._saveInProgress);this._assertIsItem(_17);var _18=_17[this._itemNumPropName];var _19=this.getIdentity(_17);if(this.referenceIntegrity){var _1a=this.getAttributes(_17);if(_17[this._reverseRefMap]){_17["backup_"+this._reverseRefMap]=_1.clone(_17[this._reverseRefMap]);}_3.forEach(_1a,function(_1b){_3.forEach(this.getValues(_17,_1b),function(_1c){if(this.isItem(_1c)){if(!_17["backupRefs_"+this._reverseRefMap]){_17["backupRefs_"+this._reverseRefMap]=[];}_17["backupRefs_"+this._reverseRefMap].push({id:this.getIdentity(_1c),attr:_1b});this._removeReferenceFromMap(_1c,_17,_1b);}},this);},this);var _1d=_17[this._reverseRefMap];if(_1d){for(var _1e in _1d){var _1f=null;if(this._itemsByIdentity){_1f=this._itemsByIdentity[_1e];}else{_1f=this._arrayOfAllItems[_1e];}if(_1f){for(var _20 in _1d[_1e]){var _21=this.getValues(_1f,_20)||[];var _22=_3.filter(_21,function(_23){return !(this.isItem(_23)&&this.getIdentity(_23)==_19);},this);this._removeReferenceFromMap(_17,_1f,_20);if(_22.length<_21.length){this._setValueOrValues(_1f,_20,_22,true);}}}}}}this._arrayOfAllItems[_18]=null;_17[this._storeRefPropName]=null;if(this._itemsByIdentity){delete this._itemsByIdentity[_19];}this._pending._deletedItems[_19]=_17;if(_17[this._rootItemPropName]){this._removeArrayElement(this._arrayOfTopLevelItems,_17);}this.onDelete(_17);return true;},setValue:function(_24,_25,_26){return this._setValueOrValues(_24,_25,_26,true);},setValues:function(_27,_28,_29){return this._setValueOrValues(_27,_28,_29,true);},unsetAttribute:function(_2a,_2b){return this._setValueOrValues(_2a,_2b,[],true);},_setValueOrValues:function(_2c,_2d,_2e,_2f){this._assert(!this._saveInProgress);this._assertIsItem(_2c);this._assert(_1.isString(_2d));this._assert(typeof _2e!=="undefined");var _30=this._getIdentifierAttribute();if(_2d==_30){throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier.");}var _31=this._getValueOrValues(_2c,_2d);var _32=this.getIdentity(_2c);if(!this._pending._modifiedItems[_32]){var _33={};for(var key in _2c){if((key===this._storeRefPropName)||(key===this._itemNumPropName)||(key===this._rootItemPropName)){_33[key]=_2c[key];}else{if(key===this._reverseRefMap){_33[key]=_1.clone(_2c[key]);}else{_33[key]=_2c[key].slice(0,_2c[key].length);}}}this._pending._modifiedItems[_32]=_33;}var _34=false;if(_1.isArray(_2e)&&_2e.length===0){_34=delete _2c[_2d];_2e=undefined;if(this.referenceIntegrity&&_31){var _35=_31;if(!_1.isArray(_35)){_35=[_35];}for(var i=0;i<_35.length;i++){var _36=_35[i];if(this.isItem(_36)){this._removeReferenceFromMap(_36,_2c,_2d);}}}}else{var _37;if(_1.isArray(_2e)){_37=_2e.slice(0,_2e.length);}else{_37=[_2e];}if(this.referenceIntegrity){if(_31){var _35=_31;if(!_1.isArray(_35)){_35=[_35];}var map={};_3.forEach(_35,function(_38){if(this.isItem(_38)){var id=this.getIdentity(_38);map[id.toString()]=true;}},this);_3.forEach(_37,function(_39){if(this.isItem(_39)){var id=this.getIdentity(_39);if(map[id.toString()]){delete map[id.toString()];}else{this._addReferenceToMap(_39,_2c,_2d);}}},this);for(var rId in map){var _3a;if(this._itemsByIdentity){_3a=this._itemsByIdentity[rId];}else{_3a=this._arrayOfAllItems[rId];}this._removeReferenceFromMap(_3a,_2c,_2d);}}else{for(var i=0;i<_37.length;i++){var _36=_37[i];if(this.isItem(_36)){this._addReferenceToMap(_36,_2c,_2d);}}}}_2c[_2d]=_37;_34=true;}if(_2f){this.onSet(_2c,_2d,_31,_2e);}return _34;},_addReferenceToMap:function(_3b,_3c,_3d){var _3e=this.getIdentity(_3c);var _3f=_3b[this._reverseRefMap];if(!_3f){_3f=_3b[this._reverseRefMap]={};}var _40=_3f[_3e];if(!_40){_40=_3f[_3e]={};}_40[_3d]=true;},_removeReferenceFromMap:function(_41,_42,_43){var _44=this.getIdentity(_42);var _45=_41[this._reverseRefMap];var _46;if(_45){for(_46 in _45){if(_46==_44){delete _45[_46][_43];if(this._isEmpty(_45[_46])){delete _45[_46];}}}if(this._isEmpty(_45)){delete _41[this._reverseRefMap];}}},_dumpReferenceMap:function(){var i;for(i=0;i<this._arrayOfAllItems.length;i++){var _47=this._arrayOfAllItems[i];if(_47&&_47[this._reverseRefMap]){}}},_getValueOrValues:function(_48,_49){var _4a=undefined;if(this.hasAttribute(_48,_49)){var _4b=this.getValues(_48,_49);if(_4b.length==1){_4a=_4b[0];}else{_4a=_4b;}}return _4a;},_flatten:function(_4c){if(this.isItem(_4c)){return {_reference:this.getIdentity(_4c)};}else{if(typeof _4c==="object"){for(var _4d in this._datatypeMap){var _4e=this._datatypeMap[_4d];if(_1.isObject(_4e)&&!_1.isFunction(_4e)){if(_4c instanceof _4e.type){if(!_4e.serialize){throw new Error("ItemFileWriteStore: No serializer defined for type mapping: ["+_4d+"]");}return {_type:_4d,_value:_4e.serialize(_4c)};}}else{if(_4c instanceof _4e){return {_type:_4d,_value:_4c.toString()};}}}}return _4c;}},_getNewFileContentString:function(){var _4f={};var _50=this._getIdentifierAttribute();if(_50!==Number){_4f.identifier=_50;}if(this._labelAttr){_4f.label=this._labelAttr;}_4f.items=[];for(var i=0;i<this._arrayOfAllItems.length;++i){var _51=this._arrayOfAllItems[i];if(_51!==null){var _52={};for(var key in _51){if(key!==this._storeRefPropName&&key!==this._itemNumPropName&&key!==this._reverseRefMap&&key!==this._rootItemPropName){var _53=this.getValues(_51,key);if(_53.length==1){_52[key]=this._flatten(_53[0]);}else{var _54=[];for(var j=0;j<_53.length;++j){_54.push(this._flatten(_53[j]));_52[key]=_54;}}}}_4f.items.push(_52);}}var _55=true;return _4.toJson(_4f,_55);},_isEmpty:function(_56){var _57=true;if(_1.isObject(_56)){var i;for(i in _56){_57=false;break;}}else{if(_1.isArray(_56)){if(_56.length>0){_57=false;}}}return _57;},save:function(_58){this._assert(!this._saveInProgress);this._saveInProgress=true;var _59=this;var _5a=function(){_59._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};_59._saveInProgress=false;if(_58&&_58.onComplete){var _5b=_58.scope||_5.global;_58.onComplete.call(_5b);}};var _5c=function(err){_59._saveInProgress=false;if(_58&&_58.onError){var _5d=_58.scope||_5.global;_58.onError.call(_5d,err);}};if(this._saveEverything){var _5e=this._getNewFileContentString();this._saveEverything(_5a,_5c,_5e);}if(this._saveCustom){this._saveCustom(_5a,_5c);}if(!this._saveEverything&&!this._saveCustom){_5a();}},revert:function(){this._assert(!this._saveInProgress);var _5f;for(_5f in this._pending._modifiedItems){var _60=this._pending._modifiedItems[_5f];var _61=null;if(this._itemsByIdentity){_61=this._itemsByIdentity[_5f];}else{_61=this._arrayOfAllItems[_5f];}_60[this._storeRefPropName]=this;for(var key in _61){delete _61[key];}_1.mixin(_61,_60);}var _62;for(_5f in this._pending._deletedItems){_62=this._pending._deletedItems[_5f];_62[this._storeRefPropName]=this;var _63=_62[this._itemNumPropName];if(_62["backup_"+this._reverseRefMap]){_62[this._reverseRefMap]=_62["backup_"+this._reverseRefMap];delete _62["backup_"+this._reverseRefMap];}this._arrayOfAllItems[_63]=_62;if(this._itemsByIdentity){this._itemsByIdentity[_5f]=_62;}if(_62[this._rootItemPropName]){this._arrayOfTopLevelItems.push(_62);}}for(_5f in this._pending._deletedItems){_62=this._pending._deletedItems[_5f];if(_62["backupRefs_"+this._reverseRefMap]){_3.forEach(_62["backupRefs_"+this._reverseRefMap],function(_64){var _65;if(this._itemsByIdentity){_65=this._itemsByIdentity[_64.id];}else{_65=this._arrayOfAllItems[_64.id];}this._addReferenceToMap(_65,_62,_64.attr);},this);delete _62["backupRefs_"+this._reverseRefMap];}}for(_5f in this._pending._newItems){var _66=this._pending._newItems[_5f];_66[this._storeRefPropName]=null;this._arrayOfAllItems[_66[this._itemNumPropName]]=null;if(_66[this._rootItemPropName]){this._removeArrayElement(this._arrayOfTopLevelItems,_66);}if(this._itemsByIdentity){delete this._itemsByIdentity[_5f];}}this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};return true;},isDirty:function(_67){if(_67){var _68=this.getIdentity(_67);return new Boolean(this._pending._newItems[_68]||this._pending._modifiedItems[_68]||this._pending._deletedItems[_68]).valueOf();}else{return !this._isEmpty(this._pending._newItems)||!this._isEmpty(this._pending._modifiedItems)||!this._isEmpty(this._pending._deletedItems);}},onSet:function(_69,_6a,_6b,_6c){},onNew:function(_6d,_6e){},onDelete:function(_6f){},close:function(_70){if(this.clearOnClose){if(!this.isDirty()){this.inherited(arguments);}else{throw new Error("dojo.data.ItemFileWriteStore: There are unsaved changes present in the store. Please save or revert the changes before invoking close.");}}}});}); \ No newline at end of file