Projet

Général

Profil

Paste
Télécharger (25,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / js / modal.js @ 6e3ce7c2

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
      modalClass: 'default'
53
    };
54

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

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

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

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

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

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

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

    
98
    resize();
99

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

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

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

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

    
137
    return html;
138
  };
139

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

    
151
    return html;
152
  };
153

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
259
        $('input[type=submit], button', this).click(function(event) {
260
          Drupal.ajax[base].element = this;
261
          this.form.clk = this;
262
          // Stop autocomplete from submitting.
263
          if (Drupal.autocompleteSubmit && !Drupal.autocompleteSubmit()) {
264
            return false;
265
          }
266
          // An empty event means we were triggered via .click() and
267
          // in jquery 1.4 this won't trigger a submit.
268
          // We also have to check jQuery version to prevent
269
          // IE8 + jQuery 1.4.4 to break on other events
270
          // bound to the submit button.
271
          if (jQuery.fn.jquery.substr(0, 3) === '1.4' && typeof event.bubbles === "undefined") {
272
            $(this.form).trigger('submit');
273
            return false;
274
          }
275
        });
276
      });
277

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

    
288
  // The following are implementations of AJAX responder commands.
289

    
290
  /**
291
   * AJAX responder command to place HTML within the modal.
292
   */
293
  Drupal.CTools.Modal.modal_display = function(ajax, response, status) {
294
    var settings = response.settings || ajax.settings || Drupal.settings;
295
    // If the modal does not exist yet, create it.
296
    if ($('#modalContent').length == 0) {
297
      Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(ajax.element));
298
    }
299
    // If the modal exists run detachBehaviors before removing existing content.
300
    else {
301
      Drupal.detachBehaviors($('#modalContent'), settings, 'unload');
302
    }
303
    $('#modal-title').html(response.title);
304
    // Simulate an actual page load by scrolling to the top after adding the
305
    // content. This is helpful for allowing users to see error messages at the
306
    // top of a form, etc.
307
    $('#modal-content').html(response.output).scrollTop(0);
308
    $(document).trigger('CToolsAttachBehaviors', $('#modalContent'));
309

    
310
    // Attach behaviors within a modal dialog.
311
    Drupal.attachBehaviors($('#modalContent'), settings);
312

    
313
    if ($('#modal-content').hasClass('ctools-modal-loading')) {
314
      $('#modal-content').removeClass('ctools-modal-loading');
315
    }
316
    else {
317
      // If the modal was already shown, and we are simply replacing its
318
      // content, then focus on the first focusable element in the modal.
319
      // (When first showing the modal, focus will be placed on the close
320
      // button by the show() function called above.)
321
      $('#modal-content :focusable:first').focus();
322
    }
323
  };
324

    
325
  /**
326
   * AJAX responder command to dismiss the modal.
327
   */
328
  Drupal.CTools.Modal.modal_dismiss = function(command) {
329
    Drupal.CTools.Modal.dismiss();
330
    $('link.ctools-temporary-css').remove();
331
  };
332

    
333
  /**
334
   * Display loading
335
   */
336
  //Drupal.CTools.AJAX.commands.modal_loading = function(command) {
337
  Drupal.CTools.Modal.modal_loading = function(command) {
338
    Drupal.CTools.Modal.modal_display({
339
      output: Drupal.theme(Drupal.CTools.Modal.currentSettings.throbberTheme),
340
      title: Drupal.CTools.Modal.currentSettings.loadingText
341
    });
342
  };
343

    
344
  /**
345
   * Find a URL for an AJAX button.
346
   *
347
   * The URL for this gadget will be composed of the values of items by
348
   * taking the ID of this item and adding -url and looking for that
349
   * class. They need to be in the form in order since we will
350
   * concat them all together using '/'.
351
   */
352
  Drupal.CTools.Modal.findURL = function(item) {
353
    var url = '';
354
    var url_class = '.' + $(item).attr('id') + '-url';
355
    $(url_class).each(
356
      function() {
357
        var $this = $(this);
358
        if (url && $this.val()) {
359
          url += '/';
360
        }
361
        url += $this.val();
362
      });
363
    return url;
364
  };
