summaryrefslogtreecommitdiffstats
path: root/js/linkit.dashboard.js
blob: 10265762ff3e8336a9c33b37fe7b0cf0f79e96d8 (plain)
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
/**
 * @file
 * Linkit dashboard functions
 */

(function ($) {

Drupal.behaviors.linkitDashboard = {
  attach: function (context, settings) {
    // Bind the insert link button.
    $('.linkit-insert', context).once('linkit-insert', function() {
      $('.linkit-insert', context).click(function(event) {
        event.preventDefault();
        // Call the insertLink() function.
        Drupal.linkit.getDialogHelper(Drupal.settings.linkit.currentInstance.helper).insertLink(Drupal.linkit.getLink());

        // Close the dialog.
        Drupal.linkit.modalClose();
      });
    });

    // Bind the close link.
    $('#linkit-cancel', context).once('linkit-cancel', function() {
      $('#linkit-cancel', context).bind('click', Drupal.linkit.modalClose);
    });

    // Run the validation if the path field is populated directly.
    $('#edit-linkit-path', context).bind('keyup paste input propertychange', function(){
      Drupal.linkit.requiredFieldsValidation();
    });

    $(".ui-dialog-titlebar").show();

    // Run required field validation.
    Drupal.linkit.requiredFieldsValidation();

    if (!Drupal.settings.linkit.currentInstance.suppressProfileChanger) {
      // Make the profile changer
      Drupal.linkit.profileChanger(context);
    }
    if (Drupal.settings.linkit.IMCEurl && !$('#linkit-imce', context).length) {
      var $imceButton = $('<input />')
        .attr({type: 'button', id: 'linkit-imce', name: 'linkit-imce'})
        .addClass('form-submit')
        .val(Drupal.t('Open file browser'))
        .insertAfter($('.form-item-linkit-search', context))
        .click(function(e) {
          e.preventDefault();
          Drupal.linkit.openFileBrowser();
        });
    }
  }
};

/**
 * Check for mandatory fields in the form and disable for submissions
 * if any of the fields are empty.
 */
Drupal.linkit.requiredFieldsValidation = function() {
  var allowed = true;
  $('#linkit-modal .required').each(function() {
    if (!$(this).val()) {
      allowed = false;
      return false;
    }
  });
  if (allowed) {
    $('#linkit-modal .linkit-insert')
      .removeAttr('disabled')
      .removeClass('form-button-disabled');
  }
  else {
    $('#linkit-modal .linkit-insert')
      .attr('disabled', 'disabled')
      .addClass('form-button-disabled');
  }
};

/**
 * Open the IMCE file browser
 */
Drupal.linkit.openFileBrowser = function () {
  window.open(decodeURIComponent(Drupal.settings.linkit.IMCEurl), '', 'width=760,height=560,resizable=1');
};

/**
 * When a file is inserted through IMCE, this function is called.
 * See IMCE api for details.
 *
 * @param file
 *   The file object that was selected inside IMCE
 * @param win
 *   The IMCE window object
 */
Drupal.linkit.IMCECallback = function(file, win) {
  Drupal.linkit.populateFields({
     path: win.imce.decode(Drupal.settings.basePath +
         Drupal.settings.linkit.publicFilesDirectory + '/' + file.relpath)
  });
  win.close();
};

/**
 * Populate fields on the dashboard.
 *
 * @param link
 *   An object with the following properties (all are optional):
 *   - path: The anchor's href.
 *   - attributes: An object with additional attributes for the anchor element.
 */
Drupal.linkit.populateFields = function(link) {
  link = link || {};
  link.attributes = link.attributes || {};

  $('#linkit-modal .linkit-path-element').val(link.path);

  $.each(link.attributes, function(name, value) {
    $('#linkit-modal .linkit-attributes .linkit-attribute-' + name).val(value);
  });

  // Run required field validation.
  Drupal.linkit.requiredFieldsValidation();
};

/**
 * Retrieve a link object by extracting values from the form.
 *
 * @return
 *   The link object.
 */
 Drupal.linkit.getLink = function() {
    var link = {
      path: $('#linkit-modal .linkit-path-element ').val(),
      attributes: {}
    };
    $.each(Drupal.linkit.additionalAttributes(), function(f, name) {
     link.attributes[name] = $('#linkit-modal .linkit-attributes .linkit-attribute-' + name).val();
    });
  return link;
};

/**
 * Retrieve a list of the currently available additional attributes in the
 * dashboard. The attribute "href" is excluded.
 *
 * @return
 *   An array with the names of the attributes.
 */
Drupal.linkit.additionalAttributes = function() {
  var attributes = [];
  $('#linkit-modal .linkit-attributes .linkit-attribute').each(function() {
    // Remove the 'linkit_' prefix.
    attributes.push($(this).attr('name').substr(7));
  });
  return attributes;
};

Drupal.linkit.profileChanger = function(context) {
  $('#linkit-profile-changer > div.form-item', context).once('linkit-change-profile', function() {
    var target = $(this);
    var toggler = $('<div id="linkit-profile-changer-toggler"></div>')
    .html(Drupal.t('Change profile'))
    .click(function() {
      target.slideToggle();
    });
    $(this).after(toggler);
  });

  $('#linkit-profile-changer .form-radio', context).each(function() {
    var id = $(this).attr('id');
    var profile = $(this).val();
    if (typeof Drupal.ajax[id] != 'undefined') {
      // @TODO: Jquery 1.5 accept success setting to be an array of functions.
      // But we have to wait for jquery to get updated in Drupal core.
      // In the meantime we have to override it.
      Drupal.ajax[id].options.success = function (response, status) {
        if (typeof response == 'string') {
          response = $.parseJSON(response);
        }

        // Update the autocomplete url.
        Drupal.settings.linkit.currentInstance.autocompletePathParsed = Drupal.settings.linkit.autocompletePath.replace('___profile___', profile);

        // Call the ajax success method.
        Drupal.ajax[id].success(response, status);
        $('#linkit-profile-changer > div.form-item').slideToggle();
      };
    }
  });
};

Drupal.behaviors.linkitSearch = {
  attach: function(context, settings) {

    $('.linkit-search-element').once('linkit-search', function() {
      // Create a synonym for this to reduce code confusion.
      var searchElement = $('.linkit-search-element');
      var callbacks = {
        constructURL: function(path, search) {
          return path + encodeURIComponent(search);
        },

        insertSuggestionList: function($results, $input) {
          var top = $input.position().top + $input.outerHeight() - 5;

          $results.width($input.outerWidth()).css({
            position: 'absolute',
            // High value because of other overlays like
            // wysiwyg fullscreen (TinyMCE) mode.
            zIndex: 211000,
            maxHeight: $(window).height() - (top + 20)
          })
          .hide()
          .insertAfter($input);
        },

        select: function(result) {
          if (typeof result == 'undefined') {
            return false;
          }
          // Only change the link text if it is empty.
          if (typeof result.disabled != 'undefined' && result.disabled) {
            return false;
          }

          Drupal.linkit.populateFields({
            path: result.path
          });

          // Store the result title (Used when no selection is made by the user).
          Drupal.settings.linkit.currentInstance.linkContent = result.title;

          $('.linkit-path-element', context).focus();
        }
      };

      searchElement.betterAutocomplete('init', Drupal.settings.linkit.currentInstance.autocompletePathParsed, Drupal.settings.linkit.currentInstance.autocomplete, callbacks);
    });
  }
};
  
})(jQuery);