Projet

Général

Profil

Paste
Télécharger (18,7 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / ctools / js / modal.js @ 3753f249

1
/**
2
 * @file
3
 *
4
 * Implement a modal form.
5
 *
6
 * @see modal.inc for documentation.
7
 *
8
 * This javascript relies on the CTools ajax responder.
9
 */
10

    
11
(function ($) {
12
  // Make sure our objects are defined.
13
  Drupal.CTools = Drupal.CTools || {};
14
  Drupal.CTools.Modal = Drupal.CTools.Modal || {};
15

    
16
  /**
17
   * Display the modal
18
   *
19
   * @todo -- document the settings.
20
   */
21
  Drupal.CTools.Modal.show = function(choice) {
22
    var opts = {};
23

    
24
    if (choice && typeof choice == 'string' && Drupal.settings[choice]) {
25
      // This notation guarantees we are actually copying it.
26
      $.extend(true, opts, Drupal.settings[choice]);
27
    }
28
    else if (choice) {
29
      $.extend(true, opts, choice);
30
    }
31

    
32
    var defaults = {
33
      modalTheme: 'CToolsModalDialog',
34
      throbberTheme: 'CToolsModalThrobber',
35
      animation: 'show',
36
      animationSpeed: 'fast',
37
      modalSize: {
38
        type: 'scale',
39
        width: .8,
40
        height: .8,
41
        addWidth: 0,
42
        addHeight: 0,
43
        // How much to remove from the inner content to make space for the
44
        // theming.
45
        contentRight: 25,
46
        contentBottom: 45
47
      },
48
      modalOptions: {
49
        opacity: .55,
50
        background: '#fff'
51
      }
52
    };
53

    
54
    var settings = {};
55
    $.extend(true, settings, defaults, Drupal.settings.CToolsModal, opts);
56

    
57
    if (Drupal.CTools.Modal.currentSettings && Drupal.CTools.Modal.currentSettings != settings) {
58
      Drupal.CTools.Modal.modal.remove();
59
      Drupal.CTools.Modal.modal = null;
60
    }
61

    
62
    Drupal.CTools.Modal.currentSettings = settings;
63

    
64
    var resize = function(e) {
65
      // When creating the modal, it actually exists only in a theoretical
66
      // place that is not in the DOM. But once the modal exists, it is in the
67
      // DOM so the context must be set appropriately.
68
      var context = e ? document : Drupal.CTools.Modal.modal;
69

    
70
      if (Drupal.CTools.Modal.currentSettings.modalSize.type == 'scale') {
71
        var width = $(window).width() * Drupal.CTools.Modal.currentSettings.modalSize.width;
72
        var height = $(window).height() * Drupal.CTools.Modal.currentSettings.modalSize.height;
73
      }
74
      else {
75
        var width = Drupal.CTools.Modal.currentSettings.modalSize.width;
76
        var height = Drupal.CTools.Modal.currentSettings.modalSize.height;
77
      }
78

    
79
      // Use the additionol pixels for creating the width and height.
80
      $('div.ctools-modal-content', context).css({
81
        'width': width + Drupal.CTools.Modal.currentSettings.modalSize.addWidth + 'px',
82
        'height': height + Drupal.CTools.Modal.currentSettings.modalSize.addHeight + 'px'
83
      });
84
      $('div.ctools-modal-content .modal-content', context).css({
85
        'width': (width - Drupal.CTools.Modal.currentSettings.modalSize.contentRight) + 'px',
86
        'height': (height - Drupal.CTools.Modal.currentSettings.modalSize.contentBottom) + 'px'
87
      });
88
    }
89

    
90
    if (!Drupal.CTools.Modal.modal) {
91
      Drupal.CTools.Modal.modal = $(Drupal.theme(settings.modalTheme));
92
      if (settings.modalSize.type == 'scale') {
93
        $(window).bind('resize', resize);
94
      }
95
    }
96

    
97
    resize();
98

    
99
    $('span.modal-title', Drupal.CTools.Modal.modal).html(Drupal.CTools.Modal.currentSettings.loadingText);
100
    Drupal.CTools.Modal.modalContent(Drupal.CTools.Modal.modal, settings.modalOptions, settings.animation, settings.animationSpeed);
101
    $('#modalContent .modal-content').html(Drupal.theme(settings.throbberTheme));
102

    
103
    // Position autocomplete results based on the scroll position of the modal.
104
    $('#modalContent .modal-content').delegate('input.form-autocomplete', 'keyup', function() {
105
      $('#autocomplete').css('top', $(this).position().top + $(this).outerHeight() + $(this).offsetParent().filter('#modal-content').scrollTop());
106
    });
107
  };
108

    
109
  /**
110
   * Hide the modal
111
   */
112
  Drupal.CTools.Modal.dismiss = function() {
113
    if (Drupal.CTools.Modal.modal) {
114
      Drupal.CTools.Modal.unmodalContent(Drupal.CTools.Modal.modal);
115
    }
116
  };
117

    
118
  /**
119
   * Provide the HTML to create the modal dialog.
120
   */
121
  Drupal.theme.prototype.CToolsModalDialog = function () {
122
    var html = ''
123
    html += '  <div id="ctools-modal">'
124
    html += '    <div class="ctools-modal-content">' // panels-modal-content
125
    html += '      <div class="modal-header">';
126
    html += '        <a class="close" href="#">';
127
    html +=            Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage;
128
    html += '        </a>';
129
    html += '        <span id="modal-title" class="modal-title">&nbsp;</span>';
130
    html += '      </div>';
131
    html += '      <div id="modal-content" class="modal-content">';
132
    html += '      </div>';
133
    html += '    </div>';
134
    html += '  </div>';
135

    
136
    return html;
137
  }
138

    
139
  /**
140
   * Provide the HTML to create the throbber.
141
   */
142
  Drupal.theme.prototype.CToolsModalThrobber = function () {
143
    var html = '';
144
    html += '  <div id="modal-throbber">';
145
    html += '    <div class="modal-throbber-wrapper">';
146
    html +=        Drupal.CTools.Modal.currentSettings.throbber;
147
    html += '    </div>';
148
    html += '  </div>';
149

    
150
    return html;
151
  };
152

    
153
  /**
154
   * Figure out what settings string to use to display a modal.
155
   */
156
  Drupal.CTools.Modal.getSettings = function (object) {
157
    var match = $(object).attr('class').match(/ctools-modal-(\S+)/);
158
    if (match) {
159
      return match[1];
160
    }
161
  }
162

    
163
  /**
164
   * Click function for modals that can be cached.
165
   */
166
  Drupal.CTools.Modal.clickAjaxCacheLink = function () {
167
    Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
168
    return Drupal.CTools.AJAX.clickAJAXCacheLink.apply(this);
169
  };
170

    
171
  /**
172
   * Handler to prepare the modal for the response
173
   */
174
  Drupal.CTools.Modal.clickAjaxLink = function () {
175
    Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this));
176
    return false;
177
  };
178

    
179
  /**
180
   * Submit responder to do an AJAX submit on all modal forms.
181
   */
182
  Drupal.CTools.Modal.submitAjaxForm = function(e) {
183
    var $form = $(this);
184
    var url = $form.attr('action');
185

    
186
    setTimeout(function() { Drupal.CTools.AJAX.ajaxSubmit($form, url); }, 1);
187
    return false;
188
  }
189

    
190
  /**
191
   * Bind links that will open modals to the appropriate function.
192
   */
193
  Drupal.behaviors.ZZCToolsModal = {
194
    attach: function(context) {
195
      // Bind links
196
      // Note that doing so in this order means that the two classes can be
197
      // used together safely.
198
      /*
199
       * @todo remimplement the warm caching feature
200
       $('a.ctools-use-modal-cache', context).once('ctools-use-modal', function() {
201
         $(this).click(Drupal.CTools.Modal.clickAjaxCacheLink);
202
         Drupal.CTools.AJAX.warmCache.apply(this);
203
       });
204
        */
205

    
206
      $('area.ctools-use-modal, a.ctools-use-modal', context).once('ctools-use-modal', function() {
207
        var $this = $(this);
208
        $this.click(Drupal.CTools.Modal.clickAjaxLink);
209
        // Create a drupal ajax object
210
        var element_settings = {};
211
        if ($this.attr('href')) {
212
          element_settings.url = $this.attr('href');
213
          element_settings.event = 'click';
214
          element_settings.progress = { type: 'throbber' };
215
        }
216
        var base = $this.attr('href');
217
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
218
      });
219

    
220
      // Bind buttons
221
      $('input.ctools-use-modal, button.ctools-use-modal', context).once('ctools-use-modal', function() {
222
        var $this = $(this);
223
        $this.click(Drupal.CTools.Modal.clickAjaxLink);
224
        var button = this;
225
        var element_settings = {};
226

    
227
        // AJAX submits specified in this manner automatically submit to the
228
        // normal form action.
229
        element_settings.url = Drupal.CTools.Modal.findURL(this);
230
        if (element_settings.url == '') {
231
          element_settings.url = $(this).closest('form').attr('action');
232
        }
233
        element_settings.event = 'click';
234
        element_settings.setClick = true;
235

    
236
        var base = $this.attr('id');
237
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
238

    
239
        // Make sure changes to settings are reflected in the URL.
240
        $('.' + $(button).attr('id') + '-url').change(function() {
241
          Drupal.ajax[base].options.url = Drupal.CTools.Modal.findURL(button);
242
        });
243
      });
244

    
245
      // Bind our custom event to the form submit
246
      $('#modal-content form', context).once('ctools-use-modal', function() {
247
        var $this = $(this);
248
        var element_settings = {};
249

    
250
        element_settings.url = $this.attr('action');
251
        element_settings.event = 'submit';
252
        element_settings.progress = { 'type': 'throbber' }
253
        var base = $this.attr('id');
254

    
255
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
256
        Drupal.ajax[base].form = $this;
257

    
258
        $('input[type=submit], button', this).click(function(event) {
259
          Drupal.ajax[base].element = this;
260
          this.form.clk = this;
261
          // An empty event means we were triggered via .click() and
262
          // in jquery 1.4 this won't trigger a submit.
263
          if (event.bubbles == undefined) {
264
            $(this.form).trigger('submit');
265
            return false;
266
          }
267
        });
268
      });
269

    
270
      // Bind a click handler to allow elements with the 'ctools-close-modal'
271
      // class to close the modal.
272
      $('.ctools-close-modal', context).once('ctools-close-modal')
273
        .click(function() {
274
          Drupal.CTools.Modal.dismiss();
275
          return false;
276
        });
277
    }
278
  };
279

    
280
  // The following are implementations of AJAX responder commands.
281

    
282
  /**
283
   * AJAX responder command to place HTML within the modal.
284
   */
285
  Drupal.CTools.Modal.modal_display = function(ajax, response, status) {
286
    if ($('#modalContent').length == 0) {
287
      Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(ajax.element));
288
    }
289
    $('#modal-title').html(response.title);
290
    // Simulate an actual page load by scrolling to the top after adding the
291
    // content. This is helpful for allowing users to see error messages at the
292
    // top of a form, etc.
293
    $('#modal-content').html(response.output).scrollTop(0);
294

    
295
    // Attach behaviors within a modal dialog.
296
    var settings = response.settings || ajax.settings || Drupal.settings;
297
    Drupal.attachBehaviors('#modalContent', settings);
298
  }
