forked from signalpoint/DrupalGap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drupalgap.js
1406 lines (1339 loc) · 42.8 KB
/
drupalgap.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var drupalgap = {
'modules':{
'core':[
{'name':'api'},
{'name':'block'},
{'name':'comment'},
{'name':'entity'},
{'name':'field'},
{'name':'image'},
{'name':'menu'},
{'name':'mvc'},
{'name':'node'},
{'name':'services',
'includes':[
{'name':'comment'},
{'name':'drupalgap_content'},
{'name':'drupalgap_system'},
{'name':'drupalgap_taxonomy'},
{'name':'drupalgap_user'},
{'name':'file'},
{'name':'node'},
{'name':'services'},
{'name':'system'},
{'name':'taxonomy_term'},
{'name':'taxonomy_vocabulary'},
{'name':'user'},
]
},
{'name':'system'},
{'name':'taxonomy'},
{'name':'user'},
{'name':'views_datasource'},
]
},
'module_paths':[],
'includes':[
{'name':'common'},
{'name':'file'},
{'name':'form'},
{'name':'menu'},
{'name':'module'},
{'name':'theme'},
],
/**
* User Default Values
* Do not change these values unless you are feeling adventurous.
*/
'user':{
'uid':0,
'name':'Anonymous', /* TODO - this value should come from the drupal site */
'roles':{
'1':'anonymous user'
},
},
'online':false,
'destination':'',
'api':{},
'back':false, /* moving backwards or not */
'back_path':'', /* the path to move back to */
'blocks':[],
'entity_info':{},
'field_info_fields':{},
'field_info_instances':{},
/*field_widget_info:{},*/
'form_errors':{},
'form_states':[],
loading:false, /* indicates if the loading message is shown or not */
'menus':{},
'menu_links':{},
'menu_router':{},
'mvc':{
'models':{},
'views':{},
'controllers':{}
},
'output':'', /* hold output generated by menu_execute_active_handler() */
'page':{
'jqm_events':[],
'title':'',
'variables':{},
'process':true,
options:{}, /* holds onto the current page's options, e.g. reloadPage, etc. */
},
'pages':[], /* Collection of page ids that are loaded into the DOM. */
'path':'', /* The current menu path. */
'router_path':'', /* The current menu router path. */
'services':{},
'sessid':null,
taxonomy_vocabularies:false, /* holds the services index of vocabularies */
'theme_path':'',
'themes':[],
'theme_registry':{},
'views_datasource':{},
}; // <!-- drupalgap -->
/**
* Given a path to a javascript file relative to the app's www directory,
* this will load the javascript file so it will be available in scope.
*/
function drupalgap_add_js() {
try {
var data;
if (arguments[0]) { data = arguments[0]; }
jQuery.ajax({
async:false,
type:'GET',
url:data,
data:null,
success:function(){
if (drupalgap.settings.debug) {
// Print the js path to the console.
console.log(data);
}
},
dataType:'script',
error: function(xhr, textStatus, errorThrown) {
console.log(JSON.stringify(xhr));
alert('drupalgap_add_js - error - (' + data + ' : ' + textStatus + ') ' + errorThrown);
}
});
}
catch (error) {
alert('drupalgap_add_js - ' + error);
}
}
/**
* Given a path to a css file relative to the app's www directory, this will
* attempt to load the css file so it will be available in scope.
*/
function drupalgap_add_css() {
try {
var data;
if (arguments[0]) { data = arguments[0]; }
$('<link/>', {rel: 'stylesheet', href: data}).appendTo('head');
}
catch (error) {
alert('drupalgap_add_css - ' + error);
}
}
/**
* Rounds up all blocks defined by hook_block_info and places them in the
* drupalgap.blocks array.
*/
function drupalgap_blocks_load() {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_blocks_load()');
console.log(JSON.stringify(arguments));
}
/*drupalgap.blocks[0] = {};
var modules = module_implements('block_info');
if (modules) {
$.each(modules, function(index, module){
var blocks = module_invoke(module, 'block_info');
if (blocks) {
$.each(blocks, function(delta, block){
// Assign the delta as the name of the block, set the delta of the
// block as well, and set the module name on the block for reference.
block.name = delta;
block.delta = delta;
block.module = module;
// Add the block to drupalgap.blocks.
eval("drupalgap.blocks[0]." + delta + " = block;");
//drupalgap.blocks[delta] = block;
});
}
});
}*/
drupalgap.blocks = module_invoke_all('block_info');
if (drupalgap.settings.debug) {
console.log(JSON.stringify(drupalgap.blocks));
}
}
catch (error) {
alert('drupalgap_blocks_load - ' + error);
}
}
/**
* Loads up all necessary assets to make DrupalGap ready.
*/
function drupalgap_bootstrap() {
try {
// Load up settings.
drupalgap_settings_load();
// Load up includes.
drupalgap_includes_load();
// Load up modules.
drupalgap_modules_load();
// Load up the theme.
drupalgap_theme_load();
// Load up blocks.
drupalgap_blocks_load();
// Initialize menu links.
menu_router_build();
// Initialize menus.
drupalgap_menus_load();
// Initialize the theme registry.
drupalgap_theme_registry_build();
// Initialize field widgets.
//drupalgap_field_widgets_load();
// Attach device back button handler (Android).
document.addEventListener("backbutton", drupalgap_back, false);
}
catch (error) {
alert('drupalgap_bootstrap - ' + error);
}
}
/**
* Takes option set 2, grabs the success/error callback(s), if any,
* and appends them onto option set 1's callback(s), then returns
* the newly assembled option set.
*/
function drupalgap_chain_callbacks(options_set_1, options_set_2) {
//console.log(JSON.stringify(options_set_1));
//console.log(JSON.stringify(options_set_2));
// Setup the new options.
var new_options_set = {};
$.extend(true, new_options_set, options_set_1);
// Chain the success callbacks.
if (options_set_2.success) {
if (new_options_set.success) {
if (!$.isArray(new_options_set.success)) {
var backup = new_options_set.success;
new_options_set.success = [];
new_options_set.success.push(backup);
}
new_options_set.success.push(options_set_2.success);
}
else {
new_options_set.success = options_set_2.success;
}
}
// Chain the error callbacks.
if (options_set_2.error) {
if (new_options_set.error) {
if (!$.isArray(new_options_set.error)) {
var backup = new_options_set.error;
new_options_set.error = [];
new_options_set.error.push(backup);
}
new_options_set.error.push(options_set_2.error);
}
else {
new_options_set.error = options_set_2.error;
}
}
// For all other variables in option set 2, add them to the new option set.
$.each(options_set_2, function(index, object){
if (index != 'success' && index != 'error') {
new_options_set[index] = object;
}
});
// Return the new option set.
//console.log(JSON.stringify(new_options_set));
return new_options_set;
}
/**
* Checks the devices connection and sets drupalgap.online to true if the
* device has a connection, false otherwise.
* @returns A string indicating the type of connection according to PhoneGap.
*/
function drupalgap_check_connection() {
try {
// We'll assume that Ripple emulation always has a connection, for now.
// http://stackoverflow.com/q/15950382/763010
if (typeof parent.window.ripple === 'function') {
drupalgap.online = true;
return 'Ethernet connection';
}
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
if (states[networkState] == 'No network connection') {
drupalgap.online = false;
}
else {
drupalgap.online = true;
}
return states[networkState];
}
catch (error) {
alert('drupalgap_check_connection - ' + error);
}
}
/**
* Implements PhoneGap's deviceready().
*/
function drupalgap_deviceready() {
// PhoneGap is loaded and it is now safe for DrupalGap to start...
drupalgap_bootstrap();
// Verify site path is set.
if (!drupalgap.settings.site_path || drupalgap.settings.site_path == '') {
navigator.notification.alert(
'No site path to Drupal set in the app/settings.js file!',
function(){},
'Error',
'OK'
);
return false;
}
// Check device connection. If the device is offline, warn the user and then
// go to the offline page.
drupalgap_check_connection();
if (!drupalgap.online) {
module_invoke_all('device_offline');
navigator.notification.alert(
'No connection found!',
function(){ drupalgap_goto('offline'); },
'Offline',
'OK'
);
return false;
}
else {
// Device is online, let's call any implementations of hook_deviceready().
// If any implementation returns false, that means they don't want DrupalGap
// to continue with the System Connect call, so we'll skip that and go
// straight to the App's front page.
var proceed = true;
var invocation_results = module_invoke_all('deviceready');
if (invocation_results && invocation_results.length > 0) {
$.each(invocation_results, function(index, object){
if (object === false) {
proceed = false;
return false;
}
});
}
if (!proceed) {
drupalgap_goto('');
// TODO - if module's are going to skip the System Connect call, then we
// need to make sure drupalgap.user is set up with appropriate defaults.
}
else {
// Device is online, let's build the default system connect options.
var options = {
'success':function(result){
// Call all hook_device_connected implementations then go to
// the front page.
module_invoke_all('device_connected');
drupalgap_goto('');
},
'error':function(jqXHR, textStatus, errorThrown) {
// Build an informative error message and display it.
var msg = 'drupalgap_deviceready() - failed connection to ' +
drupalgap.settings.site_path;
if (errorThrown != '') { msg += ' - ' + errorThrown; }
msg += ' - Check your device\'s connection and check that ' + drupalgap.settings.site_path +
' is online. If you continue to have problems visit ' +
'www.drupalgap.org for troubleshooting info.';
navigator.notification.alert(
msg,
function(){ drupalgap_goto('offline'); },
'Unable to Connect to Drupal',
'OK'
);
}
};
// If we have a session id in local storage, then we'll use it as the
// CSRF token when making the initial call to system connect. This will
// determine if the user is still authenticated with Drupal or not.
var token = window.localStorage.getItem('sessid');
if (token) {
drupalgap.sessid = token;
options.token = token;
options.beforeSend = function (request) {
request.setRequestHeader("X-CSRF-Token", options.token);
};
}
// DrupalGap System Connect Service Resource
drupalgap.services.drupalgap_system.connect.call(options);
}
}
}
/**
* Returns true if given value is empty. A generic way to test for emptiness.
*/
function drupalgap_empty(value) {
return (typeof value === "undefined" || value === null || value == '');
}
/**
*
*/
/*function drupalgap_field_widgets_load() {
try {
var modules = module_implements('field_widget_info');
if (!modules) { return null; }
$.each(modules, function(i, module){
var field_widgets = module_invoke(module, 'field_widget_info');
if (!field_widgets) { return; }
$.each(field_widgets, function(name, field_widget){
$.each(field_widget.field_types, function(j, field_type){
drupalgap.field_widget_info[field_type] = {
'module':module,
'field_widget':name
};
});
});
});
}
catch (error) {
alert('drupalgap_field_widgets_load - ' + error);
}
}*/
/**
* Checks if a given file exists, returns true or false.
* @param {string} path
* A path to a file
* @return {bool}
* True if file exists, else flase
*/
function drupalgap_file_exists(path) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_file_exists(' + path + ')');
}
var file_exists = false;
jQuery.ajax({
async:false,
type:'HEAD',
url:path,
success:function(){ file_exists = true; },
error:function(xhr, textStatus, errorThrown) { }
});
return file_exists;
}
catch (error) {
alert('drupalgap_file_exists - ' + error);
}
}
/**
* Reads entire file into a string and returns the string. Returns false if
* it fails.
*/
function drupalgap_file_get_contents(path, options) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_file_get_contents(' + path + ')');
}
var file = false;
var default_options = {
type:'GET',
url:path,
dataType:'html',
data:null,
async:false,
success:function(data){ file = data; },
error: function(xhr, textStatus, errorThrown) {
console.log('drupalgap_file_get_contents - failed to load file (' + path + ')');
}
};
$.extend(default_options, options);
jQuery.ajax(default_options);
return file;
}
catch (error) {
alert('drupalgap_file_get_contents - ' + error);
}
}
/**
* See drupal_format_plural() for more information.
* http://api.drupal.org/api/drupal/includes%21common.inc/function/format_plural/7
*/
function drupalgap_format_plural(count, singular, plural) {
try {
if (count == 1) {
return singular;
}
return plural;
}
catch (error) {
alert('drupalgap_format_plural - ' + error);
}
return null;
}
/**
* Given a JS function, this returns true if the function is available in the
* scope, false otherwise.
*/
function drupalgap_function_exists(name) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_function_exists(' + name + ')');
}
return (eval('typeof ' + name) == 'function');
}
catch (error) {
alert('drupalgap_function_exists - ' + error);
}
}
/**
* Given an html string from a *.tpl.html file, this will extract all of the
* placeholders names and return them in an array. Returns false otherwise.
*/
function drupalgap_get_placeholders_from_html(html) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_get_placeholders_from_html()');
console.log(JSON.stringify(arguments));
}
var placeholders = false;
if (html) {
placeholders = html.match(/(?!{:)([\w]+)(?=:})/g);
}
return placeholders;
}
catch (error) {
alert('drupalgap_get_placeholders_from_html - ' + error);
}
}
/**
*
* @param type
* @param name
*/
function drupalgap_get_path(type, name) {
try {
var path = '';
var found_it = false;
if (type == 'module') {
$.each(drupalgap.modules, function(bundle, modules){
$.each(modules, function(index, module){
if (name == module.name) {
path = drupalgap_modules_get_bundle_directory(bundle) + '/';
path += module.name;
found_it = true;
}
if (found_it) {
return false;
}
});
if (found_it) {
return false;
}
});
}
return path;
}
catch (error) {
alert('drupalgap_get_path - ' + error);
}
return null;
}
/**
* Implementation of drupal_get_title().
*/
function drupalgap_get_title() {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_get_title()');
console.log(JSON.stringify(arguments));
}
return drupalgap.page.title;
}
catch (error) {
alert('drupalgap_get_title - ' + error);
}
}
/**
* Given a router path, this will return an array containing the indexes of
* where the wildcards (%) are present in the router path. Returns false if
* there are no wildcards present.
*/
function drupalgap_get_wildcards_from_router_path(router_path) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_get_wildcards_from_router_path(' + router_path + ')');
}
var wildcards = false;
return wildcards;
}
catch (error) {
alert('drupalgap_get_wildcards_from_router_path - ' + error);
}
}
/**
* Given a drupal image file uri, this will return the path to the image on the
* Drupal site.
*/
function drupalgap_image_path(uri) {
try {
var altered = false;
// If any modules want to alter the path, let them do it.
var modules = module_implements('image_path_alter');
if (modules) {
$.each(modules, function(index, module){
var result = module_invoke(module, 'image_path_alter', uri);
if (result) {
altered = true;
uri = result;
return false;
}
});
}
if (!altered) {
// No one modified the image path, we'll use the default approach to
// generating the image src path.
var src = drupalgap.settings.site_path + drupalgap.settings.base_path + uri;
if (src.indexOf('public://') != -1) {
src = src.replace('public://', drupalgap.settings.file_public_path + '/');
}
return src;
}
else { return uri; }
}
catch (error) { drupalgap_error(error); }
}
/**
* Loads the js files in includes specified by drupalgap.includes.
*/
function drupalgap_includes_load() {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_includes_load()');
console.log(JSON.stringify(drupalgap.includes));
}
if (drupalgap.includes != null && drupalgap.includes.length != 0) {
$.each(drupalgap.includes, function(index, include){
var include_path = 'includes/' + include.name + '.inc.js';
jQuery.ajax({
async:false,
type:'GET',
url:include_path,
data:null,
success:function(){
if (drupalgap.settings.debug) {
// Print the include path to the console.
console.log(include_path);
}
},
dataType:'script',
error: function(xhr, textStatus, errorThrown) {
console.log(errorThrown);
// Look at the `textStatus` and/or `errorThrown` properties.
}
});
});
}
}
catch (error) {
alert('drupalgap_includes_load - ' + error);
}
}
/**
* Given an html list element id and an array of items, this will clear the
* list, populate it with the items, and then refresh the list.
*/
function drupalgap_item_list_populate(list_css_selector, items) {
try {
// TODO - this could use some validation and alerts for improper input.
$(list_css_selector).html("");
for (var i = 0; i < items.length; i ++) {
$(list_css_selector).append($("<li></li>",{"html":items[i]}));
}
$(list_css_selector).listview('refresh').listview();
}
catch (error) { drupalgap_error(error); }
}
/**
* Given a jQM page event, and the corresponding callback function name that
* handles the event, this function will call the callback function, if it has
* not already been called on the current page. This really is only used by
* menu_execute_active_handler() to prevent jQM from firing inline page event
* handlers more than once.
*/
function drupalgap_jqm_page_event_fire(event, callback, page_arguments) {
try {
// Concatenate the event name and the callback name together into a unique
// key so multiple callbacks can handle the same event.
var key = event + '-' + callback;
if ($.inArray(key, drupalgap.page.jqm_events) == -1 && drupalgap_function_exists(callback)) {
drupalgap.page.jqm_events.push(key);
var fn = window[callback];
if (page_arguments) {
// If the page arguments aren't an array, place them into an array so
// they can be applied to the callback function.
if (!$.isArray(page_arguments)) { page_arguments = [page_arguments]; }
fn.apply(null, Array.prototype.slice.call(page_arguments));
}
else { fn(); }
}
}
catch (error) {
alert('drupalgap_jqm_page_event_fire - ' + error);
}
}
/**
* Returns array of jQM Page event names.
* http://api.jquerymobile.com/category/events/
*/
function drupalgap_jqm_page_events() {
try {
return [
'pagebeforechange',
'pagebeforecreate',
'pagebeforehide',
'pagebeforeload',
'pagebeforeshow',
'pagechange',
'pagechangefailed',
'pagecreate',
'pagehide',
'pageinit',
'pageload',
'pageloadfailed',
'pageremove',
'pageshow'
];
}
catch (error) {
alert('drupalgap_jqm_page_events - ' + error);
}
}
/**
* Given a JSON object with a page id, a jQM page event name, a callback
* function to handle the jQM page event and any page arguments, this function
* will return the inline JS code needed to handle the event.
*/
function drupalgap_jqm_page_event_script_code(options) {
try {
var script_code = '<script type="text/javascript">' +
'$("#' + options.page_id + '").on("' + options.jqm_page_event + '", drupalgap_jqm_page_event_fire("' +
options.jqm_page_event + '", "' +
options.jqm_page_event_callback + '", ' +
options.jqm_page_event_args +
'));' +
'</script>';
return script_code;
}
catch (error) { drupalgap_error(error); }
}
/**
* Show the jQueryMobile loading message.
*/
function drupalgap_loading_message_show() {
try {
// Backwards compatability for versions prior to 7.x-1.6-alpha
if (drupalgap.loading === 'undefined') { drupalgap.loading = false; }
// Return if the loading message is already shown.
if (drupalgap.loading) { return; }
// Set default options. Override the options if any were set or sent in.
var options = {
text: 'Loading...',
textVisible: true
};
if (drupalgap.settings.loading) { options = drupalgap.settings.loading; }
if (arguments[0]) { options = arguments[0]; }
// Show the loading message.
$.mobile.loading("show", options);
drupalgap.loading = true;
}
catch (error) { drupalgap_error(error); }
}
/**
* Hide the jQueryMobile loading message.
*/
function drupalgap_loading_message_hide() {
try {
$.mobile.hidePageLoadingMsg();
drupalgap.loading = false;
}
catch (error) { drupalgap_error(error); }
}
/**
* Returns the suggested max width for elements within the content area.
*/
function drupalgap_max_width() {
try {
var padding = parseInt($('.ui-content').css('padding'));
if (isNaN(padding)) { padding = 16; } // use a 16px default if needed
return $(document).width() - padding*2;
}
catch (error) { drupalgap_error(error); }
}
/**
* Checks to see if the current user has access to the given path. Returns true
* if the user has access, false otherwise. You may optionally pass in a user
* account object as the second argument to check access on a specific user.
*/
function drupalgap_menu_access(path) {
try {
// User #1 is allowed to do anything, I mean anything.
if (drupalgap.user.uid == 1) { return true; }
// Everybody else will not have access unless we prove otherwise.
var access = false;
if (drupalgap.menu_links[path]) {
// Check to see if there is an access callback specified with the menu link.
if (typeof drupalgap.menu_links[path].access_callback === 'undefined') {
// No access call back specified, if there are any access arguments
// on the menu link, then it is assumed they are user permission machine
// names, so check that user account's role(s) for that permission to
// grant access.
if (drupalgap.menu_links[path].access_arguments) {
// TODO - implement
}
else {
// There is no access callback and no access arguments specified with
// the menu link, so we'll assume everyone has access.
access = true;
}
}
else {
// An access callback function is specified for this path...
// START HERE: https://github.com/signalpoint/DrupalGap/issues/191
var function_name = drupalgap.menu_links[path].access_callback;
if (drupalgap_function_exists(function_name)) {
// Grab the access callback function and make a copy of the path's
// access arguments.
var fn = window[function_name];
var access_arguments = drupalgap.menu_links[path].access_arguments.slice(0);
var args = arg();
drupalgap_prepare_argument_entities(access_arguments, args);
return fn.apply(null, Array.prototype.slice.call(access_arguments));
}
else {
alert('drupalgap_menu_access - access call back (' + function_name + ') does not exist');
}
}
}
else {
alert('drupalgap_menu_access - path (' + path + ') does not exist');
}
return access;
}
catch (error) {
alert('drupalgap_menu_access - ' + error);
}
}
/**
* Given a module name, this will return the module inside drupalgap.modules.
*/
function drupalgap_module_load(module_name) {
try {
var loaded_module = null;
$.each(drupalgap.modules, function(bundle, modules){
if (!loaded_module) {
$.each(modules, function(index, module){
if (module.name == module_name) {
// Save reference to module, then break out of loop.
loaded_module = module;
return false;
}
});
}
else {
// Module loaded, break out of loop.
return false;
}
});
return loaded_module;
}
catch (error) { alert('drupalgap_module_load - ' + error); }
}
/**
* Given a module bundle type, this will return the path to that module bundle's
* directory.
*/
function drupalgap_modules_get_bundle_directory(bundle) {
try {
dir = '';
if (bundle == 'core') { dir = 'modules'; }
else if (bundle == 'contrib') { dir = 'app/modules'; }
else if (bundle == 'custom') { dir = 'app/modules/custom'; }
return dir;
}
catch (error) {
alert('drupalgap_modules_get_bundle_directory - ' + error);
}
return '';
}
/**
* Loads each drupalgap module so they are available in the JS scope.
*/
function drupalgap_modules_load() {
if (drupalgap.modules != null && drupalgap.modules.length != 0) {
$.each(drupalgap.modules, function(bundle, modules){
$.each(modules, function(index, module){
// Determine module directory.
dir = drupalgap_modules_get_bundle_directory(bundle);
module_base_path = dir + '/' + module.name;
// Add module .js file to array of paths to load.
module_path = module_base_path + '/' + module.name + '.js';
modules_paths = [module_path];
// If there are any includes with this module, add them to the list of
// paths to include.
if (module.includes != null && module.includes.length != 0) {
$.each(module.includes, function(include_index, include_object){
modules_paths.push(module_base_path + '/' + include_object.name + '.js');
});
}
// Now load all the paths for this module.
$.each(modules_paths, function(modules_paths_index, modules_paths_object){
jQuery.ajax({
async:false,
type:'GET',
url:modules_paths_object,
data:null,
success:function(){
if (drupalgap.settings.debug) {
// Print the module path to the console.
console.log(modules_paths_object);
}
},
dataType:'script',
error: function(xhr, textStatus, errorThrown) {
alert('Failed to load module! (' + module.name + ')');
// Look at the `textStatus` and/or `errorThrown` properties.
}
});
});
});
});
// Now invoke hook_install on all modules.
module_invoke_all('install');
}
}
/**
* Given a router path (and optional path, defaults to current drupalgap path if
* one isn't provided), this takes the path's arguments and replaces any
* wildcards (%) in the router path with the corresponding path argument(s). It
* then returns the assembled path. Returns false otherwise.
*/
function drupalgap_place_args_in_path(input_path) {
try {
if (drupalgap.settings.debug) {
console.log('drupalgap_place_args_in_path(' + input_path + ')');
}
var assembled_path = false;
if (input_path) {
// Determine path to use and break it up into its args.
var path = drupalgap_path_get();
if (arguments[1]) { path = arguments[1]; }
var path_args = arg(null, path);
// Grab wild cards from router path then replace each wild card with
// the corresponding path arg.
var wildcards;
var input_path_args = arg(null, input_path);
if (input_path_args && input_path_args.length > 0) {
$.each(input_path_args, function(index, arg){
if (arg == '%') {
if (!wildcards) { wildcards = []; }
wildcards.push(index);
}
});