Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / js / plugins / media.views.js @ 87dbc3bf

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 the click on a media item
84
  $('.view-content .media-item', view).bind('click', 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
    // Multiselect!
95
    if (Drupal.settings.media.browser.params.multiselect) {
96
      // Loop through the already selected files
97
      for (index in Drupal.media.browser.selectedMedia) {
98
        var currentFid = Drupal.media.browser.selectedMedia[index].fid;
99

    
100
        // If the current file exists in the list of already selected
101
        // files, we deselect instead of selecting
102
        if (currentFid == fid) {
103
          $(this).removeClass('selected');
104
          // If we change the fid, the later matching won't
105
          // add it back again because it can't find it.
106
          fid = NaN;
107

    
108
          // The previously selected file wasn't clicked, so we retain it
109
          // as an active file
110
        }
111
        else {
112
          // Add to list of already selected files
113
          selectedFiles.push(Drupal.media.browser.selectedMedia[index]);
114

    
115
          // Mark it as selected
116
          $('.view-content *[data-fid=' + currentFid + '].media-item', view).addClass('selected');
117
        }
118
      }
119
    }
120

    
121
    // Because the files are added using drupal_add_js() and due to the fact
122
    // that drupal_get_js() runs a drupal_array_merge_deep() which re-numbers
123
    // numeric key values, we have to search in Drupal.settings.media.files
124
    // for the matching file ID rather than referencing it directly.
125
    for (index in Drupal.settings.media.files) {
126
      if (Drupal.settings.media.files[index].fid == fid) {
127
        selectedFiles.push(Drupal.settings.media.files[index]);
128

    
129
        // If multiple tabs contains the same file, it will be present in the
130
        // files-array multiple times, so we break out early so we don't have
131
        // it in the selectedFiles array multiple times.
132
        // This would interfer with multi-selection, so...
133
        break;
134
      }
135
    }
136
    Drupal.media.browser.selectMedia(selectedFiles);
137
  });
138

    
139
  // Add the processed class, so we dont accidentally process the same element twice..
140
  $(view).addClass('media-browser-views-processed');
141
}
142

    
143
}(jQuery));