-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflag.js
More file actions
297 lines (284 loc) · 8.29 KB
/
flag.js
File metadata and controls
297 lines (284 loc) · 8.29 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
/**
* Implements hook_services_success().
*/
function flag_services_postprocess(options, data) {
try {
// Extract the flag settings from the system connect result data.
if (options.service == 'system' && options.resource == 'connect') {
if (data.flag) {
drupalgap.flag = data.flag;
}
else {
console.log('flag_services_postprocess - failed to extract flag settings from system connect.');
}
}
}
catch (error) { console.log('flag_services_postprocess - ' + error); }
}
/**
* Implements hook_entity_post_render_content().
*/
function flag_entity_post_render_content(entity, entity_type, bundle) {
try {
// Since flag isn't a field, we'll just prepend it to the entity content.
if (entity_type == 'node') {
var flags = flag_get_entity_flags(entity_type, bundle);
if (!flags) { return; }
var entity_id = entity[entity_primary_key(entity_type)];
var html = '';
var page_id = drupalgap_get_page_id();
$.each(flags, function(fid, flag) {
var container_id = flag_container_id(flag.name, entity_id);
html += '<div id="' + container_id + '"></div>' +
drupalgap_jqm_page_event_script_code(
{
page_id: page_id,
jqm_page_event: 'pageshow',
jqm_page_event_callback: '_flag_pageshow',
jqm_page_event_args: JSON.stringify({
fid: fid,
entity_id: entity_id,
entity_type: entity_type,
bundle: bundle
})
},
flag.fid
);
});
entity.content = html + entity.content;
}
}
catch (error) {
console.log('flag_entity_post_render_content - ' + error);
}
}
/**
*
*/
function _flag_pageshow(options) {
try {
var flag = flag_load(options.fid);
flag_is_flagged(flag.name, options.entity_id, Drupal.user.uid, {
success: function(result) {
try {
var html = '';
var flagged = result[0];
var text = null;
var action = null;
if (flagged) {
text = flag.options.unflag_short;
action = 'unflag';
}
else {
text = flag.options.flag_short;
action = 'flag';
}
html += theme('flag', {
fid: flag.fid,
entity_id: options.entity_id,
action: action,
text: text,
entity_type: options.entity_type,
bundle: options.bundle
});
var container_id = flag_container_id(flag.name, options.entity_id);
$('#' + container_id).html(html).trigger('create');
}
catch (error) { console.log('_flag_pageshow - success - ' + error); }
}
});
}
catch (error) { console.log('_flag_pageshow - ' + error); }
}
/**
*
*/
function _flag_onclick(fid, entity_type, bundle, entity_id, action) {
try {
var flag = flag_load(fid);
if (!flag) { return; }
flag_flag(flag.name, entity_id, action, Drupal.user.uid, false, {
entity_type: entity_type,
bundle: bundle,
success: function(result) {
try {
if (result[0]) {
// Redraw the flag to be opposite of what just happened, and show
// an alert of what just happened.
var msg = null;
var text = null;
if (action == 'flag') {
action = 'unflag';
text = flag.options.unflag_short;
if (typeof flag.options.flag_message !== 'undefined' && flag.options.flag_message != '') {
msg = flag.options.flag_message;
}
}
else {
action = 'flag';
text = flag.options.flag_short;
if (typeof flag.options.unflag_message !== 'undefined' && flag.options.unflag_message != '') {
msg = flag.options.unflag_message;
}
}
if (msg) { drupalgap_alert(msg); }
var html = theme('flag', {
fid: fid,
entity_id: entity_id,
action: action,
text: text,
entity_type: entity_type,
bundle: bundle
});
var container_id = flag_container_id(flag.name, entity_id);
$('#' + container_id).html(html).trigger('create');
}
}
catch (error) { console.log('_flag_onclick - success - ' + error); }
}
});
}
catch (error) { console.log('_flag_onclick - ' + error); }
}
/**
*
*/
function flag_get_entity_flags(entity_type, bundle) {
try {
var flags = null;
$.each(drupalgap.flag, function(fid, flag) {
if (flag.entity_type == entity_type) {
$.each(flag.types, function(index, _bundle) {
if (bundle == _bundle) {
if (!flags) { flags = {}; }
flags[fid] = flag;
return false;
}
});
}
});
return flags;
}
catch (error) { console.log('flag_get_entity_flags - ' + error); }
}
/**
*
*/
function flag_container_id(flag_name, entity_id) {
try {
return 'flag_' + flag_name + '_' + entity_id;
}
catch (error) { console.log('flag_container_id - ' + error); }
}
/**
*
*/
function flag_load(fid) {
try {
var flag = null;
$.each(drupalgap.flag, function(_fid, _flag) {
if (fid == _fid) {
flag = _flag;
return false;
}
});
return flag;
}
catch (error) { console.log('flag_load - ' + error); }
}
/********|
* Theme |
********/
/**
* Theme's a flag.
*/
function theme_flag(variables) {
try {
var attributes = {
onclick: "_flag_onclick(" + variables.fid + ", '" + variables.entity_type + "', '" + variables.bundle + "', " +
variables.entity_id + ", '" + variables.action + "')"
};
return theme('button_link', {
path: null,
text: variables.text,
attributes: attributes
});
}
catch (error) { console.log('theme_flag - ' + error); }
}
/***********|
* Services |
***********/
/**
* Check if a entity was flagged by a user.
* @param {String} flag_name
* @param {Number} entity_id
* @param {Number} uid (optional)
* @param {Object} options
*/
function flag_is_flagged(flag_name, entity_id, uid, options) {
try {
options.method = 'POST';
options.path = 'flag/is_flagged.json';
options.service = 'flag';
options.resource = 'is_flagged';
var data = {
flag_name: flag_name,
entity_id: entity_id
};
if (uid) { data.uid = uid; }
options.data = JSON.stringify(data);
Drupal.services.call(options);
}
catch (error) { console.log('flag_is_flagged - ' + error); }
}
/**
* Flags (or unflags) an entity.
* @param {String} flag_name
* @param {Number} entity_id
* @param {String} action
* @param {Number} uid (optional)
* @param {Boolean} skip_permission_check (optional)
* @param {Object} options
*/
function flag_flag(flag_name, entity_id, action, uid, skip_permission_check, options) {
try {
options.method = 'POST';
options.path = 'flag/flag.json';
options.service = 'flag';
options.resource = 'flag';
if (typeof action === 'undefined') { action = 'flag'; }
if (typeof skip_permission_check === 'undefined') { skip_permission_check = false; }
var data = {
flag_name: flag_name,
entity_id: entity_id,
action: action,
skip_permission_check: skip_permission_check
};
if (uid) { data.uid = uid; }
options.data = JSON.stringify(data);
Drupal.services.call(options);
}
catch (error) { console.log('flag_flag - ' + error); }
}
/**
* Count the flags number on a specific node.
* @param {String} flag_name
* @param {Number} entity_id
* @param {Object} options
*/
function flag_countall(flag_name, entity_id, options) {
try {
options.method = 'POST';
options.path = 'flag/countall.json';
options.service = 'flag';
options.resource = 'countall';
var data = {
flag_name: flag_name,
entity_id: entity_id
};
options.data = JSON.stringify(data);
Drupal.services.call(options);
}
catch (error) { console.log('flag_countall - ' + error); }
}