Projet

Général

Profil

Paste
Télécharger (5,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / token_insert / token_insert_wysiwyg / plugins / token_insert_wysiwyg / token_insert_wysiwyg.js @ 5721e759

1
/*jshint strict:true, browser:true, curly:true, eqeqeq:true, expr:true, forin:true, latedef:true, newcap:true, noarg:true, trailing: true, undef:true, unused:true */
2
/*global Drupal:true, jQuery: true*/
3
(function ($, Drupal) {
4
  "use strict";
5
  function hideShowFieldset(buttonid, action) {
6
    var $fieldset = $('#' + buttonid).closest('fieldset');
7
    $fieldset[action]();
8
  }
9

    
10
  var dialogs = {};
11

    
12
  /**
13
   * Wysiwyg plugin button implementation for token_insert plugin.
14
   */
15
  Drupal.wysiwyg.plugins.token_insert_wysiwyg = {
16
    /**
17
     * Return whether the passed node belongs to this plugin.
18
     *
19
     * @param node
20
     *   The currently focused DOM element in the editor content.
21
     */
22
    isNode: function(node) {
23
      return ($(node).is('img.token_insert_wysiwyg-token_insert_wysiwyg'));
24
    },
25

    
26
    /**
27
     * Execute the button.
28
     *
29
     * @param data
30
     *   An object containing data about the current selection:
31
     *   - format: 'html' when the passed data is HTML content, 'text' when the
32
     *     passed data is plain-text content.
33
     *   - node: When 'format' is 'html', the focused DOM element in the editor.
34
     *   - content: The textual representation of the focused/selected editor
35
     *     content.
36
     * @param settings
37
     *   The plugin settings, as provided in the plugin's PHP include file.
38
     * @param instanceId
39
     *   The ID of the current editor instance.
40
     */
41
    invoke: function(data, settings, instanceId) {
42
      Drupal.wysiwyg.plugins.token_insert_wysiwyg.insert_form(data, settings, instanceId);
43
    },
44

    
45

    
46
    insert_form: function (data, settings, instanceId) {
47
      if (!dialogs[instanceId]) {
48
        var id = 'token-insert-dialog-' + instanceId;
49
        var $dialogdiv = $('<div id="' + id + '"></div>').appendTo('body');
50
        var ajax_settings = {
51
          url: Drupal.settings.basePath + 'token_insert_wysiwyg/insert/' + instanceId,
52
          event: 'dialog.token-insert-wysiwyg',
53
          method: 'html'
54
        };
55
        new Drupal.ajax(id, $dialogdiv[0], ajax_settings);
56
        $dialogdiv.trigger(ajax_settings.event);
57
      }
58
      else {
59
        dialogs[instanceId].dialog("open");
60
      }
61
    },
62

    
63
    insertIntoEditor: function (token, editor_id) {
64
      Drupal.wysiwyg.instances[editor_id].insert(token);
65
    },
66

    
67
    /**
68
     * Prepare all plain-text contents of this plugin with HTML representations.
69
     *
70
     * Optional; only required for "inline macro tag-processing" plugins.
71
     *
72
     * @param content
73
     *   The plain-text contents of a textarea.
74
     * @param settings
75
     *   The plugin settings, as provided in the plugin's PHP include file.
76
     * @param instanceId
77
     *   The ID of the current editor instance.
78
     */
79
    attach: function(content, settings) {
80
      content = content.replace(/<!--token_insert_wysiwyg-->/g, this._getPlaceholder(settings));
81
      // hide the token_insert_text fieldset when wysiwyg is enabled,
82
      // token_insert_test won't work then, users should use the
83
      // wysiwyg plugin instead.
84
      if (typeof Drupal.settings.token_insert !== 'undefined') {
85
        for (var index in Drupal.settings.token_insert.buttons) {
86
          if (Drupal.settings.token_insert.buttons.hasOwnProperty(index)) {
87
            hideShowFieldset(index, 'hide');
88
          }
89
        }
90
      }
91
      return content;
92
    },
93

    
94
    /**
95
     * Process all HTML placeholders of this plugin with plain-text contents.
96
     *
97
     * Optional; only required for "inline macro tag-processing" plugins.
98
     *
99
     * @param content
100
     *   The HTML content string of the editor.
101
     * @param settings
102
     *   The plugin settings, as provided in the plugin's PHP include file.
103
     * @param instanceId
104
     *   The ID of the current editor instance.
105
     */
106
    detach: function(content) {
107
      var $content = $('<div>' + content + '</div>');
108
      // show the token_insert_text fieldset when wysiwyg is disabled
109
      if (typeof Drupal.settings.token_insert !== 'undefined') {
110
        for (var index in Drupal.settings.token_insert.buttons) {
111
          if (Drupal.settings.token_insert.buttons.hasOwnProperty(index)) {
112
            hideShowFieldset(index, 'show');
113
          }
114
        }
115
      }
116
      return $content.html();
117
    },
118

    
119
    /**
120
     * Helper function to return a HTML placeholder.
121
     *
122
     * The 'drupal-content' CSS class is required for HTML elements in the editor
123
     * content that shall not trigger any editor's native buttons (such as the
124
     * image button for this example placeholder markup).
125
     */
126
    _getPlaceholder: function (settings) {
127
      return '<img src="' + settings.path + '/images/spacer.gif" alt="&lt;--token_insert_wysiwyg-&gt;" title="&lt;--token_insert_wysiwyg--&gt;" class="token_insert_wysiwyg-token_insert_wysiwyg drupal-content" />';
128
    }
129
  };
130

    
131
  Drupal.ajax.prototype.commands.tokenInsertTable = function (ajax, response, status) {
132
    var $dialogdiv = $(response.selector);
133
    var instanceId = response.instance_id;
134
    $dialogdiv.find('.token-insert-table .token-key').once('token-insert-table', function() {
135
      var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function(e){
136
        e.preventDefault();
137
        var token = $(this).text();
138
        Drupal.wysiwyg.plugins.token_insert_wysiwyg.insertIntoEditor(token, instanceId);
139
      });
140
      $(this).html(newThis);
141
    });
142
    var btns = {};
143

    
144
    btns[Drupal.t('Cancel')] = function () {
145
      $dialogdiv.dialog('close');
146
    };
147

    
148
    $dialogdiv.dialog({
149
      modal: false,
150
      autoOpen: false,
151
      closeOnEscape: true,
152
      resizable: false,
153
      draggable: true,
154
      autoresize: true,
155
      namespace: 'jquery_ui_dialog_default_ns',
156
      dialogClass: 'jquery_ui_dialog-dialog',
157
      title: Drupal.t('Insert token'),
158
      buttons: btns,
159
      width: 700
160
    });
161
    $dialogdiv.css({maxHeight: 350});
162
    dialogs[instanceId] = $dialogdiv;
163
    $dialogdiv.dialog("open");
164
  };
165
})(jQuery, Drupal);