aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/models
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2014-10-03 15:07:08 +0100
committerDave Arter <davea@mysociety.org>2015-08-28 14:27:15 +0100
commita69d425c0e5c004145ac1ab70e2f7f9fc329b54c (patch)
tree8eb7ff0ff7aaa0fce8bc7214ec6e15b694fddbdc /www/js/models
parent8fd15b58733c51d7f001f9eac66b7d03830ec0b4 (diff)
update Android to Cordova 3.6
Required due to security issue Remove Android directory as no longer required, move src -> www to match standard layout, update .gitignore to avoid including the standard platform files, update README based on Steve's zurich one
Diffstat (limited to 'www/js/models')
-rw-r--r--www/js/models/draft.js58
-rw-r--r--www/js/models/report.js223
-rw-r--r--www/js/models/user.js17
3 files changed, 298 insertions, 0 deletions
diff --git a/www/js/models/draft.js b/www/js/models/draft.js
new file mode 100644
index 0000000..ec6cff6
--- /dev/null
+++ b/www/js/models/draft.js
@@ -0,0 +1,58 @@
+(function(FMS, Backbone, _, $, moment) {
+ _.extend( FMS, {
+ Draft: Backbone.Model.extend({
+ localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-drafts'),
+
+ defaults: {
+ lat: 0,
+ lon: 0,
+ title: '',
+ details: '',
+ may_show_name: '',
+ category: '',
+ phone: '',
+ pc: '',
+ file: '',
+ created: moment.utc()
+ },
+
+ description: function() {
+ var desc = '';
+ if ( this.get('title') ) {
+ desc += this.get('title');
+ } else {
+ desc += 'Untitled draft';
+ }
+ desc += '<br><small>' + moment.utc( this.get('created') ).fromNow() + '</small>';
+
+ return desc;
+ },
+
+ isPartial: function() {
+ if (
+ this.get('title') ||
+ this.get('details') ||
+ this.get('category') ||
+ this.get('file')
+ ) {
+ return true;
+ }
+
+ return false;
+ },
+
+ createdDate: function() {
+ return moment.utc( this.get('created') ).format( 'H:mm Do MMM' );
+ }
+ })
+ });
+})(FMS, Backbone, _, $, moment);
+
+(function(FMS, Backbone, _, $) {
+ _.extend( FMS, {
+ Drafts: Backbone.Collection.extend({
+ model: FMS.Draft,
+ localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-drafts')
+ })
+ });
+})(FMS, Backbone, _, $);
diff --git a/www/js/models/report.js b/www/js/models/report.js
new file mode 100644
index 0000000..9e6290c
--- /dev/null
+++ b/www/js/models/report.js
@@ -0,0 +1,223 @@
+(function(FMS, Backbone, _, $) {
+ _.extend( FMS, {
+ Report: Backbone.Model.extend({
+ urlRoot: CONFIG.FMS_URL + 'report/ajax',
+
+ defaults: {
+ lat: 0,
+ lon: 0,
+ title: '',
+ details: '',
+ may_show_name: '',
+ category: '',
+ phone: '',
+ pc: '',
+ file: ''
+ },
+
+ sync: function(method, model, options) {
+ switch (method) {
+ case 'update':
+ case 'create':
+ this.post(model,options);
+ break;
+ case 'read':
+ Backbone.ajaxSync(method, model, options);
+ break;
+ default:
+ return true;
+ }
+ },
+
+ parse: function(res) {
+ if ( res.report && res.report.latitude ) {
+ return {
+ lat: res.report.latitude,
+ lon: res.report.longitude,
+ title: res.report.title,
+ details: res.report.detail,
+ photo: res.report.photo && res.report.photo.url ? CONFIG.FMS_URL + res.report.photo.url : null,
+ meta: res.report.meta,
+ confirmed_pp: res.report.confirmed_pp,
+ created_pp: res.report.created_pp,
+ category: res.report.category,
+ state: res.report.state,
+ state_t: res.report.state_t,
+ is_fixed: res.report.is_fixed,
+ used_map: res.report.used_map,
+ update_time: res.updates ? res.updates.update_pp : null,
+ update: res.updates ? res.updates.details : null
+ };
+ }
+ return false;
+ },
+
+ post: function(model,options) {
+
+ var params = {
+ service: device.platform,
+ title: model.get('title'),
+ detail: model.get('details'),
+ category: model.get('category'),
+ lat: model.get('lat'),
+ lon: model.get('lon'),
+ pc: model.get('pc'),
+ may_show_name: model.get('may_show_name') ? 1 : 0,
+ used_map: 1,
+ name: model.get('name') || model.get('user').get('name'),
+ email: model.get('email') || model.get('user').get('email'),
+ phone: model.get('phone'),
+ fms_extra_title: model.get('user').get('title')
+ };
+
+ var extra_fields = model.get('extra_details');
+ if ( extra_fields && extra_fields.length > 0 ) {
+ for ( var i = 0; i < extra_fields.length; i++ ) {
+ params[extra_fields[i]] = model.get(extra_fields[i]);
+ }
+ }
+
+ if ( model.get('submit_clicked') == 'submit_sign_in' ) {
+ params.submit_sign_in = 1;
+ params.password_sign_in = model.get('user').get('password');
+ params.remember_me = 1;
+ } else {
+ params.password_register = model.get('user').get('password') || '';
+ params.submit_register = 1;
+ }
+
+ var that = this;
+ if ( model.get('file') && model.get('file') !== '' ) {
+ var fileUploadSuccess = function(r) {
+ FMS.uploading = false;
+ $.mobile.loading('hide');
+ if ( r.response ) {
+ var data;
+ try {
+ data = JSON.parse( decodeURIComponent(r.response) );
+ }
+ catch(err) {
+ data = {};
+ }
+ if ( data.success ) {
+ that.success = 1;
+ that.trigger('sync', that, data, options);
+ } else if ( data.errors ) {
+ that.trigger('invalid', that, data, options);
+ } else {
+ that.trigger('error', that, FMS.strings.report_send_error, options);
+ }
+ } else {
+ that.trigger('error', that, FMS.strings.report_send_error, options);
+ }
+ };
+
+ var fileUploadFail = function(err) {
+ FMS.uploading = false;
+ $.mobile.loading('hide');
+ if ( err.code == FileTransferError.ABORT_ERR ) {
+ options.aborted = true;
+ that.trigger('error', that, FMS.strings.report_send_error, options);
+ } else {
+ that.trigger('error', that, FMS.strings.report_send_error, options);
+ }
+ };
+
+ fileURI = model.get('file');
+
+ var fileOptions = new FileUploadOptions();
+ fileOptions.fileKey="photo";
+ fileOptions.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);
+ fileOptions.mimeType="image/jpeg";
+ fileOptions.params = params;
+ fileOptions.chunkedMode = false;
+
+ var ft = new FileTransfer();
+
+ FMS.uploading = false;
+ var setupChecker = function() {
+ var uploadPcnt = 0;
+ var lastUploadPcnt = 0;
+ var uploadComputable = false;
+ var startTime = Date.now();
+ var checkUpload = function() {
+ if ( !FMS.uploading || ( uploadComputable && uploadPcnt == 80 ) ) {
+ return;
+ }
+
+ var uploadTime = Date.now() - startTime;
+ if ( ( lastUploadPcnt == 0 && uploadPcnt == 0 ) ||
+ ( lastUploadPcnt > 0 && uploadPcnt == lastUploadPcnt ) ||
+ uploadTime > FMS.uploadTimeout
+ ) {
+ ft.abort();
+ } else {
+ window.setTimeout( checkUpload, 15000 );
+ }
+ lastUploadPcnt = uploadPcnt;
+ };
+ ft.onprogress = function(evt) {
+ if (evt.lengthComputable) {
+ uploadComputable = true;
+ uploadPcnt = (evt.loaded/evt.total) * 80;
+ pcnt = uploadPcnt + '%';
+ $('.ui-loader #progress').css('display', 'block');
+ $('.ui-loader #progress').css('width', pcnt);
+ if ( pcnt == '80%' ) {
+ $('.ui-loader #progress').css('background-color', 'green' );
+ }
+ } else {
+ uploadPcnt++;
+ }
+ };
+ $.mobile.loading('show', {
+ text: FMS.strings.photo_loading,
+ textVisible: true,
+ html: '<span class="ui-icon ui-icon-loading"></span><h1>' + FMS.strings.photo_loading + '</h1><span id="progress"></span>'
+ });
+ window.setTimeout( checkUpload, 15000 );
+ FMS.uploading = true;
+ ft.upload(fileURI, CONFIG.FMS_URL + "report/new/mobile", fileUploadSuccess, fileUploadFail, fileOptions);
+ };
+ setupChecker();
+ } else {
+ $.ajax( {
+ url: CONFIG.FMS_URL + "report/new/mobile",
+ type: 'POST',
+ data: params,
+ dataType: 'json',
+ timeout: 30000,
+ success: function(data) {
+ if ( data.success ) {
+ that.success = 1;
+ that.trigger('sync', that, data, options);
+ } else {
+ that.trigger('invalid', that, data, options);
+ }
+ },
+ error: function (data, status, errorThrown ) {
+ that.trigger('error', that, data, status, errorThrown );
+ }
+ } );
+ }
+ },
+
+ getLastUpdate: function(time) {
+ if ( time ) {
+ props.time = time;
+ }
+
+ if ( !props.time ) {
+ return '';
+ }
+
+ var t;
+ if ( typeof props.time === 'String' ) {
+ t = new Date( parseInt(props.time, 10) );
+ } else {
+ t = props.time;
+ }
+ }
+ })
+ });
+})(FMS, Backbone, _, $);
diff --git a/www/js/models/user.js b/www/js/models/user.js
new file mode 100644
index 0000000..0cec52e
--- /dev/null
+++ b/www/js/models/user.js
@@ -0,0 +1,17 @@
+(function(FMS, Backbone, _, $) {
+ _.extend( FMS, {
+ User: Backbone.Model.extend({
+ localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-users')
+ })
+ });
+})(FMS, Backbone, _, $);
+
+
+(function(FMS, Backbone, _, $) {
+ _.extend( FMS, {
+ Users: Backbone.Collection.extend({
+ model: FMS.User,
+ localStorage: new Backbone.LocalStorage(CONFIG.NAMESPACE + '-users')
+ })
+ });
+})(FMS, Backbone, _, $);