offline-editor-js/dist/offline-edit-min.js
2015-03-27 17:58:27 -06:00

1 line
28 KiB
JavaScript

define(["dojo/Evented","dojo/_base/Deferred","dojo/promise/all","dojo/_base/declare","dojo/_base/array","dojo/dom-attr","dojo/dom-style","dojo/query","esri/config","esri/layers/GraphicsLayer","esri/graphic","esri/symbols/SimpleMarkerSymbol","esri/symbols/SimpleLineSymbol","esri/symbols/SimpleFillSymbol","esri/urlUtils"],function(a,b,c,d,e,f,g,h,i,j,k,l,m,n){"use strict";return d("O.esri.Edit.OfflineFeaturesManager",[a],{_onlineStatus:"online",_featureLayers:{},_editStore:new O.esri.Edit.EditStore,ONLINE:"online",OFFLINE:"offline",RECONNECTING:"reconnecting",attachmentsStore:null,proxyPath:null,DB_NAME:"features_store",DB_OBJECTSTORE_NAME:"features",events:{EDITS_SENT:"edits-sent",EDITS_ENQUEUED:"edits-enqueued",EDITS_ENQUEUED_ERROR:"edits-enqueued-error",EDITS_SENT_ERROR:"edits-sent-error",ALL_EDITS_SENT:"all-edits-sent",ATTACHMENT_ENQUEUED:"attachment-enqueued",ATTACHMENTS_SENT:"attachments-sent"},initAttachments:function(a){if(a=a||function(a){},!this._checkFileAPIs())return a(!1,"File APIs not supported");try{if(this.attachmentsStore=new O.esri.Edit.AttachmentsStore,!this.attachmentsStore.isSupported())return a(!1,"indexedDB not supported");this.attachmentsStore.init(a)}catch(b){}},extend:function(a,d,i){function l(){try{a._phantomLayer=new j({opacity:.8}),a._map.addLayer(a._phantomLayer)}catch(b){}}var m=this;this._initializeDB(i,d),this._featureLayers[a.url]=a,a._applyEdits=a.applyEdits,a._addAttachment=a.addAttachment,a._queryAttachmentInfos=a.queryAttachmentInfos,a._deleteAttachments=a.deleteAttachments,a.queryAttachmentInfos=function(a,c,d){if(m.getOnlineStatus()===m.ONLINE){var e=this._queryAttachmentInfos(a,function(){m.emit(m.events.ATTACHMENTS_INFO,arguments),c&&c.apply(this,arguments)},d);return e}if(m.attachmentsStore){var f=new b;return m.attachmentsStore.getAttachmentsByFeatureId(this.url,a,function(a){c&&c(a),f.resolve(a)}),f}},a.addAttachment=function(a,c,d,e){if(m.getOnlineStatus()===m.ONLINE)return this._addAttachment(a,c,function(){m.emit(m.events.ATTACHMENTS_SENT,arguments),d&&d.apply(this,arguments)},function(a){e&&e.apply(this,arguments)});if(m.attachmentsStore){var f=this._getFilesFromForm(c),g=f[0],i=new b,j=this._getNextTempId();return m.attachmentsStore.store(this.url,j,a,g,function(b,c){var f={attachmentId:j,objectId:a,success:b};if(b){m.emit(m.events.ATTACHMENT_ENQUEUED,f),d&&d(f),i.resolve(f);var g=this._url.path+"/"+a+"/attachments/"+j,k=h("[href="+g+"]");k.attr("href",c.url)}else f.error="can't store attachment",e&&e(f),i.reject(f)}.bind(this)),i}},a.deleteAttachments=function(a,d,e,f){if(m.getOnlineStatus()===m.ONLINE){var g=this._deleteAttachments(a,d,function(){e&&e.apply(this,arguments)},function(a){f&&f.apply(this,arguments)});return g}if(m.attachmentsStore){var h=[];d.forEach(function(c){c=parseInt(c,10);var d=new b;m.attachmentsStore.delete(c,function(b){var e={objectId:a,attachmentId:c,success:b};d.resolve(e)}),h.push(d)},this);var i=c(h);return i.then(function(a){e&&e(a)}),i}},a.applyEdits=function(a,d,e,h,i){var j=[];if(m.getOnlineStatus()===m.ONLINE){var l=this._applyEdits(a,d,e,function(){m.emit(m.events.EDITS_SENT,arguments),h&&h.apply(this,arguments)},i);return l}var n=new b,o={addResults:[],updateResults:[],deleteResults:[]},p={};return this.onBeforeApplyEdits&&this.onBeforeApplyEdits(a,d,e),a=a||[],a.forEach(function(a){var c=new b,d=this._getNextTempId();a.attributes[this.objectIdField]=d;var e=this;m._editStore.pushEdit(m._editStore.ADD,this.url,a,function(b,h){if(1==b){o.addResults.push({success:!0,error:null,objectId:d});var i=new k(a.geometry,m._getPhantomSymbol(a.geometry,m._editStore.ADD),{objectId:d});e._phantomLayer.add(i),m._editStore.pushPhantomGraphic(i,function(a){}),f.set(i.getNode(),"stroke-dasharray","10,4"),g.set(i.getNode(),"pointer-events","none"),c.resolve(b)}else o.addResults.push({success:!1,error:h,objectId:d}),c.reject(h)}),j.push(c)},this),d=d||[],d.forEach(function(a){var c=new b,d=a.attributes[this.objectIdField];p[d]=a;var e=this;m._editStore.pushEdit(m._editStore.UPDATE,this.url,a,function(b,h){if(1==b){o.updateResults.push({success:!0,error:null,objectId:d});var i=new k(a.geometry,m._getPhantomSymbol(a.geometry,m._editStore.UPDATE),{objectId:d});e._phantomLayer.add(i),m._editStore.pushPhantomGraphic(i,function(a){}),f.set(i.getNode(),"stroke-dasharray","5,2"),g.set(i.getNode(),"pointer-events","none"),c.resolve(b)}else o.addResults.push({success:!1,error:h,objectId:d}),c.reject(h)}),j.push(c)},this),e=e||[],e.forEach(function(a){var c=new b,d=a.attributes[this.objectIdField],e=this;m._editStore.pushEdit(m._editStore.DELETE,this.url,a,function(b,h){if(1==b){o.deleteResults.push({success:!0,error:null,objectId:d});var i=new k(a.geometry,m._getPhantomSymbol(a.geometry,m._editStore.DELETE),{objectId:d});e._phantomLayer.add(i),m._editStore.pushPhantomGraphic(i,function(a){}),f.set(i.getNode(),"stroke-dasharray","4,4"),g.set(i.getNode(),"pointer-events","none"),m.attachmentsStore&&m.attachmentsStore.deleteAttachmentsByFeatureId(this.url,d,function(a){}),c.resolve(b)}else o.addResults.push({success:!1,error:h,objectId:d}),c.reject(h)}),j.push(c)},this),c(j).then(function(b){for(var c=!0,d=b.length,e=0;d>e;e++)0==b[e]&&(c=!1);this._editHandler(o,a,p,h,i,n),1==c?m.emit(m.events.EDITS_ENQUEUED,o):m.emit(m.events.EDITS_ENQUEUED_ERROR,o)}.bind(this)),n},a.convertGraphicLayerToJSON=function(a,b,c){var d={};d.objectIdFieldName=b.target.objectIdField,d.globalIdFieldName=b.target.globalIdField,d.geometryType=b.target.geometryType,d.spatialReference=b.target.spatialReference,d.fields=b.target.fields;for(var e=a.length,f=[],g=0;e>g;g++){var h=a[g].toJson();if(f.push(h),g==e-1){var i=JSON.stringify(f),j=JSON.stringify(d);c(i,j);break}}},a.getFeatureLayerJSON=function(a,b){require(["esri/request"],function(c){var d=c({url:a,content:{f:"json"},handleAs:"json",callbackParamName:"callback"});d.then(function(a){b(!0,a)},function(a){b(!1,a.message)})})},a.convertFeatureGraphicsToJSON=function(a,b){for(var c=a.length,d=[],e=0;c>e;e++){var f=a[e].toJson();if(d.push(f),e==c-1){var g=JSON.stringify(d);b(g);break}}},a.setPhantomLayerGraphics=function(a){var b=a.length;if(b>0)for(var c=0;b>c;c++){var d=new k(a[c]);this._phantomLayer.add(d)}},a.getPhantomLayerGraphics=function(b){for(var c=a._phantomLayer.graphics,d=a._phantomLayer.graphics.length,e=[],f=0;d>f;f++){var g=c[f].toJson();if(e.push(g),f==d-1){var h=JSON.stringify(e);b(h);break}}},a.getPhantomGraphicsArray=function(a){m._editStore.getPhantomGraphicsArray(function(b,c){"end"==c?a(!0,b):a(!1,c)})},a.getUsage=function(a){m._editStore.getUsage(function(b,c){a(b,c)})},a.resetDatabase=function(a){m._editStore.resetEditsQueue(function(b,c){a(b,c)})},a.pendingEditsCount=function(a){m._editStore.pendingEditsCount(function(b){a(b)})},a.getFeatureDefinition=function(a,b,c,d){var e={layerDefinition:a,featureSet:{features:b,geometryType:c}};d(e)},a.getAllEditsArray=function(a){m._editStore.getAllEditsArray(function(b,c){"end"==c?a(!0,b):a(!1,c)})},a._getFilesFromForm=function(a){var b=[],c=e.filter(a.elements,function(a){return"file"===a.type});return c.forEach(function(a){b.push.apply(b,a.files)},this),b},a._replaceFeatureIds=function(a,b,c){a.length||c(0);var d,e=a.length,f=e,g=0;for(d=0;e>d;d++)m.attachmentsStore.replaceFeatureId(this.url,a[d],b[d],function(a){--f,g+=a?1:0,0===f&&c(g)}.bind(this))},a._nextTempId=-1,a._getNextTempId=function(){return this._nextTempId--},l()},goOffline:function(){this._onlineStatus=this.OFFLINE},goOnline:function(a){this._onlineStatus=this.RECONNECTING,this._replayStoredEdits(function(b,c){var d={features:{success:b,responses:c}};null!=this.attachmentsStore?this._sendStoredAttachments(function(b,c){this._onlineStatus=this.ONLINE,d.attachments={success:b,responses:c},a&&a(d)}.bind(this)):(this._onlineStatus=this.ONLINE,a&&a(d))}.bind(this))},getOnlineStatus:function(){return this._onlineStatus},_initializeDB:function(a,b){var c=this._editStore;c.dbName=this.DB_NAME,c.objectStoreName=this.DB_OBJECTSTORE_NAME,c.init(function(d,e){"object"==typeof a&&1==d?c.pushFeatureLayerJSON(a,function(a,c){a?b(!0,null):b(!1,c)}):0==d?b(!1,e):1==d&&b(!0,null)})},_checkFileAPIs:function(){return window.File&&window.FileReader&&window.FileList&&window.Blob?(XMLHttpRequest.prototype.sendAsBinary||(XMLHttpRequest.prototype.sendAsBinary=function(a){function b(a){return 255&a.charCodeAt(0)}var c=Array.prototype.map.call(a,b),d=new Uint8Array(c);this.send(d.buffer)}),!0):!1},_extendAjaxReq:function(a){a.sendAsBinary=XMLHttpRequest.prototype.sendAsBinary},_phantomSymbols:[],_getPhantomSymbol:function(a,b){if(0===this._phantomSymbols.length){var c=[0,255,0,255],d=1.5;this._phantomSymbols.point=[],this._phantomSymbols.point[this._editStore.ADD]=new l({type:"esriSMS",style:"esriSMSCross",xoffset:10,yoffset:10,color:[255,255,255,0],size:15,outline:{color:c,width:d,type:"esriSLS",style:"esriSLSSolid"}}),this._phantomSymbols.point[this._editStore.UPDATE]=new l({type:"esriSMS",style:"esriSMSCircle",xoffset:0,yoffset:0,color:[255,255,255,0],size:15,outline:{color:c,width:d,type:"esriSLS",style:"esriSLSSolid"}}),this._phantomSymbols.point[this._editStore.DELETE]=new l({type:"esriSMS",style:"esriSMSX",xoffset:0,yoffset:0,color:[255,255,255,0],size:15,outline:{color:c,width:d,type:"esriSLS",style:"esriSLSSolid"}}),this._phantomSymbols.multipoint=null,this._phantomSymbols.polyline=[],this._phantomSymbols.polyline[this._editStore.ADD]=new m({type:"esriSLS",style:"esriSLSSolid",color:c,width:d}),this._phantomSymbols.polyline[this._editStore.UPDATE]=new m({type:"esriSLS",style:"esriSLSSolid",color:c,width:d}),this._phantomSymbols.polyline[this._editStore.DELETE]=new m({type:"esriSLS",style:"esriSLSSolid",color:c,width:d}),this._phantomSymbols.polygon=[],this._phantomSymbols.polygon[this._editStore.ADD]=new n({type:"esriSFS",style:"esriSFSSolid",color:[255,255,255,0],outline:{type:"esriSLS",style:"esriSLSSolid",color:c,width:d}}),this._phantomSymbols.polygon[this._editStore.UPDATE]=new n({type:"esriSFS",style:"esriSFSSolid",color:[255,255,255,0],outline:{type:"esriSLS",style:"esriSLSDash",color:c,width:d}}),this._phantomSymbols.polygon[this._editStore.DELETE]=new n({type:"esriSFS",style:"esriSFSSolid",color:[255,255,255,0],outline:{type:"esriSLS",style:"esriSLSDot",color:c,width:d}})}return this._phantomSymbols[a.type][b]},_fieldSegment:function(a,b){return'Content-Disposition: form-data; name="'+a+'"\r\n\r\n'+b+"\r\n"},_fileSegment:function(a,b,c,d){return'Content-Disposition: form-data; name="'+a+'"; filename="'+b+'"\r\nContent-Type: '+c+"\r\n\r\n"+d+"\r\n"},_uploadAttachment:function(a){var c=new b,d=[];d.push(this._fieldSegment("f","json")),d.push(this._fileSegment("attachment",a.name,a.contentType,a.content));var e=new XMLHttpRequest;e.sendAsBinary||this._extendAjaxReq(e),e.onload=function(a){c.resolve(JSON.parse(a.target.response))},e.onerror=function(a){c.reject(a)};var f=this.proxyPath||i.defaults.io.proxyUrl||"";""!==f&&(f+="?"),e.open("post",f+a.featureId+"/addAttachment",!0);var g="---------------------------"+Date.now().toString(16);return e.setRequestHeader("Content-Type","multipart/form-data; boundary="+g),e.sendAsBinary("--"+g+"\r\n"+d.join("--"+g+"\r\n")+"--"+g+"--\r\n"),c},_deleteAttachment:function(a,c){var d=new b;return this.attachmentsStore.delete(a,function(a){d.resolve(c)}),d},_sendStoredAttachments:function(a){this.attachmentsStore.getAllAttachments(function(b){var d=[];b.forEach(function(a){var b=this._uploadAttachment(a).then(function(b){return b.addAttachmentResult&&b.addAttachmentResult.success===!0?this._deleteAttachment(a.id,b):null}.bind(this),function(){});d.push(b)},this);var e=c(d);e.then(function(b){a&&a(!0,b)},function(b){a&&a(!1,b)})}.bind(this))},_replayStoredEdits:function(a){var b,d={},e=this,f=[],g=[],h=[],i=[],j=[],l=this._featureLayers,m=this.attachmentsStore,n=this._editStore;this._editStore.getAllEditsArray(function(o){if(o.length>0){j=o;for(var p=j.length,q=0;p>q;q++){if(b=l[j[q].layer],null==m&&b.hasAttachments)throw new Error("OfflineFeaturesManager: Attachments aren't initialized.");b._attachmentsStore=m,b.__onEditsComplete=b.onEditsComplete,b.onEditsComplete=function(){},b.__onBeforeApplyEdits=b.onBeforeApplyEdits,b.onBeforeApplyEdits=function(){},f=[],g=[],h=[],i=[];var r=new k(j[q].graphic);switch(j[q].operation){case n.ADD:for(var s=0;s<b.graphics.length;s++){var t=b.graphics[s];if(t.attributes[b.objectIdField]===r.attributes[b.objectIdField]){b.remove(t);break}}i.push(r.attributes[b.objectIdField]),delete r.attributes[b.objectIdField],f.push(r);break;case n.UPDATE:g.push(r);break;case n.DELETE:h.push(r)}d[q]=e._internalApplyEdits(b,j[q].id,i,f,g,h)}}else a(!0,[]);var u=c(d);u.then(function(b){this._cleanSuccessfulEditsDatabaseRecords(b,function(c){c?(this._editStore.resetLimitedPhantomGraphicsQueue(b,function(){}),this._editStore.resetPhantomGraphicsQueue(function(c){0==c?this.emit(this.events.EDITS_SENT_ERROR,{msg:"Problem deleting phantom graphic(s)"}):this.emit(this.events.ALL_EDITS_SENT,b),a&&a(!0,b)}.bind(this))):(this.emit(this.events.EDITS_SENT_ERROR,{msg:b}),a&&a(!1,b))}.bind(e))}.bind(e),function(b){a&&a(!1,b)}.bind(e))})},_cleanSuccessfulEditsDatabaseRecords:function(a,b){if(0!==Object.keys(a).length){var d=[],e=[];for(var f in a)if(a.hasOwnProperty(f)){var g=a[f],h={};g.updateResults.length>0&&(g.updateResults[0].success?(h.layer=g.layer,h.id=g.updateResults[0].objectId,d.push(h)):e.push(g)),g.deleteResults.length>0&&(g.deleteResults[0].success?(h.layer=g.layer,h.id=g.deleteResults[0].objectId,d.push(h)):e.push(g)),g.addResults.length>0&&(g.addResults[0].success?(h.layer=g.layer,h.id=g.tempId,d.push(h)):e.push(g))}for(var i={},j=d.length,k=0;j>k;k++)i[k]=this._updateDatabase(d[k]);var l=c(i);l.then(function(a){e.length>0?b(!1,a):b(!0,a)},function(a){b(!1,a)})}else b(!0,{})},_updateDatabase:function(a){var c=new b,d={};return d.attributes={},d.attributes.objectid=a.id,this._editStore.delete(a.layer,d,function(b,d){if(b){{this._editStore.PHANTOM_GRAPHIC_PREFIX+this._editStore._PHANTOM_PREFIX_TOKEN+a.id}c.resolve({success:!0,error:null})}else c.reject({success:!1,error:d})}.bind(this)),c.promise},getFeatureLayerJSON:function(a,b){require(["esri/request"],function(c){var d=c({url:a,content:{f:"json"},handleAs:"json",callbackParamName:"callback"});d.then(function(a){b(!0,a)},function(a){b(!1,a.message)})})},_internalApplyEdits:function(a,c,d,e,f,g){var h=new b;return a._applyEdits(e,f,g,function(b,e,f){a._phantomLayer.clear(),a.onBeforeApplyEdits=a.__onBeforeApplyEdits,delete a.__onBeforeApplyEdits;var g=b.map(function(a){return a.objectId});null!=a._attachmentsStore&&a.hasAttachments&&d.length>0?a._replaceFeatureIds(d,g,function(){h.resolve({id:c,layer:a.url,tempId:d,addResults:b,updateResults:e,deleteResults:f})}):h.resolve({id:c,layer:a.url,tempId:d,addResults:b,updateResults:e,deleteResults:f})},function(b){a.onEditsComplete=a.__onEditsComplete,delete a.__onEditsComplete,a.onBeforeApplyEdits=a.__onBeforeApplyEdits,delete a.__onBeforeApplyEdits,h.reject(b)}),h.promise},_validateFeature:function(a){var c=new b;return this._editStore.getEdit(a.id,function(b,d){if(b)switch(a.operation){case this._editStore.ADD:c.resolve(a);break;case this._editStore.UPDATE:(d.operation=this._editStore.ADD)&&(a.operation=this._editStore.ADD),c.resolve(a);break;case this._editStore.DELETE:d.operation=this._editStore.ADD,c.resolve(a)}else c.resolve(a)}),c},_optimizeEditsQueue:function(){return"DEPRECATED at v2.5!"},getReadableEdit:function(){return"DEPRECATED at v2.5!"}})}),"undefined"!=typeof O?O.esri.Edit={}:(O={},O.esri={Edit:{}}),O.esri.Edit.EditStore=function(){this._db=null,this.dbName="features_store",this.objectStoreName="features";var a=this.dbName,b=this.objectStoreName,c="featureId";this.ADD="add",this.UPDATE="update",this.DELETE="delete",this.FEATURE_LAYER_JSON_ID="feature-layer-object-1001",this.PHANTOM_GRAPHIC_PREFIX="phantom-layer",this._PHANTOM_PREFIX_TOKEN="|@|",this.isSupported=function(){return window.indexedDB?!0:!1},this.pushEdit=function(a,c,d,e){var f={id:c+"/"+d.attributes.objectid,operation:a,layer:c,type:d.geometry.type,graphic:d.toJson()},g=this._db.transaction([b],"readwrite");g.oncomplete=function(){e(!0)},g.onerror=function(a){e(!1,a.target.error.message)};var h=g.objectStore(b);h.put(f)},this.pushFeatureLayerJSON=function(a,c){"object"!=typeof a&&c(!1,"dataObject type is not an object.");var d=this._db;a.id=this.FEATURE_LAYER_JSON_ID,this.getFeatureLayerJSON(function(e,f){if(e&&"undefined"!=typeof f){var g=d.transaction([b],"readwrite").objectStore(b);for(var h in a)a.hasOwnProperty(h)&&(f[h]=a[h]);var i=g.put(f);i.onsuccess=function(){c(!0,null)},i.onerror=function(a){c(!1,a)}}else{var j=d.transaction([b],"readwrite");j.oncomplete=function(){c(!0,null)},j.onerror=function(a){c(!1,a.target.error.message)};var g=j.objectStore(b);try{g.put(a)}catch(k){c(!1,JSON.stringify(k))}}})},this.getFeatureLayerJSON=function(a){var c=this._db.transaction([b],"readwrite").objectStore(b),d=c.get(this.FEATURE_LAYER_JSON_ID);d.onsuccess=function(){var b=d.result;"undefined"!=typeof b?a(!0,b):a(!1,"nothing found")},d.onerror=function(b){a(!1,b)}},this.deleteFeatureLayerJSON=function(a){var c=this._db,d=null,e=this,f=this.FEATURE_LAYER_JSON_ID;require(["dojo/Deferred"],function(g){d=new g,d.then(function(){e.editExists(f).then(function(b){0==b.success?a(!0,{message:"id does not exist"}):a(!1,{message:null})},function(){a(!0,{message:"id does not exist"})})},function(){a(!1,{message:"id does not exist"})}),e.editExists(f).then(function(a){if(a&&a.success){var e=c.transaction([b],"readwrite").objectStore(b),g=e.delete(f);g.onsuccess=function(){d.resolve(!0)},g.onerror=function(a){d.reject({success:!1,error:a})}}else d.reject({success:!1,message:"id does not exist"})},function(a){d.reject({success:!1,message:a})})})},this.pushPhantomGraphic=function(a,c){var d=this._db,e=this.PHANTOM_GRAPHIC_PREFIX+this._PHANTOM_PREFIX_TOKEN+a.attributes.objectId,f={id:e,graphic:a.toJson()},g=d.transaction([b],"readwrite");g.oncomplete=function(){c(!0,null)},g.onerror=function(a){c(!1,a.target.error.message)};var h=g.objectStore(b);h.put(f)},this.getPhantomGraphicsArray=function(a){var c=[];if(null!==this._db){var d=this.PHANTOM_GRAPHIC_PREFIX,e=this._db.transaction([b]).objectStore(b).openCursor();e.onsuccess=function(b){var e=b.target.result;e&&e.value&&e.value.id?(-1!=e.value.id.indexOf(d)&&c.push(e.value),e.continue()):a(c,"end")}.bind(this),e.onerror=function(b){a(null,b)}}else a(null,"no db")},this._getPhantomGraphicsArraySimple=function(a){var c=[];if(null!==this._db){var d=this.PHANTOM_GRAPHIC_PREFIX,e=this._db.transaction([b]).objectStore(b).openCursor();e.onsuccess=function(b){var e=b.target.result;e&&e.value&&e.value.id?(-1!=e.value.id.indexOf(d)&&c.push(e.value.id),e.continue()):a(c,"end")}.bind(this),e.onerror=function(b){a(null,b)}}else a(null,"no db")},this.deletePhantomGraphic=function(a,c){var d=this._db,e=null,f=this;require(["dojo/Deferred"],function(g){e=new g,f.editExists(a).then(function(g){if(g.success){e.then(function(){f.editExists(a).then(function(a){c(0==a.success?!0:!1)},function(){c(!0)})},function(a){c(!1,a)});var h=d.transaction([b],"readwrite").objectStore(b),i=h.delete(a);i.onsuccess=function(){e.resolve(!0)},i.onerror=function(a){e.reject({success:!1,error:a})}}},function(){c(!1)})})},this.resetLimitedPhantomGraphicsQueue=function(a,c){if(Object.keys(a).length>0){var d=this._db,e=0,f=d.transaction([b],"readwrite"),g=f.objectStore(b);g.onerror=function(){e++},f.oncomplete=function(){c(0==e?!0:!1)};for(var h in a)if(a.hasOwnProperty(h)){var i=a[h],j=this.PHANTOM_GRAPHIC_PREFIX+this._PHANTOM_PREFIX_TOKEN+i.id;i.updateResults.length>0&&i.updateResults[0].success&&g.delete(j),i.deleteResults.length>0&&i.deleteResults[0].success&&g.delete(j),i.addResults.length>0&&i.addResults[0].success&&g.delete(j)}c(!0)}else c(!0)},this.resetPhantomGraphicsQueue=function(a){var c=this._db;this._getPhantomGraphicsArraySimple(function(d){if(d!=[]){var e=0,f=c.transaction([b],"readwrite"),g=f.objectStore(b);g.onerror=function(){e++},f.oncomplete=function(){a(0==e?!0:!1)};for(var h=d.length,i=0;h>i;i++)g.delete(d[i])}else a(!0)})},this.getEdit=function(a,c){require(["dojo/Deferred"],function(){var d=db.transaction([b],"readwrite").objectStore(b),e=d.get(a);e.onsuccess=function(){var b=e.result;b&&b.id==a?c(!0,b):c(!1,"Id not found")},e.onerror=function(a){c(!1,a)}})},this.getAllEdits=function(a){if(null!==this._db){var c=this.FEATURE_LAYER_JSON_ID,d=this.PHANTOM_GRAPHIC_PREFIX,e=this._db.transaction([b]).objectStore(b).openCursor();e.onsuccess=function(b){var e=b.target.result;e&&e.hasOwnProperty("value")&&e.value.hasOwnProperty("id")?(e.value.id!==c&&-1==e.value.id.indexOf(d)&&a(e.value,null),e.continue()):a(null,"end")}.bind(this),e.onerror=function(b){a(null,b)}}else a(null,"no db")},this.getAllEditsArray=function(a){var c=[];if(null!==this._db){var d=this.FEATURE_LAYER_JSON_ID,e=this.PHANTOM_GRAPHIC_PREFIX,f=this._db.transaction([b]).objectStore(b).openCursor();f.onsuccess=function(b){var f=b.target.result;f&&f.value&&f.value.id?(f.value.id!==d&&-1==f.value.id.indexOf(e)&&c.push(f.value),f.continue()):a(c,"end")}.bind(this),f.onerror=function(b){a(null,b)}}else a(null,"no db")},this.updateExistingEdit=function(a,c,d,e){var f=this._db.transaction([b],"readwrite").objectStore(b),g=f.get(d.attributes.objectid);g.onsuccess=function(){var b=(g.result,{id:c+"/"+d.attributes.objectid,operation:a,layer:c,graphic:d.toJson()}),h=f.put(b);h.onsuccess=function(){e(!0)},h.onerror=function(a){e(!1,a)}}},this.delete=function(a,c,d){var e=this._db,f=null,g=this,h=a+"/"+c.attributes.objectid;require(["dojo/Deferred"],function(a){f=new a,g.editExists(h).then(function(a){if(a.success){f.then(function(){g.editExists(h).then(function(a){d(0==a.success?!0:!1)},function(){d(!0)})},function(a){d(!1,a)});var c=e.transaction([b],"readwrite").objectStore(b),i=c.delete(h);i.onsuccess=function(){f.resolve(!0)},i.onerror=function(a){f.reject({success:!1,error:a})}}},function(){d(!1)})})},this.resetEditsQueue=function(a){var c=this._db.transaction([b],"readwrite").objectStore(b).clear();c.onsuccess=function(){setTimeout(function(){a(!0)},0)},c.onerror=function(b){a(!1,b)}},this.pendingEditsCount=function(a){var c=0,d=this.FEATURE_LAYER_JSON_ID,e=this.PHANTOM_GRAPHIC_PREFIX,f=this._db.transaction([b],"readwrite"),g=f.objectStore(b);g.openCursor().onsuccess=function(b){var f=b.target.result;f&&f.value&&f.value.id&&-1==f.value.id.indexOf(e)?(f.value.id!==d&&c++,f.continue()):a(c)}},this.editExists=function(a){var c=this._db,d=null;return require(["dojo/Deferred"],function(e){d=new e;var f=c.transaction([b],"readwrite").objectStore(b),g=f.get(a);g.onsuccess=function(){var b=g.result;b&&b.id==a?d.resolve({success:!0,error:null}):d.reject({success:!1,error:"Layer id is not a match."})},g.onerror=function(a){d.reject({success:!1,error:a})}}),d},this.getUsage=function(a){var c=this.FEATURE_LAYER_JSON_ID,d=this.PHANTOM_GRAPHIC_PREFIX,e={sizeBytes:0,editCount:0},f=this._db.transaction([b]).objectStore(b).openCursor();f.onsuccess=function(b){var f=b.target.result;if(f&&f.value&&f.value.id){var g=f.value,h=JSON.stringify(g);e.sizeBytes+=h.length,-1==f.value.id.indexOf(d)&&f.value.id!==c&&(e.editCount+=1),f.continue()}else a(e,null)},f.onerror=function(b){a(null,b)}},this._serialize=function(a){var b=a.toJson(),c={attributes:b.attributes,geometry:b.geometry,infoTemplate:b.infoTemplate,symbol:b.symbol};return JSON.stringify(c)},this._deserialize=function(a){var b;return require(["esri/graphic"],function(c){b=new c(JSON.parse(a))}),b},this.init=function(d){var e=indexedDB.open(a,11);d=d||function(a){}.bind(this),e.onerror=function(a){d(!1,a.target.errorCode)}.bind(this),e.onupgradeneeded=function(a){var d=a.target.result;d.objectStoreNames.contains(b)&&d.deleteObjectStore(b);var e=d.createObjectStore(b,{keyPath:"id"});e.createIndex(c,c,{unique:!1})}.bind(this),e.onsuccess=function(a){this._db=a.target.result,d(!0)}.bind(this)},this.hasPendingEdits=function(){return"DEPRECATED at v2.5!"},this._isEditDuplicated=function(){return"DEPRECATED at v2.5!"},this._storeEditsQueue=function(){return"DEPRECATED at v2.5!"},this._unpackArrayOfEdits=function(){return"DEPRECATED at v2.5!"},this.getLocalStorageSizeBytes=function(){return"DEPRECATED at v2.5!"},this.peekFirstEdit=function(){return"DEPRECATED at v2.5!"},this.popFirstEdit=function(){return"DEPRECATED at v2.5!"}},O.esri.Edit.AttachmentsStore=function(){this._db=null;var a="attachments_store",b="attachments";this.isSupported=function(){return window.indexedDB?!0:!1},this.store=function(a,c,d,e,f){try{this._readFile(e,function(g){var h={id:c,objectId:d,featureId:a+"/"+d,contentType:e.type,name:e.name,size:e.size,url:this._createLocalURL(e),content:g},i=this._db.transaction([b],"readwrite");i.oncomplete=function(){f(!0,h)},i.onerror=function(a){f(!1,a.target.error.message)};var j=i.objectStore(b),k=j.put(h);k.onsuccess=function(){}}.bind(this))}catch(g){f(!1,g.stack)}},this.retrieve=function(a,c){var d=this._db.transaction([b]).objectStore(b),e=d.get(a);e.onsuccess=function(a){var b=a.target.result;b?c(!0,b):c(!1,"not found")},e.onerror=function(a){c(!1,a)}},this.getAttachmentsByFeatureId=function(a,c,d){var e=a+"/"+c,f=[],g=this._db.transaction([b]).objectStore(b),h=g.index("featureId"),i=IDBKeyRange.only(e);h.openCursor(i).onsuccess=function(a){var b=a.target.result;b?(f.push(b.value),b.continue()):d(f)}},this.getAttachmentsByFeatureLayer=function(a,c){var d=[],e=this._db.transaction([b]).objectStore(b),f=e.index("featureId"),g=IDBKeyRange.bound(a+"/",a+"/A");f.openCursor(g).onsuccess=function(a){var b=a.target.result;b?(d.push(b.value),b.continue()):c(d)}},this.getAllAttachments=function(a){var c=[],d=this._db.transaction([b]).objectStore(b);d.openCursor().onsuccess=function(b){var d=b.target.result;d?(c.push(d.value),d.continue()):a(c)}},this.deleteAttachmentsByFeatureId=function(a,c,d){var e=a+"/"+c,f=this._db.transaction([b],"readwrite").objectStore(b),g=f.index("featureId"),h=IDBKeyRange.only(e),i=0;g.openCursor(h).onsuccess=function(a){var b=a.target.result;if(b){var c=b.value;this._revokeLocalURL(c),f.delete(b.primaryKey),i++,b.continue()}else setTimeout(function(){d(i)},0)}.bind(this)},this.delete=function(a,c){this.retrieve(a,function(d,e){if(!d)return void c(!1,"attachment "+a+" not found");this._revokeLocalURL(e);var f=this._db.transaction([b],"readwrite").objectStore(b).delete(a);f.onsuccess=function(){setTimeout(function(){c(!0)},0)},f.onerror=function(a){c(!1,a)}}.bind(this))},this.deleteAll=function(a){this.getAllAttachments(function(c){c.forEach(function(a){this._revokeLocalURL(a)},this);var d=this._db.transaction([b],"readwrite").objectStore(b).clear();d.onsuccess=function(){setTimeout(function(){a(!0)},0)},d.onerror=function(b){a(!1,b)}}.bind(this))},this.replaceFeatureId=function(a,c,d,e){var f=a+"/"+c,g=this._db.transaction([b],"readwrite").objectStore(b),h=g.index("featureId"),i=IDBKeyRange.only(f),j=0;h.openCursor(i).onsuccess=function(b){var c=b.target.result;if(c){var f=a+"/"+d,h=c.value;h.featureId=f,h.objectId=d,g.put(h),j++,c.continue()}else setTimeout(function(){e(j)},1)}},this.getUsage=function(a){var c={sizeBytes:0,attachmentCount:0},d=this._db.transaction([b]).objectStore(b).openCursor();d.onsuccess=function(b){var d=b.target.result;if(d){var e=d.value,f=JSON.stringify(e);c.sizeBytes+=f.length,c.attachmentCount+=1,d.continue()}else a(c,null)}.bind(this),d.onerror=function(b){a(null,b)}},this._readFile=function(a,b){var c=new FileReader;c.onload=function(a){b(a.target.result)},c.readAsBinaryString(a)},this._createLocalURL=function(a){return window.URL.createObjectURL(a)},this._revokeLocalURL=function(a){window.URL.revokeObjectURL(a.url)},this.init=function(c){var d=indexedDB.open(a,11);c=c||function(a){}.bind(this),d.onerror=function(a){c(!1,a.target.errorCode)}.bind(this),d.onupgradeneeded=function(a){var c=a.target.result;c.objectStoreNames.contains(b)&&c.deleteObjectStore(b);var d=c.createObjectStore(b,{keyPath:"id"});d.createIndex("featureId","featureId",{unique:!1})}.bind(this),d.onsuccess=function(a){this._db=a.target.result,c(!0)}.bind(this)}};