Projet

Général

Profil

Paste
Télécharger (13,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / sweaver / sweaver_plugin.js @ 87dbc3bf

1

    
2
(function ($) {
3

    
4
Drupal.Sweaver = Drupal.Sweaver || {};
5
Drupal.Sweaver.invokes = Drupal.Sweaver.invokes || {};
6

    
7
Drupal.Sweaver.messageTimer = null;
8
Drupal.Sweaver.changed = false;
9
Drupal.Sweaver.popup = '';
10

    
11
Drupal.Sweaver.writeCss = function(context) {
12

    
13
  var fullCss = '';
14
  $.each(Drupal.Sweaver.invokes, function(func) {
15
    var css = this.execute();
16
    if (css != '') {
17
      fullCss += css;
18
    }
19
  });
20
  $style = $('head style[title="sweaver"]');
21
  $style.remove();
22
  $('head').append('<style type="text/css" title="sweaver">' + fullCss + '</style>');
23
  $('[name=css-rendered]').val(fullCss);
24

    
25
  Drupal.Sweaver.changed = true;
26
};
27

    
28
$(document).ready(function() {
29

    
30
  // Avoid overlap with the localization client.
31
  if ($('#l10n-client').length > 0) {
32
    $('#sweaver').css({'bottom': $('#l10n-client .labels').height()});
33
  }
34

    
35
  // Gather open/close state and tab information
36
  Drupal.Sweaver.activeTab = Drupal.Sweaver.cookie('sweaver_active_tab') ? Drupal.Sweaver.cookie('sweaver_active_tab') : $('#sweaver-tabs .tab:first').attr('id');
37
  $('#' + Drupal.Sweaver.activeTab).addClass('active-tab');
38
  Drupal.Sweaver.cookie('sweaver_active_tab', Drupal.Sweaver.activeTab);
39
  Drupal.Sweaver.open = Drupal.Sweaver.cookie('sweaver_open') ? Drupal.Sweaver.cookie('sweaver_open') : 'true';
40
  Drupal.Sweaver.cookie('sweaver_open', Drupal.Sweaver.open);
41

    
42
  // Add sweaver class for extra margin at bottom.
43
  if (Drupal.Sweaver.open != 'false') {
44
    $('body').addClass('sweaver');
45
  }
46
  
47
  // Open/close the Sweaver bar.
48
  $('#sweaver-tabs .close a').click(function(){
49
    Drupal.Sweaver.toggleBar($(this).parent());
50
  });
51

    
52
  // Toggle the horizontal tabs.
53
  Drupal.Sweaver.container = Drupal.Sweaver.activeTab.substr(4, Drupal.Sweaver.activeTab.length - 4);
54
  $('#sweaver-tabs .tab a').click(function(){
55
    Drupal.Sweaver.toggleTabs($(this).parent());
56
  });
57

    
58
  // Print messages if any
59
  if ($('[name=sweaver-editor-messages]').val() != '') {
60
    Drupal.Sweaver.setMessage($('[name=sweaver-editor-messages]').val(), 5000);
61
  }
62

    
63
  // toggle vertical tabs
64
  $('#sweaver .vertical-tabs a').click(function(){
65
    if (!$(this).hasClass('active')) {
66
      // handle active classes.
67
      $('#sweaver #' + Drupal.Sweaver.container + ' .vertical-tabs .active').removeClass('active');
68
      $(this).addClass('active');
69
      var id = $(this).parent().attr('id').replace('tab-', '');
70
      $('#sweaver #' + Drupal.Sweaver.container + ' .vertical-content #container-' + id).siblings().hide();
71
      $('#sweaver #' + Drupal.Sweaver.container + ' .vertical-content #container-' + id).show();
72
    }
73
    return false;
74
  });
75

    
76
});
77

    
78
/**
79
 * Separate toggle bar function.
80
 */
81
Drupal.Sweaver.toggleBar = function (tab) {
82
  if (Drupal.Sweaver.open == 'false') {
83
    $('#sweaver-middle').css('height', 'auto');
84
    tab.removeClass('active-tab');
85
    $('#sweaver-tabs .close').removeClass('active-tab');
86
    $('#' + Drupal.Sweaver.activeTab).addClass('active-tab');
87
    Drupal.Sweaver.open = 'true';
88
  }
89
  else {
90
    $('#sweaver-middle').css("height", 0);
91
    $('#follow-link').hide();
92
    Drupal.Sweaver.activeTab =  $('#sweaver-tabs .active-tab').attr('id');
93
    tab.addClass('active-tab');
94
    $('#sweaver-tabs .close').addClass('active-tab');
95
    Drupal.Sweaver.open = 'false';
96
  }
97
  // Hide the extra margin at the bottom of the screen.
98
  $('body').toggleClass('sweaver');
99

    
100
  Drupal.Sweaver.toggleClicked();
101
  Drupal.Sweaver.cookie('sweaver_open', Drupal.Sweaver.open);
102
}
103

    
104
/**
105
 * Separate toggle tabs function.
106
 */
107
Drupal.Sweaver.toggleTabs = function (tab) {
108
  // Get the container that has to be shown.
109
  var container = tab.attr('id').replace('tab-', '');
110
  if (container != Drupal.Sweaver.container) {
111
    //Drupal.Sweaver.toggleBar(tab);
112
    if (Drupal.Sweaver.open == 'false') {
113
      $('#sweaver-middle').css("height", 'auto');
114
      Drupal.Sweaver.open = 'true';
115
      $('body').addClass('sweaver');
116
    }
117
    tab.siblings().removeClass('active-tab');
118
    tab.toggleClass('active-tab');
119
    $('#'+ container + ' > div').show();
120
    $('#'+ Drupal.Sweaver.container + ' > div').hide();
121
    Drupal.Sweaver.container = container;
122
  }
123
  else {
124
    Drupal.Sweaver.toggleBar(tab);
125
  }
126
  Drupal.Sweaver.activeTab =  tab.attr('id');
127
  Drupal.Sweaver.cookie('sweaver_open', Drupal.Sweaver.open);
128
  Drupal.Sweaver.cookie('sweaver_active_tab', Drupal.Sweaver.activeTab);
129
  Drupal.Sweaver.hidePopup();
130
  Drupal.Sweaver.toggleClicked();
131
};
132

    
133
/**
134
 * Separate switch tab function. Takes the tab as arguments and the ID's
135
 * of the containers will be derived from the tabs.
136
 */
137
Drupal.Sweaver.toggleClicked = function () {
138
  if (Drupal.Sweaver.open == 'true' && Drupal.Sweaver.activeTab == 'tab-sweaver_plugin_editor') {
139
    // Show the outline on all 'clicked' classes.
140
    $('.sweaver-clicked-temp').removeClass('sweaver-clicked-temp').addClass('sweaver-clicked');
141
  }
142
  else {
143
    // Hide the outline on all 'clicked' elements
144
    $('.sweaver-clicked').removeClass('sweaver-clicked').addClass('sweaver-clicked-temp');
145
  }
146
}
147

    
148
/**
149
 * Separate switch tab function. Takes the tab as arguments and the ID's
150
 * of the containers will be derived from the tabs.
151
 */
152
Drupal.Sweaver.switchTab = function (remove_tab, show_tab) {
153
  var container_remove = remove_tab.replace('tab-', '');
154
  var container_show = show_tab.replace('tab-', '');
155

    
156
  $('#'+ remove_tab).removeClass('active-tab');
157
  $('#'+ show_tab).toggleClass('active-tab');
158
  $('#'+ container_remove + ' > div').hide();
159
  $('#'+ container_show + ' > div').show();
160
  Drupal.Sweaver.container = container_show;
161

    
162
  Drupal.Sweaver.activeTab = show_tab;
163
  Drupal.Sweaver.cookie('sweaver_active_tab', show_tab);
164
  Drupal.Sweaver.hidePopup();
165
}
166

    
167
/**
168
 * Display Sweaver messages.
169
 */
170
Drupal.Sweaver.setMessage = function(message, timeout) {
171
  Drupal.Sweaver.setMessagePosition();
172
  $('#sweaver-messages .message').html(message);
173
  $('#sweaver-messages').fadeIn('fast');
174
  Drupal.Sweaver.messageTimer = window.setTimeout(function() {$('#sweaver-messages').fadeOut('normal');}, timeout);
175

    
176
  // Bind close messages.
177
  $('#sweaver-messages .close').click(function(){
178
    $('#sweaver-messages').hide();
179
    clearTimeout(Drupal.Sweaver.messageTimer);
180
  });
181

    
182
  // Bind resize on window.
183
  $(window).resize(function(event){
184
    Drupal.Sweaver.setMessagePosition();
185
  });
186
}
187

    
188
/**
189
 * Set the position of the message.
190
 */
191
Drupal.Sweaver.setMessagePosition = function(){
192
  messageTop = $(window).height() - $('#sweaver').outerHeight() - $('#sweaver-messages').outerHeight() - 7;
193
  $('#sweaver-messages').css({'top' : messageTop});
194
}
195

    
196
/**
197
 * Display a fullscreen popup.
198
 */
199
Drupal.Sweaver.showPopup = function(message, width, height) {
200
  // Close the previous popup - if any.
201
  if (Drupal.Sweaver.popup != '') {
202
    $(Drupal.Sweaver.popup).hide();
203
  }
204

    
205
  // Create popup.
206
  popup = $('#sweaver-popup');
207
  $(message).show();
208
  Drupal.Sweaver.popup = message;
209
  Drupal.Sweaver.setPopupSize(popup, width, height);
210
  popup.fadeIn('fast');
211

    
212
  // Bind close button action.
213
  $('.close', popup).click(function(){
214
    $(message).hide();
215
    Drupal.Sweaver.hidePopup();
216
  });
217

    
218
  // Bind resize on window if no width or height was given
219
  // and the popup is full screen.
220
  if (!width && !height) {
221
    $(window).bind('resize.Drupal.Sweaver', function(event){
222
      Drupal.Sweaver.setPopupSize(popup);
223
    });
224
  }
225
}
226

    
227
/**
228
 * Set the popup width and height.
229
 */
230
Drupal.Sweaver.setPopupSize = function(popup, width, height) {
231
  popupBorder = 7;
232
  // Reset overflow in case we don't need a scrollbar.
233
  $('.content', popup).css({'overflow-y' : 'hidden'});
234

    
235
  // Calculate width and height.
236
  var popupWidth = width ? width : $(window).width() - (popupBorder * 2) - parseInt(popup.css('padding-left')) - parseInt(popup.css('padding-right'));
237
  var popupHeight = height ? height : $(window).height() - $('#sweaver').outerHeight() - (popupBorder * 2) - parseInt(popup.css('padding-top')) - parseInt(popup.css('padding-bottom'));
238
  $('.content', popup).css({'height' : popupHeight, 'width' : popupWidth});
239

    
240
  // Center the popup in case a width or height was given.
241
  var popupLeft = width ? (($(window).width() - parseInt(popupWidth)) / 2) : popupBorder;
242
  var popupTop = height ? (($(window).height() - parseInt(popupHeight)) / 2) : popupBorder;
243
  popup.css({'left' : popupLeft + 'px', 'top' : popupTop + 'px'});
244

    
245
  // Add scrollbar if in fullscreen mode.
246
  if (!height) {
247
    $('.content', popup).css({'overflow-y' : 'scroll'});
248
  }
249
}
250

    
251
/**
252
 * Hide a popup.
253
 */
254
Drupal.Sweaver.hidePopup = function() {
255
  $('#sweaver-popup').hide();
256
  $(window).unbind('resize.Drupal.Sweaver');
257
}
258

    
259
/**
260
 * Set behaviors on link which will open the popup.
261
 */
262
Drupal.behaviors.sweaverOpenPopup = {
263
  attach: function (context) {
264
  $('#sweaver .popup-link a').click(function() {
265
      var wrapper = $(this).attr('id').replace('link', 'data');
266

    
267
      popup = $('#sweaver-popup');
268
      if (popup.is(':visible') && $(this).hasClass('open-tab')) {
269
        Drupal.Sweaver.hidePopup();
270
        $(this).removeClass('open-tab');
271
      }
272
      else {
273
        $('#sweaver .open-style-actions').removeClass('open-style-actions');
274
        $('#sweaver .open-tab').removeClass('open-tab');
275
        $(this).addClass('open-tab');
276
        Drupal.Sweaver.showPopup($('#'+ wrapper));
277
      }
278
      return false;
279
    });
280

    
281
    $('#sweaver .form-submit').click(function() {
282
      Drupal.Sweaver.hidePopup();
283
    });
284

    
285
    // Open a popup when clicking on an open/save/delete/publish link.
286
    $('#sweaver .style-actions-link a').click(function() {
287
      var wrapper = $(this).attr('id').replace('link', 'data');
288

    
289
      popup = $('#sweaver-popup');
290
      if (popup.is(':visible') && $(this).hasClass('open-style-actions')) {
291
        Drupal.Sweaver.hidePopup();
292
        $(this).removeClass('open-style-actions');
293
      }
294
      else {
295
        $('#sweaver .open-style-actions').removeClass('open-style-actions');
296
        $('#sweaver .open-tab').removeClass('open-tab');
297
        $(this).addClass('open-style-actions');
298
        Drupal.Sweaver.hidePopup();
299
        Drupal.Sweaver.showPopup($('#'+ wrapper), '400px', '200px');
300
      }
301
      return false;
302
    });
303
  }
304
};
305

    
306

    
307
/**
308
 * Cookie plugin
309
 *
310
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
311
 * Dual licensed under the MIT and GPL licenses:
312
 * http://www.opensource.org/licenses/mit-license.php
313
 * http://www.gnu.org/licenses/gpl.html
314
 *
315
 */
316

    
317
/**
318
 * Create a cookie with the given name and value and other optional parameters.
319
 *
320
 * @example $.cookie('the_cookie', 'the_value');
321
 * @desc Set the value of a cookie.
322
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
323
 * @desc Create a cookie with all available options.
324
 * @example $.cookie('the_cookie', 'the_value');
325
 * @desc Create a session cookie.
326
 * @example $.cookie('the_cookie', null);
327
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
328
 *       used when the cookie was set.
329
 *
330
 * @param String name The name of the cookie.
331
 * @param String value The value of the cookie.
332
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
333
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
334
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
335
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
336
 *                             when the the browser exits.
337
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
338
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
339
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
340
 *                        require a secure protocol (like HTTPS).
341
 * @type undefined
342
 *
343
 * @name $.cookie
344
 * @cat Plugins/Cookie
345
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
346
 */
347

    
348
/**
349
 * Get the value of a cookie with the given name.
350
 *
351
 * @example $.cookie('the_cookie');
352
 * @desc Get the value of a cookie.
353
 *
354
 * @param String name The name of the cookie.
355
 * @return The value of the cookie.
356
 * @type String
357
 *
358
 * @name $.cookie
359
 * @cat Plugins/Cookie
360
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
361
 */
362
Drupal.Sweaver.cookie = function(name, value, options) {
363
  if (typeof value != 'undefined') { // name and value given, set cookie
364
    options = options || {};
365
    if (value === null) {
366
      value = '';
367
      options.expires = -1;
368
    }
369
    var expires = '';
370
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
371
      var date;
372
      if (typeof options.expires == 'number') {
373
        date = new Date();
374
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
375
      }
376
      else {
377
        date = options.expires;
378
      }
379
      expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
380
    }
381
    // CAUTION: Needed to parenthesize options.path and options.domain
382
    // in the following expressions, otherwise they evaluate to undefined
383
    // in the packed version for some reason...
384
    var path = options.path ? '; path=' + (options.path) : '; path=/';
385
    var domain = options.domain ? '; domain=' + (options.domain) : '';
386
    var secure = options.secure ? '; secure' : '';
387
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
388
  }
389
  else { // only name given, get cookie
390
    var cookieValue = null;
391
    if (document.cookie && document.cookie != '') {
392
      var cookies = document.cookie.split(';');
393
      for (var i = 0; i < cookies.length; i++) {
394
        var cookie = jQuery.trim(cookies[i]);
395
        // Does this cookie string begin with the name we want?
396
        if (cookie.substring(0, name.length + 1) == (name + '=')) {
397
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
398
          break;
399
        }
400
      }
401
    }
402
    return cookieValue;
403
  }
404
};
405

    
406
})(jQuery);