Idea: General structure of the new drupal.js

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
kkaefer's picture

Some of my ideas for the future drupal.js:

Note: This is demo/fake code, not actual, working code.

var Drupal = {
  jsEnabled: false,
  basePath: 'http://example.com/drupal',
  locales: {},
  settings: {},
  
  addLocales: function(translations) { ... },
  t: function(str, args) { ... },
  watchdog: function(type, message, severity, link) { ... },
  parseJson: function(data) { ... }
};

jQuery.dimensions = function() { ... };
jQuery.absolutePosition = function() { ... };
jQuery.freezeHeight = function() { ... };
jQuery.redirectFormButton = function() { ... };
  • Drupal.jsEnabled is of course set in $(document).ready();.
  • Drupal.basePath could be either set in the page header or in a separate JavaScript file that is generated by PHP (better solution, IMHO).
  • Drupal.locales is an empty array that can be filled by adding JavaScript files to the page that call Drupal.addLocales({ ... });. The addition of these translation files is done by modules. We could provide a standardized way to add js-translation files.
  • Drupal.settings will be the settings storage for all modules. (See also the issue “A centralized place for JavaScript settings”).
  • Drupal.addLocales() performs the addition of locales to the global translation storage.
  • Drupal.t() is an equivalent to Drupal's server-side t() function: It tries to replace the english version of a string with a translation (if available) and replaces variables inside the string.
  • Drupal.watchdog() is an equivalent to Drupal's server-side watchdog() function: It sends an AJAX request to the server which adds the message to Drupal's log.
  • Drupal.parseJson() tries to interpret JSON (as text) which is returned by some server requests.

We should try to convert as much functions as possible into jQuery “plugins” so that we just have to call $('fieldset.collapsible').makeFieldsetCollapsible(); or similar functions.

Comments

push An addition to the

kkaefer's picture

push

An addition to the JavaScript translation thing: We should also take into account plural forms. Any suggestions?