-
Notifications
You must be signed in to change notification settings - Fork 2
/
TinFormFunctions.js
executable file
·433 lines (373 loc) · 12.1 KB
/
TinFormFunctions.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
=============COPYRIGHT============
Tin Statement Sender - An I-Did-This prototype for Tin Can API 0.95
Copyright (C) 2012 Andrew Downes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<http://www.gnu.org/licenses/>.
*/
function getLRSFromQueryString()
{
var lrs = $.getUrlVar('lrs');
if (!(lrs == undefined))
{
lrs = JSON.parse(urldecode(lrs));
for (var i=1;i<lrs.length;i++)
{
appendLRS();
}
$('#lrs').find('.lrs').each(function(index){
$(this).find('.endpoint').val(lrs[index].endpoint);
$(this).find('.basicLogin').val(lrs[index].login);
$(this).find('.basicPass').val(lrs[index].pass);
});
}
}
function getObjectFromQueryString(objectToGet)
{
var qString = $.getUrlVar(objectToGet);
if (!(qString == undefined))
{
return JSON.parse(urldecode(qString));
}
else
{
return;
}
}
function ObjectTypeChanged (event)
{
var elementId = event.data.elementId;
//Hide all subsections (if they exist)
$('#' + elementId).find('.activitySubSection').addClass('displayNone');
$('#' + elementId).find('.agentSubSection').addClass('displayNone');
//hide Agent add/remove buttons (used for groups)
$('#' + elementId).find('.agentAdd').addClass('displayNone');
$('#' + elementId).find('.agentRemove').addClass('displayNone');
switch($(this).val())
{
case 'Agent':
//display agentsubsection (if found)
$('#' + elementId).find('.agentSubSection').removeClass('displayNone');
//hide group
$('#' + elementId).find('.group').addClass('displayNone');
//hide extra agents
$('#' + elementId).find('.agent').slice(1).addClass('displayNone')
//If all agents have been removed (in group mode) then add one.
if ($('#' + elementId).find('.agent').length < 1)
{
appendAgent(elementId);
}
break;
case 'Group':
//display agentsubsection (if found)
$('#' + elementId).find('.agentSubSection').removeClass('displayNone');
//reveal group
$('#' + elementId).find('.group').removeClass('displayNone');
//reveal all agents
$('#' + elementId).find('.agent').removeClass('displayNone');
//reveal add/remove buttons
$('#' + elementId).find('.agentAdd').removeClass('displayNone');
if ($('#' + elementId).find('.agent').length > 1)
{
$('#' + elementId).find('.agentRemove').removeClass('displayNone');
}
break;
case 'Activity':
//display activitysubsection (if found)
$('#' + elementId).find('.activitySubSection').removeClass('displayNone');
break;
}
}
function appendLanguageMapOnEvent(event)
{
appendLanguageMap(event.data.elementId, event.data.propertyClass, event.data.loop, event.data.languageMap)
}
function appendLanguageMap(elementId,propertyClass,loop,languageMap)
{
var capitalizedPropertyClass = capitaliseFirstLetter(propertyClass);
loop = typeof loop !== 'undefined' ? loop : 1;
for (var i=0;i<loop;i++)
{
var propertyCount = $('#' + elementId).parent().find('.' + propertyClass).length;
var language = typeof languageMap[propertyCount] !== 'undefined' ? languageMap[propertyCount] : "";
$('#' + elementId + capitalizedPropertyClass + 'ButtonHolder').before('\
<tr class="' + propertyClass + '">\
<td class="label">\
<input type="text" name="' + elementId + capitalizedPropertyClass + 'Key' + propertyCount + '" id="' + elementId + capitalizedPropertyClass + 'Key' + propertyCount + '" value="' + language + '" class="'+ propertyClass + 'Key" />\
</td>\
<td>\
<input type="text" name="' + elementId + capitalizedPropertyClass + 'Value' + propertyCount + '" id="' + elementId + capitalizedPropertyClass + 'Value' + propertyCount + '" class="' + propertyClass + 'Value" />\
</td>\
</tr>\
');
//Show the '-' button
$('#' + elementId + capitalizedPropertyClass + 'Remove').removeClass('displayNone');
}
}
function appendAgentOnEvent(event)
{
appendAgent(event.data.elementId)
}
function appendAgent(elementId)
{
var agentsOnlyCount = $('#' + elementId).find('.agent').length;
var propertyCount = agentsOnlyCount + $('#' + elementId).find('.group').length;
var newAgent = $('\
<div class="agent">\
<h3>Agent ' + (agentsOnlyCount + 1) + '</h3>\
<table>\
<tr>\
<td class="label">Name:</td>\
<td>\
<input type="text" name="' + elementId + 'Name' + propertyCount + '" id="' + elementId + 'Name' + propertyCount + '" class="name"/>\
</td>\
</tr>\
<tr>\
<td class="label">\
<select name="' + elementId + 'FunctionalIdentifierType' + propertyCount + '" id="' + elementId + 'FunctionalIdentifierType' + propertyCount + '" class="functionalIdentifierType">\
<option value="mbox" selected="selected">mbox</option>\
<option value="mbox_sha1sum">mbox_sha1sum</option>\
<option value="openid">openid</option>\
<option value="account">account</option>\
</select>\
</td>\
<td>\
<input type="text" name="' + elementId + 'FunctionalIdentifier' + propertyCount + '" id="' + elementId + 'FunctionalIdentifier' + propertyCount + '" class="functionalIdentifier"/>\
</td>\
</tr>\
<tr class="agentAccount displayNone">\
<td class="label">homePage:</td>\
<td>\
<input type="text" name="' + elementId + 'AccountHomePage' + propertyCount + '" id="' + elementId + 'AccountHomePage' + propertyCount + '" class="accountHomePage"/>\
</td>\
</tr>\
<tr class="agentAccount displayNone">\
<td class="label">name:</td>\
<td>\
<input type="text" name="' + elementId + 'AccountName' + propertyCount + '" id="' + elementId + 'AccountName' + propertyCount + '" class="accountName"/>\
</td>\
</tr>\
</table>\
</div>\
').appendTo('#' + elementId);
$('#' + elementId + 'FunctionalIdentifierType' + propertyCount).change(function(){
if ($(this).val() == 'account')
{
$(this).closest('table').find('.agentAccount').removeClass('displayNone');
$('#' + elementId + 'FunctionalIdentifier' + propertyCount).addClass('displayNone');
}
else
{
$(this).closest('table').find('.agentAccount').addClass('displayNone');
$('#' + elementId + 'FunctionalIdentifier' + propertyCount).removeClass('displayNone');
}
});
//If this is a group, ensure the removeAgent button is visible if there are more than 1 agent
if (($('#'+elementId).parent().find('.objectType').val() == 'Group') && (agentsOnlyCount > 0))
{
$('#' + elementId + 'Remove').removeClass('displayNone');
}
return newAgent;
}
function appendGroup(elementId)
{
//Create an Agent
var Group = appendAgent(elementId);
var GroupIndex = Group.index($('#'+elementId).find('.agent'));
//change its class
Group.removeClass('agent').addClass('group');
//change its heading
Group.find('h3').text('Group Details');
return Group;
}
function appendLRS()
{
var lrsCount = $('.lrs').length;
var newLrs = $('\
<div class="lrs">\
<h3>LRS '+ (lrsCount + 1) + '</h3>\
<table>\
<tr>\
<td class="label">Endpoint:</td>\
<td>\
<input type="text" name="endpoint'+ lrsCount + '" id="endpoint'+ lrsCount + '" class="required endpoint"/>\
</td>\
</tr>\
<tr>\
<td class="label">Basic Login:</td>\
<td>\
<input type="text" name="basicLogin'+ lrsCount + '" id="basicLogin'+ lrsCount + '" class="required basicLogin" />\
</td>\
</tr>\
<tr>\
<td class="label">Basic Password:</td>\
<td>\
<input type="text" name="basicPass'+ lrsCount + '" id="basicPass'+ lrsCount + '" class="required basicPass"/>\
</td>\
</tr>\
</table>\
</div>\
').appendTo('#lrs');
//Show the '-LRS' button if there is now more than 1 LRS
if (lrsCount > 0)
{
$('#lrsLrsRemove').removeClass('displayNone');
}
return newLrs;
}
function removeProperty(event)
{
//get paremters
var elementId = event.data.elementId;
var propertyClass = event.data.propertyClass;
//get and count elements
var propertyArray = $('#' + elementId).find('.' + propertyClass),
propertyCount = propertyArray.length;
//Hide the '-' button if this function will reduce us to the minimum
if (propertyCount < (event.data.minimum + 2))
{
$('#' + elementId).parent().find('.' + propertyClass + 'Remove').addClass('displayNone');
}
//start at the top and loop through LRSes
propertyArray.each(function(index){
//if lrs fields are empty or we have reached last lrs
if (index == (propertyCount-1))
{
//TODO: add Are you sure?
$(this).remove();
}
else if (areAllInputsEmpty($(this)))
{
//shuffle all values up one item
bubbleInputValuesUp(propertyArray.slice(index));
//remove the last item
$(propertyArray[propertyCount-1]).remove();
}
});
}
//Checks if all inputs within a Jquery Object are empty
function areAllInputsEmpty(jqueryObjectToTest)
{
var allEmpty = true;
jqueryObjectToTest.find("input[type=text]").each(function(index){
if (!($(this).val() == ""))
{
allEmpty = false;
}
});
//TODO: check other input types and selects
return allEmpty;
}
function bubbleInputValuesUp(objectArray)
{
//first pass - Get all values
var valuesArray = new Array();
objectArray.each(function(objectArrayIndex){
//if not first item
if (!(objectArrayIndex==0))
{
valuesArray[objectArrayIndex] = new Array();
$(this).find("input[type=text]").each(function(inputIndex){
valuesArray[objectArrayIndex][inputIndex] = $(this).val();
});
}
});
//second pass - Set values
var objectArrayLength = objectArray.length;
objectArray.each(function(objectArrayIndex){
//if not last item
if (!(objectArrayIndex==(objectArrayLength-1)))
{
valuesArray[objectArrayIndex] = new Array();
$(this).find("input[type=text]").each(function(inputIndex){
$(this).val(valuesArray[objectArrayIndex + 1][inputIndex]);
});
}
});
}
function getActor(jQueryAgent,objectType)
{
//default to agent
objectType = typeof objectType !== 'undefined' ? objectType : 'Agent';
var rtnActor;
var rtnActorFunctionalIdentifierType = jQueryAgent.find('.functionalIdentifierType').val();
if (rtnActorFunctionalIdentifierType == 'account')
{
rtnActor= new TinCan[objectType]({
name : jQueryAgent.find('.name').val(),
account: {
name:jQueryAgent.find('.accountHomePage').val(),
homePage:jQueryAgent.find('.accountName').val()
}
});
}
else if (rtnActorFunctionalIdentifierType == 'mbox')
{
rtnActor= new TinCan[objectType]({
name : jQueryAgent.find('.name').val(),
mbox : jQueryAgent.find('.functionalIdentifier').val()
});
}
else
{
rtnActor= new TinCan[objectType]({
name : jQueryAgent.find('.name').val()
});
rtnActor[rtnActorFunctionalIdentifierType] = jQueryAgent.find('.functionalIdentifier').val();
}
rtnActor.objectType = objectType;
return rtnActor;
}
//=====================UTILITY FUNCTIONS==============
function getLangFromMapInGBOrDefault (languageMap){
var defaultLang;
for (var lang in languageMap) {
defaultLang = lang;
break;
}
return (languageMap['en-GB']) ? languageMap['en-GB'] : languageMap[lang];
}
//removes any empty properties
function deleteEmptyProperties(objectToTest)
{
if (typeof objectToTest=="object"){
for (i in objectToTest) {
if (objectToTest[i] == null || objectToTest[i] == "" || (JSON.stringify(objectToTest[i])=="{}")) {
delete objectToTest[i];
}
else {
deleteEmptyProperties(objectToTest[i]);
}
}
}
return objectToTest;
}
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function urldecode(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});