-
Notifications
You must be signed in to change notification settings - Fork 45
/
dashboard.js
175 lines (147 loc) · 5.01 KB
/
dashboard.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
var dashboard = {
'add':function(){
$.ajax({ url: path+"dashboard/create.json", data: "", async: false, success: function(data){} });
},
'remove':function(id){
$.ajax({ url: path+"dashboard/delete.json", data: "id="+id, async: false, success: function(data){} });
},
'list':function(){
var result = {};
$.ajax({ url: path+"dashboard/list.json", dataType: 'json', async: false, success: function(data) {result = data;} });
return result;
},
'set':function(id, fields){
var result = {};
$.ajax({ type: "POST", url: path+"dashboard/set.json", data: "id="+id+"&fields="+JSON.stringify(fields), async: false, success: function(data){result = data;} });
return result;
},
'setcontent':function(id, content,height){
var result = {};
$.ajax({
type: "POST",
url : path+"dashboard/setcontent.json",
data : "&id="+id+'&content='+encodeURIComponent(content)+'&height='+height,
dataType: 'json',
async: false,
success : function(data) { result = data; }
});
return result;
},
'clone':function(id){
var result = {};
$.ajax({ url: path+"dashboard/clone.json", data: "id="+id, async: false, success: function(data){result = data;} });
return result;
}
}
// ASYNC : TRUE
// ----------------------------------------------------
// Should return a promise object for calling function to run .done(),.fail() etc
// the v1 version was not asyncronus and blocked page rendering
/*
eg:
calling this from a page is non-blocking:
```
dashboard_v2.list().done(function(data){
alert('found %s results'.replace('%s',data.length))
})
```
however
calling the v1 version would block the page render until complete:
` alert('found %s results'.replace('%s', dashboard.list().length))
@todo: remove v1 and replace for v2
*/
var dashboard_v2 = {
add: function(){
return dashboard_v2._fetch(path + "dashboard/create.json");
},
remove: function(id){
return dashboard_v2._fetch({
url: path + "dashboard/delete.json",
data: {id: id}
});
},
list: function(){
let url = path + "dashboard/list.json";
let options = {}
let promise = dashboard_v2._fetch(url, options)
promise.url = url;
return promise;
},
set: function(column, id, value){
var fields = {}
fields[column] = value;
return dashboard_v2._fetch({
url: path + "dashboard/set.json",
data: {
id: id,
fields: JSON.stringify(fields)
}
});
},
setcontent: function(id, content, height){
// call the dashboard/setcontent api endpoint and return the callback queue
return dashboard_v2._fetch({
type: "POST",
url : path+"dashboard/setcontent.json",
data : {
id: id,
content: encodeURIComponent(content),
height: height
},
dataType: 'json'
});
},
clone: function(id) {
return dashboard_v2._fetch({
url: path + "dashboard/clone.json",
data: {id: id}
});
},
// AJAX UTILITIES
// ---------------------
/**
* Send and test the response of an $.ajax request for error messages
*
* Wrapper for $.ajax & tests for {success: false} in response
*
* return standard $.ajax response
* return failed $.ajax response if error message exists
*
* @param: _fetch(settings)
* @param: _fetch(url,[settings])
* @see: https://api.jquery.com/jQuery.ajax/ for settings
* @author: emrys@openenergymonitor.org
*/
_fetch: function() {
// call an api endpoint and return the callback queue
var deferred = $.Deferred();
var promise = deferred.promise();
// if single object passed use that. else supply url and options
var settings = arguments[0] || {};
var jqxhr = null;
// return rejected promise if no url passed
if(!arguments[0]) deferred.reject(null, 'no url given');
// if first parameter is string use that as the url
if (typeof settings === 'string') {
let url = arguments[0] || '';
let settings = arguments[1] || {};
jqxhr = $.ajax(url, settings);
} else {
jqxhr = $.ajax(settings);
}
// on ajax success check response for error message
jqxhr.done(function(data, status, xhr) {
// reject if data has property success set to false
if (!data || data.hasOwnProperty('success') && data.success === false) {
deferred.reject(jqxhr, data.message || 'error');
} else {
deferred.resolve(data, status, xhr);
}
});
// on ajax error return rejected promise
jqxhr.fail(function(jqXHR, status, error) {
deferred.reject(jqXHR, status, error);
});
return promise;
}
}