Projet

Général

Profil

Paste
Télécharger (5,96 ko) Statistiques
| Branche: | Révision:

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

1
/**
2
 * @file
3
 * Handles the JS for the views file browser.
4
 *
5
 * Note that this does not currently support multiple file selection
6
 */
7

    
8
(function ($) {
9

    
10
namespace('Drupal.media.browser.views');
11
Drupal.behaviors.mediaViews = {
12
  attach: function (context, settings) {
13

    
14
    // Make sure when pressing enter on text inputs, the form isn't submitted
15
    $('.ctools-auto-submit-full-form .views-exposed-form input:text, input:text.ctools-auto-submit', context)
16
      .filter(':not(.ctools-auto-submit-exclude)')
17
      .bind('keydown keyup', function (e) {
18
        if(e.keyCode === 13) {
19
          e.stopImmediatePropagation();
20
          e.preventDefault();
21
        }
22
      });
23
    // Disable the links on media items list
24
    $('.view-content ul.media-list-thumbnails a').click(function() {
25
      return false;
26
    });
27

    
28
    // We loop through the views listed in Drupal.settings.media.browser.views
29
    // and set them up individually.
30
    var views_ids = [];
31
    for(var key in Drupal.settings.media.browser.views){
32
      views_ids.push(key);
33
    }
34

    
35
    for (var i = 0; i < views_ids.length; i++) {
36
      var views_id = views_ids[i];
37
      for (var j= 0; j < Drupal.settings.media.browser.views[views_id].length; j++) {
38
        var views_display_id = Drupal.settings.media.browser.views[views_id][j],
39
          view = $('.view-id-' + views_id + '.view-display-id-' + views_display_id);
40
        if (view.length) {
41
          Drupal.media.browser.views.setup(view);
42
        }
43
      }
44
    }
45

    
46
    // Reset the state on tab-changes- bind on the 'select' event on the tabset
47
    $('#media-browser-tabset').bind('tabsselect', function(event, ui) {
48
      var view = $('.view', ui.panel);
49
      if (view.length) {
50
        Drupal.media.browser.views.select(view);
51
      }
52
    });
53

    
54
  }
55
}
56

    
57
/**
58
 * Event-function that is called with a view, when the tab containing that
59
 * view is selected.
60
 */
61
Drupal.media.browser.views.select = function(view) {
62
  // Reset the list of selected files
63
  Drupal.media.browser.selectMedia([]);
64

    
65
  // Reset all 'selected'-status.
66
  $('.view-content .media-item', view).removeClass('selected');
67
}
68

    
69
/**
70
 * Setup function. Called once for every Media Browser view.
71
 *
72
 * Sets up event-handlers for selecting items in the view.
73
 */
74
Drupal.media.browser.views.setup = function(view) {
75
  // Ensure we only setup each view once..
76
  if ($(view).hasClass('media-browser-views-processed')) {
77
    return;
78
  }
79

    
80
  // Reset the list of selected files
81
  Drupal.media.browser.selectMedia([]);
82

    
83
  // Catch double click to submit a single item.
84
  $('.view-content .media-item', view).bind('dblclick', function () {
85
    var fid = $(this).closest('.media-item[data-fid]').data('fid'),
86
      selectedFiles = new Array();
87

    
88
    // Remove all currently selected files
89
    $('.view-content .media-item', view).removeClass('selected');
90

    
91
    // Mark it as selected
92
    $(this).addClass('selected');
93

    
94
    // Because the files are added using drupal_add_js() and due to the fact
95
    // that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
96
    // numeric key values, we have to search in Drupal.settings.media.files
97
    // for the matching file ID rather than referencing it directly.
98
    for (index in Drupal.settings.media.files) {
99
      if (Drupal.settings.media.files[index].fid == fid) {
100
        selectedFiles.push(Drupal.settings.media.files[index]);
101

    
102
        // If multiple tabs contains the same file, it will be present in the
103
        // files-array multiple times, so we break out early so we don't have
104
        // it in the selectedFiles array multiple times.
105
        // This would interfer with multi-selection, so...
106
        break;
107
      }
108
    }
109
    Drupal.media.browser.selectMediaAndSubmit(selectedFiles);
110
  });
111

    
112

    
113
  // Catch the click on a media item
114
  $('.view-content .media-item', view).bind('click', function () {
115
    var fid = $(this).closest('.media-item[data-fid]').data('fid'),
116
      selectedFiles = new Array();
117

    
118
    // Remove all currently selected files
119
    $('.view-content .media-item', view).removeClass('selected');
120

    
121
    // Mark it as selected
122
    $(this).addClass('selected');
123

    
124
    // Multiselect!
125
    if (Drupal.settings.media.browser.params.multiselect) {
126
      // Loop through the already selected files
127
      for (index in Drupal.media.browser.selectedMedia) {
128
        var currentFid = Drupal.media.browser.selectedMedia[index].fid;
129

    
130
        // If the current file exists in the list of already selected
131
        // files, we deselect instead of selecting
132
        if (currentFid == fid) {
133
          $(this).removeClass('selected');
134
          // If we change the fid, the later matching won't
135
          // add it back again because it can't find it.
136
          fid = NaN;
137

    
138
          // The previously selected file wasn't clicked, so we retain it
139
          // as an active file
140
        }
141
        else {
142
          // Add to list of already selected files
143
          selectedFiles.push(Drupal.media.browser.selectedMedia[index]);
144

    
145
          // Mark it as selected
146
          $('.view-content *[data-fid=' + currentFid + '].media-item', view).addClass('selected');
147
        }
148
      }
149
    }
150

    
151
    // Because the files are added using drupal_add_js() and due to the fact
152
    // that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
153
    // numeric key values, we have to search in Drupal.settings.media.files
154
    // for the matching file ID rather than referencing it directly.
155
    for (index in Drupal.settings.media.files) {
156
      if (Drupal.settings.media.files[index].fid == fid) {
157
        selectedFiles.push(Drupal.settings.media.files[index]);
158

    
159
        // If multiple tabs contains the same file, it will be present in the
160
        // files-array multiple times, so we break out early so we don't have
161
        // it in the selectedFiles array multiple times.
162
        // This would interfer with multi-selection, so...
163
        break;
164
      }
165
    }
166
    Drupal.media.browser.selectMedia(selectedFiles);
167
  });
168

    
169
  // Add the processed class, so we dont accidentally process the same element twice..
170
  $(view).addClass('media-browser-views-processed');
171
}
172

    
173
}(jQuery));