299

    
300
  /**
301
   * AJAX responder command to dismiss the modal.
302
   */
303
  Drupal.CTools.Modal.modal_dismiss = function(command) {
304
    Drupal.CTools.Modal.dismiss();
305
    $('link.ctools-temporary-css').remove();
306
  }
307

    
308
  /**
309
   * Display loading
310
   */
311
  //Drupal.CTools.AJAX.commands.modal_loading = function(command) {
312
  Drupal.CTools.Modal.modal_loading = function(command) {
313
    Drupal.CTools.Modal.modal_display({
314
      output: Drupal.theme(Drupal.CTools.Modal.currentSettings.throbberTheme),
315
      title: Drupal.CTools.Modal.currentSettings.loadingText
316
    });
317
  }
318

    
319
  /**
320
   * Find a URL for an AJAX button.
321
   *
322
   * The URL for this gadget will be composed of the values of items by
323
   * taking the ID of this item and adding -url and looking for that
324
   * class. They need to be in the form in order since we will
325
   * concat them all together using '/'.
326
   */
327
  Drupal.CTools.Modal.findURL = function(item) {
328
    var url = '';
329
    var url_class = '.' + $(item).attr('id') + '-url';
330
    $(url_class).each(
331
      function() {
332
        var $this = $(this);
333
        if (url && $this.val()) {
334
          url += '/';
335
        }
336
        url += $this.val();
337
      });
338
    return url;
339
  };
