forked from developerforce/Force.com-JavaScript-REST-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
243 lines (202 loc) · 8 KB
/
app.js
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
/*
* Copyright (c) 2011, salesforce.com, inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This is a simple jQuery-based app that uses the Force.com REST API.
* See example.page for code required to run this in Visualforce
* See example.html for code required to run this on your own server
*/
function logout(e) {
e.preventDefault();
window.location.href = 'https://login.salesforce.com/secur/logout.jsp';
}
function errorCallback(jqXHR){
$dialog.dialog('option', 'title', 'Error');
$dialog.dialog('option', 'modal', true);
$dialog.html(TrimPath.processDOMTemplate("error_jst", jqXHR));
$dialog.find('#ok').click(function(e) {
e.preventDefault();
$dialog.dialog('close');
logout(e);
});
$dialog.dialog('open');
}
function metadataCallback(response){
// Using TrimPath Template for now - may switch to jQuery Template at some
// point
$('#prompt').html(TrimPath.processDOMTemplate("prompt_jst"
, response));
// Set up autocomplete
$( "#value" ).autocomplete({
source: function( request, response ) {
var query = "SELECT Id, Name FROM Account "+
"WHERE "+$("#field").val()+" LIKE '%"+request.term+"%' "+
"ORDER BY Name LIMIT 20";
client.query(query, function( data ) {
response( $.map( data.records, function( record ) {
return {
label: record.Name,
value: record.Id
}
}));
}, errorCallback);
},
minLength: 2,
delay: 1000,
select: function( event, ui ) {
if ( ui.item != null ) {
showAccountDetail(ui.item.value);
} else {
filterAccounts($("#field").val(),this.value);
}
return false;
}
});
$('#go').click(function(e) {
var field = $("#field").val();
var value = $("#value").val();
e.preventDefault();
filterAccounts(field,value);
});
$('#new').click(function(e) {
e.preventDefault();
// Just make Trimpath happy
var dummy = {};
var i;
for (i = 0; i < response.fields.length; i++) {
dummy[response.fields[i].name] = '';
}
$dialog.html(TrimPath.processDOMTemplate("edit_jst", dummy));
$dialog.find('#action').text('Create').click(function(e) {
e.preventDefault();
$dialog.dialog('close');
var fields = {};
$dialog.find('input').each(function() {
var child = $(this);
if ( child.val().length > 0 ) {
fields[child.attr("name")] = child.val();
}
});
$('#list').html(ajaxgif+" creating account...");
client.create('Account', fields, createCallback, errorCallback);
});
$dialog.dialog('option', 'title', 'New Account');
$dialog.dialog('open');
});
filterAccounts();
}
function queryCallback(response) {
$('#list').html(TrimPath.processDOMTemplate("accounts_jst"
, response));
$('#version').text($.fn.jquery);
$('#uiversion').text($.ui.version);
$("#list tr:nth-child(odd)").addClass("odd");
$('#logout').click(logout);
$('#accounts').find('.id')
.hover(function() {
$(this).addClass("highlighted");
},function(){
$(this).removeClass("highlighted");
})
.click(function(){
showAccountDetail(this.id);
});
}
// Make our own startsWith utility fn
String.prototype.startsWith = function(str){
return (this.substr(0, str.length) === str);
}
function detailCallback(response) {
if (response.Website != null
&& !response.Website.startsWith('http://')) {
response.Website = 'http://'+response.Website;
}
$dialog.html(TrimPath.processDOMTemplate("detail_jst"
,response));
$dialog.find('#industry').click(function(e) {
e.preventDefault();
$dialog.dialog('close');
filterIndustry($(this).text());
});
$dialog.find('#delete').click(function(e) {
e.preventDefault();
$dialog.dialog('close');
$('#list').html(ajaxgif+" deleting account...");
client.del('Account', $dialog.find('#id').val(), deleteCallback, errorCallback);
});
$dialog.find('#edit').click(function(e) {
e.preventDefault();
$dialog.html(TrimPath.processDOMTemplate("edit_jst"
,response));
$dialog.find('#action').text('Update').click(function(e) {
e.preventDefault();
$dialog.dialog('close');
var fields = {};
$dialog.find('input').each(function() {
var child = $(this);
if ( child.val().length > 0 && child.attr("name") != 'id') {
fields[child.attr("name")] = child.val();
}
});
$('#list').html(ajaxgif+" updating account...");
client.update('Account', $dialog.find('#id').val(), fields, updateCallback, errorCallback);
});
});
}
function createCallback(response) {
$('#list').text('Created '+response.id);
setTimeout("filterAccounts()",1000);
}
function updateCallback(response) {
$('#list').text('Updated');
setTimeout("filterAccounts()",1000);
}
function deleteCallback(response) {
$('#list').text('Deleted');
setTimeout("filterAccounts()",1000);
}
function showAccountDetail(id) {
// Show the dialog
$dialog.dialog('option', 'title', 'Account Detail');
$dialog.dialog('open');
$dialog.html(ajaxgif+" retrieving...");
// Get account details and populate the dialog
client.retrieve('Account', id, 'Name,Industry,TickerSymbol,Website'
, detailCallback, errorCallback);
}
function filterIndustry(industry) {
$('#list').html(ajaxgif+" loading data...");
var query = "SELECT Id, Name FROM Account WHERE Industry = '"+industry
+"' ORDER BY Name LIMIT 20";
client.query(query, queryCallback, errorCallback);
}
function filterAccounts(field, value) {
$('#list').html(ajaxgif+" loading data...");
var query = ( typeof value !== 'undefined' && value.length > 0 )
? "SELECT Id, Name FROM Account WHERE "+field+" LIKE '%"+value
+"%' ORDER BY Name LIMIT 20"
: "SELECT Id, Name FROM Account ORDER BY Name LIMIT 20";
client.query(query, queryCallback, errorCallback);
}