1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
var tpl = {
// Hash of preloaded templates for the app
templates:{},
// Recursively pre-load all the templates for the app.
// This implementation should be changed in a production environment. All the template files should be
// concatenated in a single file.
loadTemplates:function (names, callback) {
var that = this;
var loadTemplate = function (index) {
var name = names[index];
console.log('Loading template: ' + name + ', index: ' + index);
$.get('templates/en/' + name + '.html', function (data) {
that.templates[name] = data;
index++;
if (index < names.length) {
loadTemplate(index);
} else {
callback();
}
});
};
loadTemplate(0);
},
// Get template by name from hash of preloaded templates
get:function (name) {
return this.templates[name];
}
};
(function (FMS, Backbone, _, $) {
_.extend(FMS, {
templates: [
'home', 'around', 'offline', 'reports', 'address_search', 'existing', 'photo', 'details', 'submit', 'submit_email', 'submit_name', 'submit_password', 'sent'
],
isOffline: 0,
initialized: 0,
users: new FMS.Users(),
currentUser: null,
currentLocation: null,
currentPosition: null,
currentDraft: new FMS.Draft(),
allDrafts: new FMS.Drafts(),
reportToView: null,
online: function() {
FMS.isOffline = 0;
},
offline: function() {
FMS.isOffline = 1;
},
saveCurrentDraft: function() {
FMS.router.pause();
FMS.allDrafts.add( FMS.currentDraft );
FMS.currentDraft.save();
localStorage.currentDraftID = FMS.currentDraft.id;
},
loadCurrentDraft: function() {
if ( localStorage.currentDraftID && localStorage.currentDraftID != 'null' ) {
var r = FMS.allDrafts.get( localStorage.currentDraftID );
if ( r ) {
FMS.currentDraft = r;
}
}
localStorage.currentDraftID = null;
if ( navigator && navigator.connection && ( navigator.connection.type == Connection.NONE ||
navigator.connection.type == Connection.UNKNOWN ) ) {
FMS.offline();
}
},
removeDraft: function(draftID, removePhoto) {
console.log( draftID );
var draft = FMS.allDrafts.get(draftID);
console.log( draft );
var uri = draft.get('file');
FMS.allDrafts.remove(draft);
draft.destroy();
if ( removePhoto && uri ) {
return FMS.files.deleteURI( uri );
}
var p = $.Deferred();
p.resolve();
return p;
},
initialize: function () {
if ( this.initialized == 1 ) {
return this;
}
FMS.initialized = 1;
tpl.loadTemplates( FMS.templates, function() {
_.extend(FMS, {
router: new FMS.appRouter(),
locator: new FMS.Locate()
});
_.extend( FMS.locator, Backbone.Events );
// we only ever have the details of one user
FMS.users.fetch();
if ( FMS.users.length > 0 ) {
FMS.currentUser = FMS.users.get(1);
}
if ( FMS.currentUser === null ) {
FMS.currentUser = new FMS.User({id: 1});
}
document.addEventListener('pause', function() { FMS.saveCurrentDraft(); }, false);
document.addEventListener('resume', function() { FMS.loadCurrentDraft(); }, false);
document.addEventListener('backbutton', function() { FMS.router.back(); }, true);
document.addEventListener('offline', function() { FMS.offline(); }, true);
document.addEventListener('online', function() { FMS.online(); }, true);
FMS.allDrafts.fetch();
FMS.loadCurrentDraft();
Backbone.history.start();
navigator.splashscreen.hide();
});
}
});
})(FMS, Backbone, _, $);
var androidStartUp = function() {
// deviceready does not fire on some android versions very reliably so
// we do this instead
if (FMS.initialized === 1) {
return;
}
if ( typeof device != 'undefined' ) {
if ( device.platform == 'Android' ) {
FMS.initialize();
}
} else {
window.setTimeout( androidStartUp, 1000 );
}
};
document.addEventListener('deviceready', FMS.initialize, false);
window.setTimeout( androidStartUp, 2000 );
|