340

    
341

    
342
  /**
343
   * modalContent
344
   * @param content string to display in the content box
345
   * @param css obj of css attributes
346
   * @param animation (fadeIn, slideDown, show)
347
   * @param speed (valid animation speeds slow, medium, fast or # in ms)
348
   */
349
  Drupal.CTools.Modal.modalContent = function(content, css, animation, speed) {
350
    // If our animation isn't set, make it just show/pop
351
    if (!animation) {
352
      animation = 'show';
353
    }
354
    else {
355
      // If our animation isn't "fadeIn" or "slideDown" then it always is show
356
      if (animation != 'fadeIn' && animation != 'slideDown') {
357
        animation = 'show';
358
      }
359
    }
360

    
361
    if (!speed) {
362
      speed = 'fast';
363
    }
364

    
365
    // Build our base attributes and allow them to be overriden
366
    css = jQuery.extend({
367
      position: 'absolute',
368
      left: '0px',
369
      margin: '0px',
370
      background: '#000',
371
      opacity: '.55'
372
    }, css);
373

    
374
    // Add opacity handling for IE.
375
    css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')';
376
    content.hide();
377

    
378
    // if we already ahve a modalContent, remove it
379
    if ( $('#modalBackdrop')) $('#modalBackdrop').remove();
380
    if ( $('#modalContent')) $('#modalContent').remove();
381

    
382
    // position code lifted from http://www.quirksmode.org/viewport/compatibility.html
383
    if (self.pageYOffset) { // all except Explorer
384
    var wt = self.pageYOffset;
385
    } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
386
      var wt = document.documentElement.scrollTop;
387
    } else if (document.body) { // all other Explorers
388
      var wt = document.body.scrollTop;
389
    }