365

    
366

    
367
  /**
368
   * modalContent
369
   * @param content string to display in the content box
370
   * @param css obj of css attributes
371
   * @param animation (fadeIn, slideDown, show)
372
   * @param speed (valid animation speeds slow, medium, fast or # in ms)
373
   * @param modalClass class added to div#modalContent
374
   */
375
  Drupal.CTools.Modal.modalContent = function(content, css, animation, speed, modalClass) {
376
    // If our animation isn't set, make it just show/pop
377
    if (!animation) {
378
      animation = 'show';
379
    }
380
    else {
381
      // If our animation isn't "fadeIn" or "slideDown" then it always is show
382
      if (animation != 'fadeIn' && animation != 'slideDown') {
383
        animation = 'show';
384
      }
385
    }
386

    
387
    if (!speed && 0 !== speed) {
388
      speed = 'fast';
389
    }
390

    
391
    // Build our base attributes and allow them to be overriden
392
    css = jQuery.extend({
393
      position: 'absolute',
394
      left: '0px',
395
      margin: '0px',
396
      background: '#000',
397
      opacity: '.55'
398
    }, css);
399

    
400
    // Add opacity handling for IE.
401
    css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')';
402
    content.hide();
403

    
404
    // If we already have modalContent, remove it.
405
    if ($('#modalBackdrop').length) $('#modalBackdrop').remove();
406
    if ($('#modalContent').length) $('#modalContent').remove();
407

    
408
    // position code lifted from http://www.quirksmode.org/viewport/compatibility.html
409
    if (self.pageYOffset) { // all except Explorer
410
    var wt = self.pageYOffset;
411
    } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
412
      var wt = document.documentElement.scrollTop;
413
    } else if (document.body) { // all other Explorers
414
      var wt = document.body.scrollTop;
415
    }
416

    
417
    // Get our dimensions
418

    
419
    // Get the docHeight and (ugly hack) add 50 pixels to make sure we dont have a *visible* border below our div
420
    var docHeight = $(document).height() + 50;
421
    var docWidth = $(document).width();
422
    var winHeight = $(window).height();
423
    var winWidth = $(window).width();
424
    if( docHeight < winHeight ) docHeight = winHeight;
425

    
426
    // Create our divs
427
    $('body').append('<div id="modalBackdrop" class="backdrop-' + modalClass + '" style="z-index: 1000; display: none;"></div><div id="modalContent" class="modal-' + modalClass + '" style="z-index: 1001; position: absolute;">' + $(content).html() + '</div>');
428

    
429
    // Get a list of the tabbable elements in the modal content.
430
    var getTabbableElements = function () {
431
      var tabbableElements = $('#modalContent :tabbable'),
432
          radioButtons = tabbableElements.filter('input[type="radio"]');
433

    
434
      // The list of tabbable elements from jQuery is *almost* right. The
435
      // exception is with groups of radio buttons. The list from jQuery will
436
      // include all radio buttons, when in fact, only the selected radio button
437
      // is tabbable, and if no radio buttons in a group are selected, then only
438
      // the first is tabbable.
439
      if (radioButtons.length > 0) {
440
        // First, build up an index of which groups have an item selected or not.
441
        var anySelected = {};
442
        radioButtons.each(function () {
443
          var name = this.name;
444

    
445
          if (typeof anySelected[name] === 'undefined') {
446
            anySelected[name] = radioButtons.filter('input[name="' + name + '"]:checked').length !== 0;
447
          }
448
        });
449

    
450
        // Next filter out the radio buttons that aren't really tabbable.
451
        var found = {};
452
        tabbableElements = tabbableElements.filter(function () {
453
          var keep = true;
454

    
455
          if (this.type == 'radio') {
456
            if (anySelected[this.name]) {
457
              // Only keep the selected one.
458
              keep = this.checked;
459
            }
460
            else {
461
              // Only keep the first one.
462
              if (found[this.name]) {
463
                keep = false;
464
              }
465
              found[this.name] = true;
466
            }
467
          }
468

    
469
          return keep;
470
        });
471
      }
472

    
473
      return tabbableElements.get();
474
    };
475

    
476
    // Keyboard and focus event handler ensures only modal elements gain focus.
