Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / js / media.browser.js @ 2b3c8cc1

1
/**
2
 * @file
3
 * Provides default functions for the media browser
4
 */
5

    
6
(function ($) {
7
namespace('Drupal.media.browser');
8

    
9
Drupal.media.browser.selectedMedia = [];
10
Drupal.media.browser.mediaAdded = function () {};
11
Drupal.media.browser.selectionFinalized = function (selectedMedia) {
12
  // This is intended to be overridden if a callee wants to be triggered
13
  // when the media selection is finalized from inside the browser.
14
  // This is used for the file upload form for instance.
15
};
16

    
17
Drupal.behaviors.MediaBrowser = {
18
  attach: function (context) {
19
    if (Drupal.settings.media && Drupal.settings.media.selectedMedia) {
20
      Drupal.media.browser.selectMedia(Drupal.settings.media.selectedMedia);
21
      // Fire a confirmation of some sort.
22
      Drupal.media.browser.finalizeSelection();
23
    }
24

    
25
    // Instantiate the tabs.
26
    var showFunc = function(event, ui) {
27
      // Store index of the tab being activated.
28
      if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
29
        $(parent_iframe).attr('current_tab', $('#media-tabs-wrapper > ul > li.ui-state-active').index());
30
      }
31
    };
32
    var activeTab = Drupal.media.browser.tabFromHash();
33
    $('#media-browser-tabset').once('MediaBrowser').tabs({
34
      selected: activeTab, // jquery < 1.9
35
      active: activeTab, // jquery >= 1.9
36
      show: showFunc, // jquery ui < 1.8
37
      activate: showFunc // jquery ui >= 1.8
38
    });
39

    
40
    $('.media-browser-tab').each( Drupal.media.browser.validateButtons );
41
  }
42
  // Wait for additional params to be passed in.
43
};
44

    
45
Drupal.media.browser.getParentIframe = function (window) {
46
  var arrFrames = parent.document.getElementsByTagName("IFRAME");
47
  for (var i = 0; i < arrFrames.length; i++) {
48
    if (arrFrames[i].contentWindow === window) {
49
      return arrFrames[i];
50
    }
51
  }
52
}
53

    
54
/**
55
 * Get index of the active tab from window.location.hash
56
 */
57
Drupal.media.browser.tabFromHash = function () {
58
  if (parent_iframe = Drupal.media.browser.getParentIframe(window)) {
59
    return $(parent_iframe).attr('current_tab');
60
  }
61
  return 0;
62
};
63

    
64
Drupal.media.browser.launch = function () {
65

    
66
};
67

    
68
Drupal.media.browser.validateButtons = function() {
69
  // The media browser runs in an IFRAME. The Drupal.media.popups.mediaBrowser()
70
  // function sets up the IFRAME and an "OK" button that is outside of the
71
  // IFRAME, so that its click handlers can destroy the IFRAME while retaining
72
  // information about what media items were selected. However, Drupal UI
73
  // convention is to place all action buttons on the same "line" at the bottom
74
  // of the form, so if the form within the IFRAME contains a "Submit" button or
75
  // other action buttons, then the "OK" button will appear below the IFRAME
76
  // which breaks this convention and is confusing to the user. Therefore, we
77
  // add a "Submit" button inside the IFRAME, and have its click action trigger
78
  // the click action of the corresponding "OK" button that is outside the
79
  // IFRAME. media.css contains CSS rules that hide the outside buttons.
80

    
81
  // If a submit button is present, another round-trip to the server is needed
82
  // before the user's selection is finalized. For these cases, when the form's
83
  // real Submit button is clicked, the server either returns another form for
84
  // the user to fill out, or else a completion page that contains or sets the
85
  // Drupal.media.browser.selectedMedia variable. If the latter, then
86
  // Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad() auto-triggers the
87
  // "OK" button action to finalize the selection and remove the IFRAME.
88

    
89
  // We need to check for the fake submit button that is used on non-form based
90
  // pane content. On these items we need to bind the clicks so that media can
91
  // be selected or the window can be closed. This is still a hacky approach,
92
  // but it is a step in the right direction.
93

    
94
  $('a.button.fake-submit', this).once().bind('click', Drupal.media.browser.submit);
95
};
96

    
97
Drupal.media.browser.submit = function () {
98
  // @see Drupal.media.browser.validateButtons().
99
  var buttons = $(parent.window.document.body).find('#mediaBrowser').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
100
  buttons[0].click();
101

    
102
  // Return false to prevent the fake link "click" from continuing.
103
  return false;
104
}
105

    
106
Drupal.media.browser.selectMedia = function (selectedMedia) {
107
  Drupal.media.browser.selectedMedia = selectedMedia;
108
};
109

    
110
Drupal.media.browser.selectMediaAndSubmit = function (selectedMedia) {
111
  Drupal.media.browser.selectedMedia = selectedMedia;
112
  Drupal.media.browser.submit();
113
};
114

    
115
Drupal.media.browser.finalizeSelection = function () {
116
  if (!Drupal.media.browser.selectedMedia) {
117
    throw new exception(Drupal.t('Cannot continue, nothing selected'));
118
  }
119
  else {
120
    Drupal.media.browser.selectionFinalized(Drupal.media.browser.selectedMedia);
121
  }
122
};
123

    
124
}(jQuery));