390

    
391
    // Get our dimensions
392

    
393
    // Get the docHeight and (ugly hack) add 50 pixels to make sure we dont have a *visible* border below our div
394
    var docHeight = $(document).height() + 50;
395
    var docWidth = $(document).width();
396
    var winHeight = $(window).height();
397
    var winWidth = $(window).width();
398
    if( docHeight < winHeight ) docHeight = winHeight;
399

    
400
    // Create our divs
401
    $('body').append('<div id="modalBackdrop" style="z-index: 1000; display: none;"></div><div id="modalContent" style="z-index: 1001; position: absolute;">' + $(content).html() + '</div>');
402

    
403
    // Keyboard and focus event handler ensures focus stays on modal elements only
404
    modalEventHandler = function( event ) {
405
      target = null;
406
      if ( event ) { //Mozilla
407
        target = event.target;
408
      } else { //IE
409
        event = window.event;
410
        target = event.srcElement;
411
      }
412

    
413
      var parents = $(target).parents().get();
414
      for (var i = 0; i < parents.length; ++i) {
415
        var position = $(parents[i]).css('position');
416
        if (position == 'absolute' || position == 'fixed') {
417
          return true;
418
        }
419
      }
420
      if( $(target).filter('*:visible').parents('#modalContent').size()) {
421
        // allow the event only if target is a visible child node of #modalContent
422
        return true;
423
      }
424
      if ( $('#modalContent')) $('#modalContent').get(0).focus();
425
      return false;
426
    };
427
    $('body').bind( 'focus', modalEventHandler );
428
    $('body').bind( 'keypress', modalEventHandler );
429

    
430
    // Create our content div, get the dimensions, and hide it