477
    modalEventHandler = function( event ) {
478
      target = null;
479
      if ( event ) { //Mozilla
480
        target = event.target;
481
      } else { //IE
482
        event = window.event;
483
        target = event.srcElement;
484
      }
485

    
486
      var parents = $(target).parents().get();
487
      for (var i = 0; i < parents.length; ++i) {
488
        var position = $(parents[i]).css('position');
489
        if (position == 'absolute' || position == 'fixed') {
490
          return true;
491
        }
492
      }
493

    
494
      if ($(target).is('#modalContent, body') || $(target).filter('*:visible').parents('#modalContent').length) {
495
        // Allow the event only if target is a visible child node
496
        // of #modalContent.
497
        return true;
498
      }
499
      else {
500
        getTabbableElements()[0].focus();
501
      }
502

    
503
      event.preventDefault();
504
    };
505
    $('body').bind( 'focus', modalEventHandler );
506
    $('body').bind( 'keypress', modalEventHandler );
507

    
508
    // Keypress handler Ensures you can only TAB to elements within the modal.
509
    // Based on the psuedo-code from WAI-ARIA 1.0 Authoring Practices section
510
    // 3.3.1 "Trapping Focus".
511
    modalTabTrapHandler = function (evt) {
512
      // We only care about the TAB key.
513
      if (evt.which != 9) {
514
        return true;
515
      }
516

    
517
      var tabbableElements = getTabbableElements(),
518
          firstTabbableElement = tabbableElements[0],
519
          lastTabbableElement = tabbableElements[tabbableElements.length - 1],
520
          singleTabbableElement = firstTabbableElement == lastTabbableElement,
521
          node = evt.target;
522

    
523
      // If this is the first element and the user wants to go backwards, then
524
      // jump to the last element.
525
      if (node == firstTabbableElement && evt.shiftKey) {
526
        if (!singleTabbableElement) {
527
          lastTabbableElement.focus();
528
        }
529
        return false;
530
      }
531
      // If this is the last element and the user wants to go forwards, then
532
      // jump to the first element.
533
      else if (node == lastTabbableElement && !evt.shiftKey) {
534
        if (!singleTabbableElement) {
535
          firstTabbableElement.focus();
536
        }
537
        return false;
538
      }
539
      // If this element isn't in the dialog at all, then jump to the first
540
      // or last element to get the user into the game.
541
      else if ($.inArray(node, tabbableElements) == -1) {
542
        // Make sure the node isn't in another modal (ie. WYSIWYG modal).
543
        var parents = $(node).parents().get();
544
        for (var i = 0; i < parents.length; ++i) {
545
          var position = $(parents[i]).css('position');
546
          if (position == 'absolute' || position == 'fixed') {
547
            return true;
548
          }
549
        }
550

    
551
        if (evt.shiftKey) {
552
          lastTabbableElement.focus();
553
        }
554
        else {
555
          firstTabbableElement.focus();
556
        }
557
      }
558
    };
559
    $('body').bind('keydown', modalTabTrapHandler);
560

    
561
    // Create our content div, get the dimensions, and hide it
562
    var modalContent = $('#modalContent').css('top','-1000px');
563
    var $modalHeader = modalContent.find('.modal-header');
564
    var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0);
565
    var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
566
    $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show();
567
    modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed);
568

    
569
    // Bind a click for closing the modalContent
570
    modalContentClose = function(){close(); return false;};
571
    $('.close', $modalHeader).bind('click', modalContentClose);
572

    
573
    // Bind a keypress on escape for closing the modalContent
574
    modalEventEscapeCloseHandler = function(event) {
575
      if (event.keyCode == 27) {
576
        close();
577
        return false;
578
      }
579
    };
580

    
581
    $(document).bind('keydown', modalEventEscapeCloseHandler);
582

    
583
    // Per WAI-ARIA 1.0 Authoring Practices, initial focus should be on the
584
    // close button, but we should save the original focus to restore it after
585
    // the dialog is closed.
586
    var oldFocus = document.activeElement;
587
    $('.close', $modalHeader).focus();
588

    
589
    // Close the open modal content and backdrop
