-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoRest.html
418 lines (362 loc) · 17 KB
/
mongoRest.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script src="./js/jquery.loadTemplate-1.3.2.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css"
rel="stylesheet" type="text/css" />
<link href=".//main.css" rel="stylesheet" type="text/css" />
<script>
var modelData = new Array();
var dtoToDelete=new Array();
var dtoToInsert=new Array();
var dtoToReplace = new Array();
var dtoToAction = new Array();
function periodicRefresh() {
console.log("5 seconds");
getRestDTO();
setTimeout(periodicRefresh, 5000);
}
setTimeout(periodicRefresh, 5000);
function compareRefreshData(newData){
dtoToAction = new Array();
var modelCursor = 0;
var newDataCursor=0;
console.log(modelData.length)
console.log(newData.length)
var infinteLoopCounter=0;
for(var key in modelData){
var modelDataDtoInspection = modelData[key];
var finded = false;
for(var key2 in newData){
var newDataDtoInspection = newData[key2];
if(modelDataDtoInspection._id.$oid==newDataDtoInspection._id.$oid){
finded=true;
}
}
if(!finded){
dtoToDelete.push({dto:modelDataDtoInspection,position:key});
dtoToAction.push({dto:modelDataDtoInspection,position:key,action:"delete"});
}
}
for(var key in newData){
var newDataDtoInspection = newData[key];
var finded = false;
var modelDtoPosition=null;
for(var key2 in modelData){
var modelDataDtoInspection = modelData[key2];
if(modelDataDtoInspection._id.$oid==newDataDtoInspection._id.$oid){
finded=true;
modelDtoPosition = key;
dtoToReplace.push({dto : newDataDtoInspection,position:key});
dtoToAction.push({dto:newDataDtoInspection,position:key,action:"update"});
}
}
if(!finded){
dtoToInsert.push({dto:newDataDtoInspection,position:key});
dtoToAction.push({dto:newDataDtoInspection,position:key,action:"insert"});
}
}
console.log(dtoToDelete);
console.log(dtoToInsert);
console.log(dtoToReplace);
};
function builDOMDTO(dtoToRender){
var tmpContainer = $("<div></div>");
tmpContainer.loadTemplate($("#templateDTOContainer"), dtoToRender);
var templatedElements = tmpContainer.find(".itemRow");
//console.log(templatedElements);
templatedElements.on("click",function(){
if(!$(this).hasClass("editing")){
var text1 = $(this).find(".typeItemContent").val();
//console.log($(this).find(".typeItemContent"));
//console.log("find "+text1);
$(this).find(".typeItemcontrol option").filter(function() {
//may want to use $.trim in here
return $(this).text() == text1;
}).prop('selected', true);
$(this).find(".itemContentRead").hide();
$(this).find(".itemContentEdit").show();
$(this).addClass("editing");
}
});
templatedElements.find(".deleteDTOButton").on("click",function(event) {
var idDto = $(this).parents(".itemRow").find(".idDTO").val();
deleteRestDTO(idDto);
event.stopPropagation();
});
templatedElements.find(".cancelDTOButton").on("click",function(event) {
$(this).parents(".itemRow").find(".itemContentRead").show();
$(this).parents(".itemRow").find(".itemContentEdit").hide();
$(this).parents(".itemRow").removeClass("editing");
event.stopPropagation();
});
templatedElements.find(".updateDTOButton").on("click",function(event) {
console.log($(this).parents(".itemRow").find(".typeItemContent").val());
var type=$(this).parents(".itemRow").find(".typeItemcontrol").val();
console.log(type);
if (type=="none"){
type = null;
}
var niveau=$(this).parents(".itemRow").find(".niveauItemContent").val();
if (niveau==""){
niveau = null;
}
var dto = {
id : $(this).parents(".itemRow").find(".idDTO").val(),
name : $(this).parents(".itemRow").find(".nameItemContent").val(),
niveau : niveau,
type : type
}
console.log(dto);
putRestDTO(dto);
$(this).parents(".itemRow").find(".itemContentRead").show();
$(this).parents(".itemRow").find(".itemContentEdit").hide();
$(this).parents(".itemRow").removeClass("editing");
event.stopPropagation();
});
templatedElements.find("input[type=submit], a, button").button();
templatedElements.find(".itemContentEdit").hide();
return templatedElements;
}
/*modelData=[
{_id:4},
{_id:5},
{_id:6},
{_id:7},
{_id:8},
{_id:12},
{_id:13},
{_id:16},
{_id:17},
{_id:18}
];*/
var tableau2=[
{_id:1},
{_id:2},
{_id:3},
{_id:4},
{_id:5},
{_id:9},
{_id:10},
{_id:11},
{_id:14},
{_id:15}
];
function getRestDTO(){
$.ajax({
dataType: "json",
type: "get",
data: {
},
url: "https://api.mongohq.com/databases/referentiel_imie/collections/cursus/documents?_apikey=1CcVlrLo047v9eyOrvrx5BdhRO6iNldYqUAMkUCvc4&sort=%7B%22name%22:1%7D",
success: function(response){
//console.log("success");
//console.log(response);
compareRefreshData(response);
modelData=response;
var inspectionDtoToReplaceCursor = 0;
var inspectionDtoToInsertCursor = 0;
var inspectionDtoToDeleteCursor = 0;
var inspectionDtoToReplace=null;
var inspectionDtoToInsert=null;
var inspectionDtoToDelete=null;
var infinitloop=0;
var maxrow=35;
var offSetInserted=0;
var offSetDeleteImpossible=0;
for(var key in dtoToAction){
var dtoActionToTreat=dtoToAction[key];
console.log(dtoActionToTreat);
var searchElementIdex=(parseInt(dtoActionToTreat.position))*2;
if(dtoActionToTreat.action=="delete"){
var findedDom= $("#itemListContainer").children().eq(searchElementIdex);
findedDom.next().remove();
findedDom.remove();
}
if(dtoActionToTreat.action=="update"){
//var findedDom= $("#itemListContainer").children().eq((parseInt(dtoActionToTreat.position)+offSetInserted)*2);
var findedDom= $("#itemListContainer").children().eq(searchElementIdex);
if (!findedDom.hasClass("editing")){
var templatedElements = builDOMDTO(dtoActionToTreat.dto);
findedDom.next().remove();
findedDom.replaceWith(templatedElements);
}
}
if(dtoActionToTreat.action=="insert"){
var findedDom= $("#itemListContainer").children().eq(searchElementIdex);
var templatedElements = builDOMDTO(dtoActionToTreat.dto);
if (findedDom.length==0){
//console.log("append");
$("#itemListContainer").append(templatedElements);
}else{
console.log("insertBefore");
templatedElements.insertBefore(findedDom)
}
}
}
$("#load").dialog("close");
},
complete: function(response){
console.log("complete");
console.log(response)
},
error: function(response){
console.log("error");
console.log( this );
}
});
}
function deleteRestDTO(idDto){
console.log("delete id:")
console.log(idDto)
$.ajax({
dataType: "json",
type: "delete",
data: {},
url: "https://api.mongohq.com/databases/referentiel_imie/collections/cursus/documents/"+idDto+"?_apikey=1CcVlrLo047v9eyOrvrx5BdhRO6iNldYqUAMkUCvc4",
success: function(response){
console.log("success");
console.log(response);
getRestDTO();
$("#load").dialog("close");
},
complete: function(response){
console.log("complete");
console.log(response)
},
error: function(response){
console.log("error");
console.log( this );
}
});
}
function postRestDTO(){
$.ajax({
dataType: "json",
type: "post",
data: {
"document" : {
name:$("#inputAddDTO").val()
}
},
url: "https://api.mongohq.com/databases/referentiel_imie/collections/cursus/documents?_apikey=1CcVlrLo047v9eyOrvrx5BdhRO6iNldYqUAMkUCvc4",
success: function(response){
console.log("success");
console.log(response);
getRestDTO();
$("#load").dialog("close");
},
complete: function(response){
console.log("complete");
console.log(response)
},
error: function(response){
console.log("error");
console.log( this );
}
});
};
function putRestDTO(dto){
console.log(dto);
$.ajax({
dataType: "json",
type: "put",
data: {
"document" : {
"$set" :{
name: dto.name,
type: dto.type,
niveau: dto.niveau
}
}
},
url: "https://api.mongohq.com/databases/referentiel_imie/collections/cursus/documents/"+dto.id+"?_apikey=1CcVlrLo047v9eyOrvrx5BdhRO6iNldYqUAMkUCvc4",
success: function(response){
console.log("success");
console.log(response);
getRestDTO();
$("#load").dialog("close");
},
complete: function(response){
console.log("complete");
console.log(response)
},
error: function(response){
console.log("error");
console.log( this );
}
});
};
$(document)
.ready(function() {
$("#load").dialog();
$("input[type=submit], a, button").button();
$("#createDTOButton").on("click", function() {
postRestDTO();
});
});
</script>
</head>
<body style="background-image:url('http://i1215.photobucket.com/albums/cc520/ChelleCoca/gplaypattern.png');">
<div id="addDTOContainer" class="itemListContainer ">
<div id="itemAddDTOContainer" class="itemRowContainer">
<div id="itemAddDTOInput" class="itemCell ui-widget-content">
<div class="itemContent">
<input id="inputAddDTO" type="text"/>
</div>
</div>
<div id="itemAddDTOAction" class="itemCell ui-widget-content" style="width:100px;">
<div class="itemContent" style="width:100px;">
<button id="createDTOButton"><span class="ui-icon ui-icon-plusthick"></span></button>
</div>
</div>
</div>
</div>
<div id="itemListContainer" class="itemListContainer ui-widget" style="margin-top : 30px">
</div>
<div id="load" title="dialog">LOAD</div>
<script type="text/html" id="templateDTOContainer">
<div class="itemRowContainer itemRow itemRowEditable">
<div class="itemCell ui-widget-content">
<div style="width:250px">
<div class="itemContent itemContentRead" data-content="name"/>
<input type="text" class="itemContent itemContentEdit nameItemContent" data-value="name"/>
</div>
</div>
<div class="itemCell ui-widget-content">
<div style="width:60px">
<div class="itemContent itemContentRead"data-content="niveau">
</div>
<input type="text" style="width:40px" class="itemContent itemContentEdit niveauItemContent" data-value="niveau"/>
</div>
</div>
<div class="itemCell ui-widget-content">
<div style="width:250px">
<div class="itemContent itemContentRead" data-content="type">
</div>
<input type="hidden" class="typeItemContent" data-value="type"/>
<select class="itemContent itemContentEdit typeItemcontrol">
<option value="none"></option>
<option value="alternance">alternance</option>
<option value="continue" >continue</option>
</select>
</div>
</div>
<div class="itemCell ui-widget-content">
<input type="hidden" class="idDTO" data-value="_id.$oid"/>
</div>
<div class="itemCell ui-widget-content" style="width:180px">
<div class="itemContent" style="width:180px">
<button class="deleteDTOButton itemContent itemContentRead" value="delete"><span class="ui-icon ui-icon-closethick"></span></button>
<button class="updateDTOButton itemContent itemContentEdit" value="update"><span class="ui-icon ui-icon-check"></span></button>
<button class="cancelDTOButton itemContent itemContentEdit" value="cancel"><span class="ui-icon ui-icon-arrowreturnthick-1-w"></span></button>
</div>
</div>
</div>
<div class="itemRowContainer itemRow" style="height:2px; backround-color:red"></div>
</script>
</body>
</html>