"use strict"; define([ "edit/editsStore", "edit/attachmentsStore", "dojo/Evented", "dojo/_base/Deferred", "dojo/promise/all", "dojo/_base/declare", "dojo/_base/array", "dojo/dom-attr", "dojo/dom-style", "esri/layers/GraphicsLayer", "esri/graphic", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol"], function(editsStore, AttachmentsStore, Evented,Deferred,all,declare,array,domAttr,domStyle, GraphicsLayer,Graphic,SimpleMarkerSymbol,SimpleLineSymbol,SimpleFillSymbol) { return declare([Evented], { _onlineStatus: "online", _featureLayers: {}, ONLINE: "online", // all edits will directly go to the server OFFLINE: "offline", // edits will be enqueued RECONNECTING: "reconnecting", // sending stored edits to the server // manager emits event when... events: { EDITS_SENT: 'edits-sent', // ...whenever any edit is actually sent to the server EDITS_ENQUEUED: 'edits-enqueued', // ...when an edit is enqueued (and not sent to the server) ALL_EDITS_SENT: 'all-edits-sent', // ...after going online and there are no pending edits in the queue ATTACHMENT_ENQUEUED: 'attachment-enqueued', ATTACHMENTS_SENT: 'attachments-sent' }, /** * Need to call this method only if you want to support offline attachments * it is optional * @param callback(success) * @returns void */ initAttachments: function(callback) { callback = callback || function(success) { console.log("attachments inited ", success? "ok" : "failed"); }; if( !this._checkFileAPIs()) { return callback(false, "File APIs not supported"); } try { this.attachmentsStore = new AttachmentsStore(); if( /*false &&*/ this.attachmentsStore.isSupported() ) { this.attachmentsStore.init(callback); } else { return callback(false, "indexedDB not supported"); } } catch(err) { console.log("problem! " + err.toString()); } }, /** * internal method that checks if this browser supports everything that is needed to handle offline attachments * it also extends XMLHttpRequest with sendAsBinary() method, needed in Chrome */ _checkFileAPIs: function() { if( window.File && window.FileReader && window.FileList && window.Blob ) { console.log("All APIs supported!"); if(!XMLHttpRequest.prototype.sendAsBinary ) { // https://code.google.com/p/chromium/issues/detail?id=35705#c40 XMLHttpRequest.prototype.sendAsBinary = function(datastr) { function byteValue(x) { return x.charCodeAt(0) & 0xff; } var ords = Array.prototype.map.call(datastr, byteValue); var ui8a = new Uint8Array(ords); this.send(ui8a.buffer); }; console.log("extending XMLHttpRequest"); } return true; } alert('The File APIs are not fully supported in this browser.'); return false; }, /** * internal method that extends an object with sendAsBinary() method * sometimes extending XMLHttpRequest.prototype is not enough, as ArcGIS JS lib seems to mess with this object too * @param oAjaxReq object to extend */ _extendAjaxReq: function(oAjaxReq) { oAjaxReq.sendAsBinary = XMLHttpRequest.prototype.sendAsBinary; console.log("extending XMLHttpRequest"); }, /** * Overrides a feature layer. * @param layer * @returns deferred */ extend: function(layer) { var self = this; // we keep track of the FeatureLayer object this._featureLayers[ layer.url ] = layer; // replace the applyEdits() method layer._applyEdits = layer.applyEdits; // attachments layer._addAttachment = layer.addAttachment; layer._queryAttachmentInfos = layer.queryAttachmentInfos; layer._deleteAttachments = layer.deleteAttachments; /* operations supported offline: 1. add a new attachment to an existing feature (DONE) 2. add a new attachment to a new feature (DONE) 3. remove an attachment that is already in the server... (NOT YET) 4. remove an attachment that is not in the server yet (DONE) 5. update an existing attachment to an existing feature (NOT YET) 6. update a new attachment (NOT YET) concerns: - manage the relationship between offline features and attachments: what if the user wants to add an attachment to a feature that is still offline? we need to keep track of objectids so that when the feature is sent to the server and receives a final objectid we replace the temporary negative id by its final objectid (DONE) - what if the user deletes an offline feature that had offline attachments? we need to discard the attachment (DONE) pending tasks: - delete attachment (DONE) - send attachments to server when reconnecting (DONE) - check for hasAttachments attribute in the FeatureLayer (NOT YET) */ // // attachments // layer.queryAttachmentInfos = function(objectId,callback,errback) { if( self.getOnlineStatus() === self.ONLINE) { var def = this._queryAttachmentInfos(objectId, function() { console.log(arguments); self.emit(self.events.ATTACHMENTS_INFO,arguments); callback && callback.apply(this,arguments); }, errback); return def; } if( !self.attachmentsStore ) { console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager"); } // will only return LOCAL attachments var deferred = new Deferred(); self.attachmentsStore.getAttachmentsByFeatureId(this.url, objectId, function(attachments) { callback && callback(attachments); deferred.resolve(attachments); }); return deferred; }; layer.addAttachment = function(objectId,formNode,callback,errback) { if( self.getOnlineStatus() === self.ONLINE) { return this._addAttachment(objectId,formNode, function() { console.log(arguments); self.emit(self.events.ATTACHMENTS_SENT,arguments); callback && callback.apply(this,arguments); }, function(err) { console.log("addAttachment: " + err); errback && errback.apply(this,arguments); } ); } if( !self.attachmentsStore ) { console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager"); } var deferred = new Deferred(); console.assert(objectId); // get input node(s) // read file from input into variable this._readFilesFromForm(formNode, function(files) { console.log(files); console.assert(files.length === 1, "we don't support multiple files (yet?)"); files.forEach(function(file) { // store the attachment var attachmentId = this._getNextTempId(); self.attachmentsStore.store(this.url,attachmentId, objectId, file, function(success) { var returnValue = { attachmentId: attachmentId, objectId: objectId, success: success }; if( success ) { self.emit(self.events.ATTACHMENT_ENQUEUED,returnValue); callback && callback(returnValue); deferred.resolve(returnValue); // PROBLEM HERE: theoretically, the form can have multiple input[file] controls // and each input can contain multiple files BUT here we call the callback and // resolve the deferred with the FIRST of the files // In practice, popups only have one input, and it doesn't allow multiple files } else { returnValue.error = "can't store attachment"; errback && errback(returnValue); deferred.reject(returnValue); } }); },this); }.bind(this)); return deferred; }; layer.deleteAttachments = function(objectId,attachmentsIds,callback,errback) { if( self.getOnlineStatus() === self.ONLINE) { var def = this._deleteAttachments(objectId,attachmentsIds, function() { callback && callback.apply(this,arguments); }, function(err) { console.log("deleteAttachments: " + err); errback && errback.apply(this,arguments); }); return def; } if( !self.attachmentsStore ) { console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager"); } // case 1.- it is a new attachment // case 2.- it is an already existing attachment // only case 1 is supported right now // asynchronously delete each of the attachments var promises = []; attachmentsIds.forEach(function(attachmentId) { attachmentId = parseInt(attachmentId,10); // to number console.assert( attachmentId<0 , "we only support deleting local attachments"); var deferred = new Deferred(); self.attachmentsStore.delete(attachmentId, function(success) { var result = { objectId: objectId, attachmentId: attachmentId, success: success }; deferred.resolve(result); }); promises.push(deferred); }, this); // call callback once all deletes have finished var allPromises = all(promises); allPromises.then( function(results) { callback && callback(results); }); return allPromises; }; // // other functions // /** * Overrides the ArcGIS API for JavaSccript applyEdits() method. * @param adds Creates a new edit entry. * @param updates Updates an existing entry. * @param deletes Deletes an existing entry. * @param callback Called when the operation is complete. * @param errback An error object is returned if an error occurs * @returns {*} deferred */ layer.applyEdits = function(adds,updates,deletes,callback,errback) { // inside this method, 'this' will be the FeatureLayer // and 'self' will be the offlineFeatureLayer object if( self.getOnlineStatus() === self.ONLINE) { var def = this._applyEdits(adds,updates,deletes, function() { self.emit(self.events.EDITS_SENT,arguments); callback && callback.apply(this,arguments); }, errback); return def; } var deferred = new Deferred(); var results = { addResults:[],updateResults:[], deleteResults:[] }; var updatesMap = {}; this.onBeforeApplyEdits(adds, updates, deletes); adds = adds || []; adds.forEach(function(addEdit) { var objectId = this._getNextTempId(); addEdit.attributes[ this.objectIdField ] = objectId; var result = editsStore.pushEdit(editsStore.ADD, this.url, addEdit); results.addResults.push({ success:result.success, error: result.error, objectId: objectId}); if(result.success) { var phantomAdd = new Graphic( addEdit.geometry, self._getPhantomSymbol(addEdit.geometry, editsStore.ADD), { objectId: objectId }); this._phantomLayer.add(phantomAdd); domAttr.set(phantomAdd.getNode(),"stroke-dasharray","10,4"); domStyle.set(phantomAdd.getNode(), "pointer-events","none"); } },this); updates = updates || []; updates.forEach(function(updateEdit) { var objectId = updateEdit.attributes[ this.objectIdField ]; var result = editsStore.pushEdit(editsStore.UPDATE, this.url, updateEdit); results.updateResults.push({success:result.success, error: result.error, objectId: objectId}); updatesMap[ objectId ] = updateEdit; if(result.success) { var phantomUpdate = new Graphic( updateEdit.geometry, self._getPhantomSymbol(updateEdit.geometry, editsStore.UPDATE), { objectId: objectId }); this._phantomLayer.add(phantomUpdate); domAttr.set(phantomUpdate.getNode(),"stroke-dasharray","5,2"); domStyle.set(phantomUpdate.getNode(), "pointer-events","none"); } },this); deletes = deletes || []; deletes.forEach(function(deleteEdit) { var objectId = deleteEdit.attributes[ this.objectIdField ]; var result = editsStore.pushEdit(editsStore.DELETE, this.url, deleteEdit); results.deleteResults.push({success:result.success, error: result.error, objectId: objectId}); if(result.success) { var phantomDelete = new Graphic( deleteEdit.geometry, self._getPhantomSymbol(deleteEdit.geometry, editsStore.DELETE), { objectId: objectId }); this._phantomLayer.add(phantomDelete); domAttr.set(phantomDelete.getNode(),"stroke-dasharray","4,4"); domStyle.set(phantomDelete.getNode(), "pointer-events","none"); } // delete attachments of this feature, if any... we just launch the delete and don't wait for it to complete self.attachmentsStore.deleteAttachmentsByFeatureId(this.url, objectId, function(deletedCount) { console.log("deleted",deletedCount,"attachments of feature",objectId); }); },this); /* we already pushed the edits into the local store, now we let the FeatureLayer to do the local updating of the layer graphics */ setTimeout(function() { this._editHandler(results, adds, updatesMap, callback, errback, deferred); self.emit(self.events.EDITS_ENQUEUED, results); }.bind(this),0); return deferred; }; // layer.applyEdits() /* internal methods */ layer._readFilesFromForm = function(formNode, callback) { var inputNodes = array.filter(formNode.elements, function(node) { return node.type === "file"; }); console.assert(inputNodes.length <= 1, "we don't support multiple input nodes in one form (yet?)"); inputNodes.forEach(function(inputNode) { var file, files = []; var pendingFiles = inputNode.files.length; var i, n = inputNode.files.length; var reader; if( !pendingFiles ) { return callback(files); } for(i=0;i 0) { layer._replaceFeatureIds(tempObjectIds,newObjectIds,function(success) { dfd.resolve({addResults:addResults,updateResults:updateResults,deleteResults:deleteResults}); // wrap three arguments in a single object }); } else { dfd.resolve({addResults:addResults,updateResults:updateResults,deleteResults:deleteResults}); // wrap three arguments in a single object } }, function(error) { layer.onEditsComplete = layer.__onEditsComplete; delete layer.__onEditsComplete; layer.onBeforeApplyEdits = layer.__onBeforeApplyEdits; delete layer.__onBeforeApplyEdits; dfd.reject(error); } ); return dfd; }(layer,tempObjectIds)); } } // // wait for all requests to finish // var allPromises = all(promises); allPromises.then( function(responses) { console.log("all responses are back"); this.emit(this.events.EDITS_SENT); this.emit(this.events.ALL_EDITS_SENT); callback && callback(true,responses); }.bind(this), function(errors) { console.log("ERROR!!"); console.log(errors); callback && callback(false,errors); }.bind(this)); } // hasPendingEdits() else { this.emit(this.events.ALL_EDITS_SENT); callback && callback(true, {}); } } }); // declare }); // define