590
    function close() {
591
      // Unbind the events
592
      $(window).unbind('resize',  modalContentResize);
593
      $('body').unbind( 'focus', modalEventHandler);
594
      $('body').unbind( 'keypress', modalEventHandler );
595
      $('body').unbind( 'keydown', modalTabTrapHandler );
596
      $('.close', $modalHeader).unbind('click', modalContentClose);
597
      $(document).unbind('keydown', modalEventEscapeCloseHandler);
598
      $(document).trigger('CToolsCloseModalBehaviors', $('#modalContent'));
599
      $(document).trigger('CToolsDetachBehaviors', $('#modalContent'));
600

    
601
      // Closing animation.
602
      switch (animation) {
603
        case 'fadeIn':
604
          modalContent.fadeOut(speed, modalContentRemove);
605
          break;
606

    
607
        case 'slideDown':
608
          modalContent.slideUp(speed, modalContentRemove);
609
          break;
610

    
611
        case 'show':
612
          modalContent.hide(speed, modalContentRemove);
613
          break;
614
      }
615
    }
616

    
617
    // Remove the content.
618
    modalContentRemove = function () {
619
      $('#modalContent').remove();
620
      $('#modalBackdrop').remove();
621

    
622
      // Restore focus to where it was before opening the dialog.
623
      $(oldFocus).focus();
624
    };
625

    
626
    // Move and resize the modalBackdrop and modalContent on window resize.
627
    modalContentResize = function () {
628
      // Reset the backdrop height/width to get accurate document size.
629
      $('#modalBackdrop').css('height', '').css('width', '');
630

    
631
      // Position code lifted from:
632
      // http://www.quirksmode.org/viewport/compatibility.html
633
      if (self.pageYOffset) { // all except Explorer
634
        var wt = self.pageYOffset;
635
      } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
636
        var wt = document.documentElement.scrollTop;
637
      } else if (document.body) { // all other Explorers
638
        var wt = document.body.scrollTop;
639
      }
640

    
641
      // Get our heights
642
      var docHeight = $(document).height();
643
      var docWidth = $(document).width();
644
      var winHeight = $(window).height();
645
      var winWidth = $(window).width();
646
      if( docHeight < winHeight ) docHeight = winHeight;
647

    
648
      // Get where we should move content to
649
      var modalContent = $('#modalContent');
650
      var mdcTop = wt + Math.max((winHeight / 2) - (modalContent.outerHeight() / 2), 0);
651
      var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
652

    
653
      // Apply the changes
654
      $('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show();
655
      modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show();
656
    };
657
    $(window).bind('resize', modalContentResize);
658
  };
659

    
660
  /**
661
   * unmodalContent
662
   * @param content (The jQuery object to remove)
663
   * @param animation (fadeOut, slideUp, show)
664
   * @param speed (valid animation speeds slow, medium, fast or # in ms)
665
   */
666
  Drupal.CTools.Modal.unmodalContent = function(content, animation, speed)
667
  {
668
    // If our animation isn't set, make it just show/pop
669
    if (!animation) { var animation = 'show'; } else {
670
      // If our animation isn't "fade" then it always is show
671
      if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show';
672
    }
673
    // Set a speed if we dont have one
674
    if ( !speed ) var speed = 'fast';
675

    
676
    // Unbind the events we bound
677
    $(window).unbind('resize', modalContentResize);
678
    $('body').unbind('focus', modalEventHandler);
679
    $('body').unbind('keypress', modalEventHandler);
680
    $('body').unbind( 'keydown', modalTabTrapHandler );
681
    var $modalContent = $('#modalContent');
682
    var $modalHeader = $modalContent.find('.modal-header');
683
    $('.close', $modalHeader).unbind('click', modalContentClose);
684
    $(document).unbind('keydown', modalEventEscapeCloseHandler);
685
    $(document).trigger('CToolsDetachBehaviors', $modalContent);
686

    
687
    // jQuery magic loop through the instances and run the animations or removal.
688
    content.each(function(){
689
      if ( animation == 'fade' ) {
690
        $('#modalContent').fadeOut(speed, function() {
691
          $('#modalBackdrop').fadeOut(speed, function() {
692
            $(this).remove();
693
          });
694
          $(this).remove();
695
        });
696
      } else {
697
        if ( animation == 'slide' ) {
698
          $('#modalContent').slideUp(speed,function() {
699
            $('#modalBackdrop').slideUp(speed, function() {
700
              $(this).remove();
701
            });
702
            $(this).remove();
703
          });
704
        } else {
705
          $('#modalContent').remove();
706
          $('#modalBackdrop').remove();
707
        }
708
      }
709
    });
710
  };
711

    
712
$(function() {
713
  Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display;
714
  Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss;
715
});
716

    
717
})(jQuery);