431
    var modalContent = $('#modalContent').css('top','-1000px');
432
    var mdcTop = wt + ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
433
    var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
434
    $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show();
435
    modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed);
436

    
437
    // Bind a click for closing the modalContent
438
    modalContentClose = function(){close(); return false;};
439
    $('.close').bind('click', modalContentClose);
440

    
441
    // Bind a keypress on escape for closing the modalContent
442
    modalEventEscapeCloseHandler = function(event) {
443
      if (event.keyCode == 27) {
444
        close();
445
        return false;
446
      }
447
    };
448

    
449
    $(document).bind('keydown', modalEventEscapeCloseHandler);
450

    
451
    // Close the open modal content and backdrop
452
    function close() {
453
      // Unbind the events
454
      $(window).unbind('resize',  modalContentResize);
455
      $('body').unbind( 'focus', modalEventHandler);
456
      $('body').unbind( 'keypress', modalEventHandler );
457
      $('.close').unbind('click', modalContentClose);
458
      $('body').unbind('keypress', modalEventEscapeCloseHandler);
459
      $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
460

    
461
      // Set our animation parameters and use them
462
      if ( animation == 'fadeIn' ) animation = 'fadeOut';
463
      if ( animation == 'slideDown' ) animation = 'slideUp';
464
      if ( animation == 'show' ) animation = 'hide';
465

    
466
      // Close the content
467
      modalContent.hide()[animation](speed);
468

    
469
      // Remove the content
470
      $('#modalContent').remove();
471
      $('#modalBackdrop').remove();
472
    };
473

    
474
    // Move and resize the modalBackdrop and modalContent on resize of the window
475
     modalContentResize = function(){
476
      // Get our heights
477
      var docHeight = $(document).height();
478
      var docWidth = $(document).width();
479
      var winHeight = $(window).height();
480
      var winWidth = $(window).width();
481
      if( docHeight < winHeight ) docHeight = winHeight;
482

    
483
      // Get where we should move content to
484
      var modalContent = $('#modalContent');
485
      var mdcTop = ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
486
      var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
487

    
488
      // Apply the changes
489
      $('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show();
490
      modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show();
491
    };
492
    $(window).bind('resize', modalContentResize);
493

    
494
    $('#modalContent').focus();
495
  };
496

    
497
  /**
498
   * unmodalContent
499
   * @param content (The jQuery object to remove)
500
   * @param animation (fadeOut, slideUp, show)
501
   * @param speed (valid animation speeds slow, medium, fast or # in ms)
502
   */
503
  Drupal.CTools.Modal.unmodalContent = function(content, animation, speed)
504
  {
505
    // If our animation isn't set, make it just show/pop
506
    if (!animation) { var animation = 'show'; } else {
507
      // If our animation isn't "fade" then it always is show
508
      if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show';
509
    }
510
    // Set a speed if we dont have one
511
    if ( !speed ) var speed = 'fast';
512

    
513
    // Unbind the events we bound
514
    $(window).unbind('resize', modalContentResize);
515
    $('body').unbind('focus', modalEventHandler);
516
    $('body').unbind('keypress', modalEventHandler);
517
    $('.close').unbind('click', modalContentClose);
518
    $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
519

    
520
    // jQuery magic loop through the instances and run the animations or removal.
521
    content.each(function(){
522
      if ( animation == 'fade' ) {
523
        $('#modalContent').fadeOut(speed, function() {
524
          $('#modalBackdrop').fadeOut(speed, function() {
525
            $(this).remove();
526
          });
527
          $(this).remove();
528
        });
529
      } else {
530
        if ( animation == 'slide' ) {
531
          $('#modalContent').slideUp(speed,function() {
532
            $('#modalBackdrop').slideUp(speed, function() {
533
              $(this).remove();
534
            });
535
            $(this).remove();
536
          });
537
        } else {
538
          $('#modalContent').remove();
539
          $('#modalBackdrop').remove();
540
        }
541
      }
542
    });
543
  };
544

    
545
$(function() {
546
  Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display;
547
  Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss;
548
});
549

    
550
})(jQuery);