forked from janexip/flag
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflag.js
More file actions
392 lines (366 loc) · 11.8 KB
/
flag.js
File metadata and controls
392 lines (366 loc) · 11.8 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
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
/**
* 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_view_alter().
*/
function flag_entity_view_alter(entity_type, entity_id, mode, build) {
try {
if (!Drupal.user.uid) { return; } // Anonymous users cannot flag anything.
var bundle = null;
var entity = null;
if (entity_type == 'node') { entity = build.node; bundle = entity.type; }
else if (entity_type == 'user') { entity = build.account; }
if (!entity) { return; }
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) {
// Skip user profiles if the flag is configured that way.
if (entity_type == 'user' && !flag.options.show_on_profile) { return; }
// Only
if (entity_type == 'user' && flag.options.access_uid == 'others' && Drupal.user.uid == entity_id) { return; }
// Build the render array.
build['flag_' + fid]= { markup: '<div id="' + flag_container_id(flag.name, entity_id) + '" class="flag"></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
})
},
entity_type + '-' + entity_id + '-' + flag.fid
) };
});
}
catch (error) { console.log('flag_entity_view_alter - ' + 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) {
if (!bundle) {
if (!flags) { flags = {}; }
flags[fid] = flag;
return;
}
$.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); }
}
/**
* HELPERS
*/
/**
* Returns html for a quick link that can be used for a "split" link in a jQuery Mobile list view item.
* @param {String} flag_name
* @param {String} entity_type
* @param {Number} entity_id
* @param {Number} flagged The current flag status.
* @returns {String}
*/
function flag_quick_link(flag_name, entity_type, entity_id, flagged) {
return l('Do it', null, {
attributes: {
onclick: _flag_quick_link_onclick_attribute(flag_name, entity_type, entity_id, flagged),
'data-theme': _flag_quick_link_data_theme(flagged)
}
});
}
/**
* Handles clicks on jQM list view "split" quick links to toggle the flag for a given entity.
* @param {Object} button
* @param {String} flag_name
* @param {String} entity_type
* @param {Number} entity_id
* @param {Number} flagged
*/
function _flag_quick_link_onclick(button, flag_name, entity_type, entity_id, flagged) {
var action = flagged ? 'unflag' : 'flag';
flag_flag(flag_name, entity_id, action, Drupal.user.uid, true, {
success: function(results) {
if (!results[0]) { console.log(t('Flagging was unsuccessful!')); return; }
var new_theme = _flag_quick_link_data_theme(!flagged);
var old_theme = _flag_quick_link_data_theme(flagged);
var new_onclick = _flag_quick_link_onclick_attribute(flag_name, entity_type, entity_id, !flagged);
var new_class = 'ui-btn-' + new_theme;
var old_class = 'ui-btn-' + old_theme;
$(button).attr('data-theme', new_theme).attr('onclick', new_onclick).removeClass(old_class).addClass(new_class).trigger('create');
// In case the entity page view was already in the DOM, try to remove it.
setTimeout(function() {
drupalgap_remove_page_from_dom(drupalgap_get_page_id(entity_type + '/' + entity_id));
}, 50);
}
});
}
/**
* Returns the onclick attribute value for a flag quick link.
* @param {String} flag_name
* @param {String} entity_type
* @param {Number} entity_id
* @param {Boolean} flagged
* @returns {string}
*/
function _flag_quick_link_onclick_attribute(flag_name, entity_type, entity_id, flagged) {
return "_flag_quick_link_onclick(this, '" + flag_name + "', '" + entity_type + "', " + entity_id + ", " + flagged + ")";
}
/**
* Returns the data theme to use on a quick link button for the given flagged status.
* @param {Boolean} flagged
* @returns {string}
*/
function _flag_quick_link_data_theme(flagged) {
var data_theme_flagged = 'b';
var data_theme_unflagged = 'a';
if (drupalgap.settings.flag) {
if (drupalgap.settings.flag.data_theme_flagged) { data_theme_flagged = drupalgap.settings.flag.data_theme_flagged; }
if (drupalgap.settings.flag.data_theme_unflagged) { data_theme_unflagged = drupalgap.settings.flag.data_theme_unflagged; }
}
return flagged ? data_theme_flagged : data_theme_unflagged;
}
/********|
* Theme |
********/
/**
* Theme's a flag.
*/
function theme_flag(variables) {
try {
var css_class = variables.action == 'flag' ? 'unflagged' : 'flagged';
var attributes = {
onclick: "_flag_onclick(" + variables.fid + ", '" + variables.entity_type + "', '" + variables.bundle + "', " +
variables.entity_id + ", '" + variables.action + "')",
'class': css_class,
'data-theme': _flag_quick_link_data_theme(css_class == 'flagged')
};
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); }
}