Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / js / modal.js @ e4c061ad

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
          // Stop autocomplete from submitting.
262
          if (Drupal.autocompleteSubmit && !Drupal.autocompleteSubmit()) {
263
            return false;
264
          }
265
          // An empty event means we were triggered via .click() and
266
          // in jquery 1.4 this won't trigger a submit.
267
          if (event.bubbles == undefined) {
268
            $(this.form).trigger('submit');
269
            return false;
270
          }
271
        });
272
      });
273

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

    
284
  // The following are implementations of AJAX responder commands.
285

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

    
299
    // Attach behaviors within a modal dialog.
300
    var settings = response.settings || ajax.settings || Drupal.settings;
301
    Drupal.attachBehaviors('#modalContent', settings);
302
  }
303

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

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

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

    
345

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

    
365
    if (!speed) {
366
      speed = 'fast';
367
    }
368

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

    
378
    // Add opacity handling for IE.
379
    css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')';
380
    content.hide();
381

    
382
    // If we already have modalContent, remove it.
383
    if ($('#modalBackdrop').length) $('#modalBackdrop').remove();
384
    if ($('#modalContent').length) $('#modalContent').remove();
385

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

    
395
    // Get our dimensions
396

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

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

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

    
417
      var parents = $(target).parents().get();
418
      for (var i = 0; i < parents.length; ++i) {
419
        var position = $(parents[i]).css('position');
420
        if (position == 'absolute' || position == 'fixed') {
421
          return true;
422
        }
423
      }
424

    
425
      if ($(target).is('#modalContent, body') || $(target).filter('*:visible').parents('#modalContent').length) {
426
        // Allow the event only if target is a visible child node
427
        // of #modalContent.
428
        return true;
429
      }
430
      else {
431
        $('#modalContent').focus();
432
      }
433

    
434
      event.preventDefault();
435
    };
436
    $('body').bind( 'focus', modalEventHandler );
437
    $('body').bind( 'keypress', modalEventHandler );
438

    
439
    // Create our content div, get the dimensions, and hide it
440
    var modalContent = $('#modalContent').css('top','-1000px');
441
    var mdcTop = wt + ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
442
    var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
443
    $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show();
444
    modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed);
445

    
446
    // Bind a click for closing the modalContent
447
    modalContentClose = function(){close(); return false;};
448
    $('.close').bind('click', modalContentClose);
449

    
450
    // Bind a keypress on escape for closing the modalContent
451
    modalEventEscapeCloseHandler = function(event) {
452
      if (event.keyCode == 27) {
453
        close();
454
        return false;
455
      }
456
    };
457

    
458
    $(document).bind('keydown', modalEventEscapeCloseHandler);
459

    
460
    // Close the open modal content and backdrop
461
    function close() {
462
      // Unbind the events
463
      $(window).unbind('resize',  modalContentResize);
464
      $('body').unbind( 'focus', modalEventHandler);
465
      $('body').unbind( 'keypress', modalEventHandler );
466
      $('.close').unbind('click', modalContentClose);
467
      $('body').unbind('keypress', modalEventEscapeCloseHandler);
468
      $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
469

    
470
      // Set our animation parameters and use them
471
      if ( animation == 'fadeIn' ) animation = 'fadeOut';
472
      if ( animation == 'slideDown' ) animation = 'slideUp';
473
      if ( animation == 'show' ) animation = 'hide';
474

    
475
      // Close the content
476
      modalContent.hide()[animation](speed);
477

    
478
      // Remove the content
479
      $('#modalContent').remove();
480
      $('#modalBackdrop').remove();
481
    };
482

    
483
    // Move and resize the modalBackdrop and modalContent on resize of the window
484
     modalContentResize = function(){
485

    
486
      // position code lifted from http://www.quirksmode.org/viewport/compatibility.html
487
      if (self.pageYOffset) { // all except Explorer
488
      var wt = self.pageYOffset;
489
      } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
490
        var wt = document.documentElement.scrollTop;
491
      } else if (document.body) { // all other Explorers
492
        var wt = document.body.scrollTop;
493
      }
494

    
495
      // Get our heights
496
      var docHeight = $(document).height();
497
      var docWidth = $(document).width();
498
      var winHeight = $(window).height();
499
      var winWidth = $(window).width();
500
      if( docHeight < winHeight ) docHeight = winHeight;
501

    
502
      // Get where we should move content to
503
      var modalContent = $('#modalContent');
504
      var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2);
505
      var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
506

    
507
      // Apply the changes
508
      $('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show();
509
      modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show();
510
    };
511
    $(window).bind('resize', modalContentResize);
512

    
513
    $('#modalContent').focus();
514
  };
515

    
516
  /**
517
   * unmodalContent
518
   * @param content (The jQuery object to remove)
519
   * @param animation (fadeOut, slideUp, show)
520
   * @param speed (valid animation speeds slow, medium, fast or # in ms)
521
   */
522
  Drupal.CTools.Modal.unmodalContent = function(content, animation, speed)
523
  {
524
    // If our animation isn't set, make it just show/pop
525
    if (!animation) { var animation = 'show'; } else {
526
      // If our animation isn't "fade" then it always is show
527
      if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show';
528
    }
529
    // Set a speed if we dont have one
530
    if ( !speed ) var speed = 'fast';
531

    
532
    // Unbind the events we bound
533
    $(window).unbind('resize', modalContentResize);
534
    $('body').unbind('focus', modalEventHandler);
535
    $('body').unbind('keypress', modalEventHandler);
536
    $('.close').unbind('click', modalContentClose);
537
    $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
538

    
539
    // jQuery magic loop through the instances and run the animations or removal.
540
    content.each(function(){
541
      if ( animation == 'fade' ) {
542
        $('#modalContent').fadeOut(speed, function() {
543
          $('#modalBackdrop').fadeOut(speed, function() {
544
            $(this).remove();
545
          });
546
          $(this).remove();
547
        });
548
      } else {
549
        if ( animation == 'slide' ) {
550
          $('#modalContent').slideUp(speed,function() {
551
            $('#modalBackdrop').slideUp(speed, function() {
552
              $(this).remove();
553
            });
554
            $(this).remove();
555
          });
556
        } else {
557
          $('#modalContent').remove();
558
          $('#modalBackdrop').remove();
559
        }
560
      }
561
    });
562
  };
563

    
564
$(function() {
565
  Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display;
566
  Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss;
567
});
568

    
569
})(jQuery);