forked from forcedotcom/mobile-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountEditor.html
More file actions
377 lines (339 loc) · 17.1 KB
/
Copy pathAccountEditor.html
File metadata and controls
377 lines (339 loc) · 17.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<!-- Importing Google Polymer framework -->
<script src="dependencies/platform/platform.js"></script>
<!-- Container --/>
<script src="cordova-2.3.0.js"></script>
<script src="dependencies/mobilesdk-shared/cordova.force.js"></script>
<!-- End Container -->
<!-- Local Testing -->
<script src="dependencies/mobilesdk-shared/MockCordova.js"></script>
<script src="dependencies/mobilesdk-shared/cordova.force.js"></script>
<script src="dependencies/mobilesdk-shared/MockSmartStore.js"></script>
<!-- End Local Testing -->
<!-- Place your HTML imports here -->
<link rel="import" href="elements/mobile-ui-elements.html">
<link rel="import" href="dependencies/polymer-ui-elements/polymer-ui-toggle-button/polymer-ui-toggle-button.html">
</head>
<body>
<polymer-element name="search-screen" attributes="sobject">
<template>
<header class="bar-title">
<h1 class="title">{{sobject}}s</h1>
<a class="button" href="#add">Create</a>
</header>
<force-ui-search id="search" sobject="{{sobject}}" searchfield="Name"></force-ui-search>
<div class="content" style="margin-top:44px /* XXX */">
<force-selector-list id="list" sobject="{{sobject}}" querytype="{{$.search.querytype}}" query="{{$.search.query}}"></force-selector-list>
<ul id="listbody" class="list">
<template repeat="{{$.list.collection.models}}">
<li name="{{id}}">
<a href="#edit/{{id}}">
{{attributes.Name}}
<template if="{{attributes.__sync_failed__}}"><span class="count-negative">Failed to sync</span></template>
<template if="{{attributes.__local__}}">
<span class="count-main">Locally
<template if="{{attributes.__locally_created__}}"> created</template>
<template if="{{attributes.__locally_updated__}}"> updated</template>
<template if="{{attributes.__locally_deleted__}}"> deleted</template>
</span>
</template>
<template if="{{!attributes.__local__}}"><span class="count-positive">Cached</span></template>
</a>
</li>
</template>
</ul>
</div>
</template>
<script type="text/javascript">
Polymer('search-screen', {
refresh: function() {
this.$.list.fetch();
}
});
</script>
</polymer-element>
<polymer-element name="sync-screen" attributes="sobject">
<template>
<header class="bar-title">
<a class="button-prev" on-click="{{goBack}}">Back</a>
<h1 class="title">Sync</h1>
</header>
<div class="bar-standard bar-header-secondary">
<a class="button button-block" on-click="{{doSync}}">Process {{$.list.collection.length}} records</a>
</div>
<force-selector-list id="list" sobject="{{sobject}}" querytype="cache" query="{{query}}"></force-selector-list>
<div style="margin-top:88px /*XXX*/">
<ul id="listbody" class="list">
<template repeat="{{$.list.collection.models}}">
<li name="{{id}}">
<a href="#editFromCache/{{id}}">
{{attributes.Name}}
<template if="{{attributes.__sync_failed__}}"><span class="count-negative">Failed to sync</span></template>
</a>
</li>
</template>
</ul>
</div>
</template>
<script type="text/javascript">
Polymer('sync-screen', {
query: {queryType:"exact", indexPath:"__local__", matchKey:true, order:"ascending", pageSize:25},
doSync: function() {
var that = this;
var collection = this.$.list.collection;
if (collection.length == 0 || collection.at(0).get("__sync_failed__")) {
// we push sync failures back to the end of the list - if we encounter one, it means we are done
return;
}
else {
var record = collection.shift();
var options = {
mergeMode: Force.MERGE_MODE.MERGE_FAIL_IF_CHANGED,
success: function() {
if (collection.length == 0) {
app.router.navigateFirst();
}
else {
that.doSync();
}
},
error: function() {
record = record.set("__sync_failed__", true);
collection.push(record);
that.doSync();
},
fieldlist: _.intersection(_.keys(record.attributes), app.editablefieldlist),
cache: collection.cache,
cacheForOriginals: collection.cacheForOriginals
};
return record.get("__locally_deleted__") ? record.destroy(options) : record.save(null, options);
}
},
goBack: function() {
if (this.$.list.collection.length > 0) {
// We are not done - going back offline before leaving screen
app.onlinestatus = false;
// NB: This will cause us to navigate out of the sync screen
}
else {
app.router.navigateFirst();
}
},
refresh: function() {
this.$.list.fetch();
}
});
</script>
</polymer-element>
<polymer-element name="detail-screen" attributes="sobject recordid">
<template>
<header class="bar-title">
<a class="button-prev" on-click="{{goBack}}">Back</a>
<h1 class="title">{{recordid ? 'Edit' : 'Add'}} {{sobject}}</h1>
<a class="button" style="display:{{recordid ? 'display' : 'block'}}" id="toggleDeleteButton" on-click="{{doToggleDelete}}">{{model.attributes.__locally_deleted__ ? 'Undelete' : 'Delete'}}</a>
</header>
<div class="bar-standard bar-header-secondary">
<a class="button" id="saveButton" on-click="{{doSave}}">Save</a>
<template if="{{forcingCacheFetch}}">
<a class="button" id="mergeButton" on-click="{{doSaveMerge}}">Merge</a>
<a class="button" id="overwriteButton" on-click="{{doSaveOverwrite}}">Overwrite</a>
</template>
<span style="position:absolute; right:10px">
<template if="{{model.attributes.__local__}}">
<span class="count-main">Locally
<template if="{{model.attributes.__locally_created__}}"> created</template>
<template if="{{model.attributes.__locally_updated__}}"> updated</template>
<template if="{{model.attributes.__locally_deleted__}}"> deleted</template>
</span>
</template>
</span>
</div>
<div class="content">
<force-ui-detail flex id="detail" sobject="{{sobject}}" recordid="{{recordid}}" foredit="true"></force-ui-detail>
</div>
</template>
<script type="text/javascript">
Polymer('detail-screen', {
get model() {
return (this.$ ? this.$.detail.model : null);
},
forceCacheFetch: function(b) {
this.forcingCacheFetch = b;
this.$.detail.fetchCacheMode = b ? Force.CACHE_MODE.CACHE_ONLY : null;
},
refresh: function() {
this.$.detail.$.force_sobject.reset();
},
goBack: function() {
app.router.navigateBack();
},
getSaveOptions: function(mergeMode, cacheMode) {
var that = this;
return {
cacheMode: cacheMode,
mergeMode: mergeMode,
success: this.goBack,
error: function(data, err, options) {
var error = new Force.Error(err);
if (error.type == "ConflictError") {
$(that.$.mergeButton).show();
$(that.$.overwriteButton).show();
}
},
fieldlist: this.model.get("__local__")
? _.intersection(_.keys(this.model.attributes), app.editablefieldlist)
: _.keys(this.model.changed)
};
},
handleError: function(error) {
if (error.type == "ConflictError") {
$(this.$.mergeButton).show();
$(this.$.overwriteButton).show();
}
},
doSave: function() {
this.$.detail.save(this.getSaveOptions(Force.MERGE_MODE.MERGE_FAIL_IF_CHANGED));
},
doSaveMerge: function() {
this.$.detail.save(this.getSaveOptions(Force.MERGE_MODE.MERGE_ACCEPT_YOURS));
},
doSaveOverwrite: function() {
this.$.detail.save(this.getSaveOptions(Force.MERGE_MODE.OVERWRITE));
},
doToggleDelete: function() {
var that = this;
var force_sobject = this.$.detail.$.force_sobject;
if (force_sobject.get("__locally_deleted__")) {
force_sobject.set("__locally_deleted__", false);
force_sobject.save(this.getSaveOptions(null, Force.CACHE_MODE.CACHE_ONLY));
}
else {
force_sobject.delete({
success: this.goBack,
error: function(data, err, options) {
var error = new Force.Error(err);
alert("Failed to delete " + that.sobject + ": " + (error.type === "RestError" ? error.details[0].message : "Remote change detected - delete aborted"));
}
});
}
}
})
</script>
</polymer-element>
<polymer-element name="mobile-app" attributes="sobject">
<template>
<force-ui-app id="force_ui_app" multipage="true" accesstoken="{{accesstoken}}" instanceurl="{{instanceurl}}" hideheader="true">
<search-screen id="search" class="page absolute" sobject="{{sobject}}"></search-screen>
<detail-screen id="detail" class="page absolute" sobject="{{sobject}}"></detail-screen>
<sync-screen id="sync" class="page absolute" sobject="{{sobject}}"></sync-screen>
</force-ui-app>
<polymer-ui-toggle-button id="onlineToggler" value="{{onlinestatus}}" onCaption="Online" offCaption="Offline" style="position: fixed; bottom: 0; right: 0; margin: 20px;"></polymer-ui-toggle-button>
</template>
<script type="text/javascript">
Polymer('mobile-app', {
observe: {
"sobject": "reset",
},
_onlineStatus: true,
get onlinestatus() {
return this._onlineStatus;
},
set onlinestatus(newStatus) {
if (newStatus && !this._onlineStatus) {
app.router.navigate("#sync", {trigger:true})
}
else if (!newStatus && document.location.hash == "#sync") {
app.router.navigate("#", {trigger:true});
}
SFDC.isOnline = function() { return newStatus }; // XXX
this._onlineStatus = newStatus;
},
reset: function() {
var that = this;
$.when(SFDC.launcher).then(function() {
SFDC.getSObjectType(that.sobject).describe().then(function(describeResult) {
app.editablefieldlist = _.pluck(_.filter(describeResult.fields, function(fieldInfo) {
return fieldInfo.updateable;
}), "name");
});
});
},
ready: function() {
var app = this;
this.router = app.$.force_ui_app.setupRouter({
routes: {
"": "list",
"list": "list",
"add": "add",
"edit/:id": "edit",
"editFromCache/:id" : "editFromCache",
"sync":"sync"
},
list: function() {
app.$.search.refresh();
app.router.gotoPageId('#search');
},
add: function() {
this.edit("");
},
editFromCache: function(recordid) {
this.showDetail(recordid, true);
},
edit: function(recordid) {
this.showDetail(recordid);
},
showDetail: function(recordid, fetchFromCache) {
app.$.detail.forceCacheFetch(fetchFromCache);
if (app.$.detail.recordid == recordid) {
app.$.detail.refresh();
}
app.$.detail.recordid = recordid;
app.router.gotoPageId('#detail');
},
sync: function() {
app.$.sync.refresh();
app.router.gotoPageId('#sync');
}
});
Backbone.history.start();
}
});
</script>
</polymer-element>
<mobile-app sobject="Account"></mobile-app>
<script type="text/javascript">
var app;
window.addEventListener('WebComponentsReady', function() {
app = $("mobile-app")[0];
// Container
if (cordova.callbackId) {
document.addEventListener("deviceready", function() {
cordova.require("salesforce/plugin/oauth").getAuthCredentials(
function(creds) {
SFDC.launch(creds, cordova.require("salesforce/plugin/oauth").forcetkRefresh);
},
function(error) {
console.log("Auth failed: " + error);
});
}, false);
}
// Local testing
else {
var creds = {
accessToken: "--will-be-obtained-by-refresh--",
refreshToken: "--get-a-valid-refresh-token-from-rest-api-explorer--",
clientId: "--paste-a-valid-client-id--",
loginUrl: "https://test.salesforce.com",
instanceUrl: "https://cs1.salesforce.com",
};
SFDC.launch(creds);
}
});
</script>
</body>
</html>