Source: src/modules/system/system.js

  1. var _system_reload_page = null;
  2. var _system_reload_messages = null;
  3. /**
  4. * Implements hook_install().
  5. */
  6. function system_install() {
  7. // Remove any old forms from local storage, then purge the form expiration tracker.
  8. for (var form_id in Drupal.cache_expiration.forms) {
  9. if (!Drupal.cache_expiration.forms.hasOwnProperty(form_id)) { continue; }
  10. drupalgap_form_local_storage_delete(form_id);
  11. }
  12. Drupal.cache_expiration.forms = {};
  13. window.localStorage.setItem('cache_expiration', JSON.stringify(Drupal.cache_expiration));
  14. }
  15. /**
  16. * Implements hook_block_info().
  17. * @return {Object}
  18. */
  19. function system_block_info() {
  20. // System blocks.
  21. var blocks = {
  22. 'main': {
  23. 'delta': 'main',
  24. 'module': 'system'
  25. },
  26. messages: {
  27. delta: 'messages',
  28. module: 'system'
  29. },
  30. 'logo': {
  31. 'delta': 'logo',
  32. 'module': 'system'
  33. },
  34. logout: {
  35. delta: 'logout',
  36. module: 'system'
  37. },
  38. 'title': {
  39. 'delta': 'title',
  40. 'module': 'system'
  41. },
  42. 'powered_by': {
  43. 'delta': 'powered_by',
  44. 'module': 'system'
  45. },
  46. 'help': {
  47. 'delta': 'help',
  48. 'module': 'system'
  49. }
  50. };
  51. // Make additional blocks for each system menu.
  52. var system_menus = menu_list_system_menus();
  53. for (var menu_name in system_menus) {
  54. if (!system_menus.hasOwnProperty(menu_name)) { continue; }
  55. var menu = system_menus[menu_name];
  56. var block_delta = menu.menu_name;
  57. blocks[block_delta] = {
  58. name: block_delta,
  59. delta: block_delta,
  60. module: 'menu'
  61. };
  62. }
  63. return blocks;
  64. }
  65. /**
  66. * Implements hook_block_view().
  67. * @param {String} delta
  68. * @return {String}
  69. */
  70. function system_block_view(delta) {
  71. try {
  72. switch (delta) {
  73. case 'main':
  74. // This is the main content block, it is required to be in a theme's
  75. // region for the content of a page to show up (nodes, users, taxonomy,
  76. // comments, etc). Depending on the menu link router, we need to route
  77. // this through the appropriate template files and functions.
  78. return drupalgap_render_page();
  79. break;
  80. case 'messages':
  81. // If there are any messages waiting to be displayed, render them, then
  82. // clear out the messages array.
  83. var html = '';
  84. if (drupalgap.messages.length == 0) { return html; }
  85. for (var index in drupalgap.messages) {
  86. if (!drupalgap.messages.hasOwnProperty(index)) { continue; }
  87. var msg = drupalgap.messages[index];
  88. html += '<div class="messages ' + msg.type + '">' +
  89. msg.message +
  90. '</div>';
  91. }
  92. drupalgap.messages = [];
  93. return html;
  94. break;
  95. case 'logo':
  96. if (drupalgap.settings.logo) {
  97. return '<div class="logo">' +
  98. l(theme('image', {'path': drupalgap.settings.logo}), '') +
  99. '</div>';
  100. }
  101. return '';
  102. break;
  103. case 'logout':
  104. if (Drupal.user.uid) { return theme('logout'); }
  105. return '';
  106. break;
  107. case 'title':
  108. var title_id = system_title_block_id(drupalgap_path_get());
  109. return '<h1 id="' + title_id + '" class="page-title"></h1>';
  110. break;
  111. case 'powered_by':
  112. return '<p style="text-align: center;">' + t('Powered by') + ': ' +
  113. l('DrupalGap', 'http://www.drupalgap.org', {InAppBrowser: true}) +
  114. '</p>';
  115. break;
  116. case 'help':
  117. return l('Help', 'http://www.drupalgap.org/support');
  118. break;
  119. default:
  120. return '';
  121. break;
  122. }
  123. }
  124. catch (error) { console.log('system_block_info - ' + error); }
  125. }
  126. /**
  127. * Implements hook_menu().
  128. * @return {Object}
  129. */
  130. function system_menu() {
  131. var items = {
  132. 'dashboard': {
  133. 'title': t('Dashboard'),
  134. 'page_callback': 'system_dashboard_page'
  135. },
  136. 'error': {
  137. 'title': t('Error'),
  138. 'page_callback': 'system_error_page'
  139. },
  140. 'offline': {
  141. 'title': t('Offline'),
  142. 'page_callback': 'system_offline_page'
  143. },
  144. '401': {
  145. title: '401 - ' + t('Not Authorized'),
  146. page_callback: 'system_401_page'
  147. },
  148. '404': {
  149. title: '404 - ' + t('Not Found'),
  150. page_callback: 'system_404_page'
  151. }
  152. };
  153. items['_reload'] = {
  154. title: t('Reloading') + '...',
  155. page_callback: 'system_reload_page',
  156. pageshow: 'system_reload_pageshow'
  157. };
  158. return items;
  159. }
  160. /**
  161. * Page callback for the 401 page.
  162. * @param {String} path
  163. * @return {String}
  164. */
  165. function system_401_page(path) {
  166. return t('Sorry, you are not authorized to view this page.');
  167. }
  168. /**
  169. * Page callback for the 404 page.
  170. * @param {String} path
  171. * @return {String}
  172. */
  173. function system_404_page(path) {
  174. return t('Sorry, the page you requested was not found.');
  175. }
  176. /**
  177. * The page callback for the reload page.
  178. * @return {String}
  179. */
  180. function system_reload_page() {
  181. try {
  182. // Set aside any messages, then return an empty page.
  183. var messages = drupalgap_get_messages();
  184. if (!empty(messages)) {
  185. _system_reload_messages = messages.slice();
  186. drupalgap_set_messages([]);
  187. }
  188. return '';
  189. }
  190. catch (error) { console.log('system_reload_page - ' + error); }
  191. }
  192. /**
  193. * The pageshow callback for the reload page.
  194. */
  195. function system_reload_pageshow() {
  196. try {
  197. // Set any messages that were set aside.
  198. if (_system_reload_messages && !empty(_system_reload_messages)) {
  199. for (var i = 0; i < _system_reload_messages.length; i++) {
  200. drupalgap_set_message(
  201. _system_reload_messages[i].message,
  202. _system_reload_messages[i].type
  203. );
  204. }
  205. _system_reload_messages = null;
  206. }
  207. drupalgap_loading_message_show();
  208. }
  209. catch (error) { console.log('system_reload_pageshow - ' + error); }
  210. }
  211. /**
  212. * Implements hook_system_drupalgap_goto_post_process().
  213. * @param {String} path
  214. */
  215. function system_drupalgap_goto_post_process(path) {
  216. try {
  217. // To reload the "current" page, grab the path we have been requested to
  218. // reload, clear out our global reference to it, then go!
  219. // @see https://github.com/signalpoint/DrupalGap/issues/254
  220. if (path == '_reload') {
  221. if (!_system_reload_page) { return; }
  222. var path = '' + _system_reload_page;
  223. _system_reload_page = null;
  224. drupalgap_loading_message_show();
  225. drupalgap_goto(path, { reloadPage: true });
  226. }
  227. }
  228. catch (error) {
  229. console.log('system_drupalgap_goto_post_process - ' + error);
  230. }
  231. }
  232. /**
  233. * Page callback for the dashboard page.
  234. * @return {Object}
  235. */
  236. function system_dashboard_page() {
  237. try {
  238. var content = {};
  239. content.site_info = {
  240. markup: '<h4 style="text-align: center;">' +
  241. Drupal.settings.site_path +
  242. '</h4>'
  243. };
  244. content.welcome = {
  245. markup: '<h2 style="text-align: center;">' +
  246. t('Welcome to DrupalGap') +
  247. '</h2>' +
  248. '<p style="text-align: center;">' +
  249. t('The open source application development kit for Drupal!') +
  250. '</p>'
  251. };
  252. if (drupalgap.settings.logo) {
  253. content.logo = {
  254. markup: '<center>' +
  255. theme('image', {path: drupalgap.settings.logo}) +
  256. '</center>'
  257. };
  258. }
  259. content.get_started = {
  260. theme: 'button_link',
  261. text: t('Getting Started Guide'),
  262. path: 'http://www.drupalgap.org/get-started',
  263. options: {InAppBrowser: true}
  264. };
  265. content.support = {
  266. theme: 'button_link',
  267. text: t('Support'),
  268. path: 'http://www.drupalgap.org/support',
  269. options: {InAppBrowser: true}
  270. };
  271. return content;
  272. }
  273. catch (error) { console.log('system_dashboard_page - ' + error); }
  274. }
  275. /**
  276. * The page callback for the error page.
  277. * @return {Object}
  278. */
  279. function system_error_page() {
  280. var content = {
  281. info: {
  282. markup: '<p>' + t('An unexpected error has occurred!') + '</p>'
  283. }
  284. };
  285. return content;
  286. }
  287. /**
  288. * Call back for the offline page.
  289. * @return {Object}
  290. */
  291. function system_offline_page() {
  292. try {
  293. var content = {
  294. 'message': {
  295. 'markup': '<h2>' + t('Failed Connection') + '</h2>' +
  296. '<p>' + t("Oops! We couldn't connect to") + ':</p>' +
  297. '<p>' + Drupal.settings.site_path + '</p>'
  298. },
  299. 'try_again': {
  300. 'theme': 'button',
  301. 'text': t('Try Again'),
  302. 'attributes': {
  303. 'onclick': 'javascript:offline_try_again();'
  304. }
  305. },
  306. 'footer': {
  307. 'markup': '<p>' +
  308. t("Check your device's network settings and try again.") +
  309. '</p>'
  310. }
  311. };
  312. return content;
  313. }
  314. catch (error) { console.log('system_offline_page - ' + error); }
  315. }
  316. /**
  317. * When the 'try again' button is clicked, check for a connection and if it has
  318. * one make a call to system connect then go to the front page, otherwise just
  319. * inform user the device is still offline.
  320. * @return {*}
  321. */
  322. function offline_try_again() {
  323. try {
  324. var connection = drupalgap_check_connection();
  325. if (drupalgap.online) {
  326. system_connect({
  327. success: function() {
  328. drupalgap_goto('');
  329. }
  330. });
  331. }
  332. else {
  333. var msg = t('Sorry, no connection found!') + ' (' + connection + ')';
  334. drupalgap_alert(msg, {
  335. title: 'Offline'
  336. });
  337. return false;
  338. }
  339. }
  340. catch (error) { console.log('offline_try_again - ' + error); }
  341. }
  342. /**
  343. * Returns an array of region names defined by the system that themes must use.
  344. * We do this so Core and Contrib Modules can use these regions for UI needs.
  345. * @return {Array}
  346. */
  347. function system_regions_list() {
  348. var regions = ['header', 'content', 'footer'];
  349. return regions;
  350. }
  351. /**
  352. * Add default buttons to a form and set its prefix.
  353. * @param {Object} form
  354. * @param {Object} form_state
  355. * @return {Object}
  356. */
  357. function system_settings_form(form, form_state) {
  358. try {
  359. // Add submit button to form if one isn't present.
  360. if (!form.elements.submit) {
  361. form.elements.submit = {
  362. type: 'submit',
  363. value: t('Save configuration')
  364. };
  365. }
  366. // Add cancel button to form if one isn't present.
  367. if (!form.buttons.cancel) {
  368. form.buttons['cancel'] = drupalgap_form_cancel_button();
  369. }
  370. // Attach submit handler.
  371. form.submit.push('system_settings_form_submit');
  372. return form;
  373. }
  374. catch (error) { console.log('system_settings_form - ' + error); }
  375. }
  376. /**
  377. * Execute the system_settings_form.
  378. * @param {Object} form
  379. * @param {Object} form_state
  380. */
  381. function system_settings_form_submit(form, form_state) {
  382. try {
  383. if (form_state.values) {
  384. for (var variable in form_state.values) {
  385. if (!form_state.values.hasOwnProperty(variable)) { continue; }
  386. var value = form_state.values[variable];
  387. variable_set(variable, value);
  388. }
  389. }
  390. }
  391. catch (error) { console.log('system_settings_form_submit - ' + error); }
  392. }
  393. /**
  394. * Returns the block id used on the system's title block.
  395. * @param {String} path
  396. * @return {String}
  397. */
  398. function system_title_block_id(path) {
  399. try {
  400. var id = 'drupalgap_page_title_' + drupalgap_get_page_id(path);
  401. return id;
  402. }
  403. catch (error) { console.log('system_title_block_id - ' + error); }
  404. }
  405. /**
  406. * The default access callback function for the logout block. Allows the block
  407. * to only be shown when a user is viewing their own profile.
  408. * @param {Object} options
  409. * @return {Boolean}
  410. */
  411. function system_logout_block_access_callback(options) {
  412. try {
  413. var args = arg(null, options.path);
  414. if (
  415. args &&
  416. args.length == 2 &&
  417. args[0] == 'user' &&
  418. args[1] == Drupal.user.uid
  419. ) { return true; }
  420. return false;
  421. }
  422. catch (error) {
  423. console.log('system_logout_block_access_callback - ' + error);
  424. }
  425. }