Projet

Général

Profil

Révision 5d12d676

Ajouté par Assos Assos il y a environ 6 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/views/js/ajax_view.js
4 4
 */
5 5
(function ($) {
6 6

  
7
/**
8
 * Attaches the AJAX behavior to Views exposed filter forms and key View links.
9
 */
10
Drupal.behaviors.ViewsAjaxView = {};
11
Drupal.behaviors.ViewsAjaxView.attach = function() {
12
  if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
13
    $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
14
      Drupal.views.instances[i] = new Drupal.views.ajaxView(settings);
15
    });
16
  }
17
};
7
  /**
8
   * Attaches the AJAX behavior to exposed filter forms and key views links.
9
   */
10
  Drupal.behaviors.ViewsAjaxView = {};
11
  Drupal.behaviors.ViewsAjaxView.attach = function() {
12
    if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
13
      $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
14
        Drupal.views.instances[i] = new Drupal.views.ajaxView(settings);
15
      });
16
    }
17
  };
18 18

  
19
Drupal.views = {};
20
Drupal.views.instances = {};
19
  Drupal.views = {};
20
  Drupal.views.instances = {};
21 21

  
22
/**
23
 * Javascript object for a certain view.
24
 */
25
Drupal.views.ajaxView = function(settings) {
26
  var selector = '.view-dom-id-' + settings.view_dom_id;
27
  this.$view = $(selector);
28

  
29
  // Retrieve the path to use for views' ajax.
30
  var ajax_path = Drupal.settings.views.ajax_path;
31

  
32
  // If there are multiple views this might've ended up showing up multiple times.
33
  if (ajax_path.constructor.toString().indexOf("Array") != -1) {
34
    ajax_path = ajax_path[0];
35
  }
36

  
37
  // Check if there are any GET parameters to send to views.
38
  var queryString = window.location.search || '';
39
  if (queryString !== '') {
40
    // Remove the question mark and Drupal path component if any.
41
    var queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
22
  /**
23
   * Javascript object for a certain view.
24
   */
25
  Drupal.views.ajaxView = function(settings) {
26
    var selector = '.view-dom-id-' + settings.view_dom_id;
27
    this.$view = $(selector);
28

  
29
    // Retrieve the path to use for views' ajax.
30
    var ajax_path = Drupal.settings.views.ajax_path;
31

  
32
    // If there are multiple views this might've ended up showing up multiple
33
    // times.
34
    if (ajax_path.constructor.toString().indexOf("Array") != -1) {
35
      ajax_path = ajax_path[0];
36
    }
37

  
38
    // Check if there are any GET parameters to send to views.
39
    var queryString = window.location.search || '';
42 40
    if (queryString !== '') {
43
      // If there is a '?' in ajax_path, clean url are on and & should be used to add parameters.
44
      queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
41
      // Remove the question mark and Drupal path component if any.
42
      var queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
43
      if (queryString !== '') {
44
        // If there is a '?' in ajax_path, clean url are on and & should be
45
        // used to add parameters.
46
        queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
47
      }
45 48
    }
46
  }
47

  
48
  this.element_settings = {
49
    url: ajax_path + queryString,
50
    submit: settings,
51
    setClick: true,
52
    event: 'click',
53
    selector: selector,
54
    progress: { type: 'throbber' }
55
  };
56 49

  
57
  this.settings = settings;
50
    this.element_settings = {
51
      url: ajax_path + queryString,
52
      submit: settings,
53
      setClick: true,
54
      event: 'click',
55
      selector: selector,
56
      progress: {
57
        type: 'throbber'
58
      }
59
    };
60

  
61
    this.settings = settings;
58 62

  
59
  // Add the ajax to exposed forms.
60
  this.$exposed_form = $('#views-exposed-form-'+ settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
61
  this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this));
63
    // Add the ajax to exposed forms.
64
    this.$exposed_form = $('#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
65
    this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this));
62 66

  
63
  // Store Drupal.ajax objects here for all pager links.
64
  this.links = [];
67
    // Store Drupal.ajax objects here for all pager links.
68
    this.links = [];
65 69

  
66
  // Add the ajax to pagers.
67
  this.$view
70
    // Add the ajax to pagers.
71
    this.$view
68 72
    // Don't attach to nested views. Doing so would attach multiple behaviors
69 73
    // to a given element.
70
    .filter(jQuery.proxy(this.filterNestedViews, this))
71
    .once(jQuery.proxy(this.attachPagerAjax, this));
72

  
73
  // Add a trigger to update this view specifically. In order to trigger a
74
  // refresh use the following code.
75
  //
76
  // @code
77
  // jQuery('.view-name').trigger('RefreshView');
78
  // @endcode
79
  // Add a trigger to update this view specifically.
80
  var self_settings = this.element_settings;
81
  self_settings.event = 'RefreshView';
82
  this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings);
83
};
84

  
85
Drupal.views.ajaxView.prototype.attachExposedFormAjax = function() {
86
  var button = $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form);
87
  button = button[0];
88

  
89
  this.exposedFormAjax = new Drupal.ajax($(button).attr('id'), button, this.element_settings);
90
};
91

  
92
Drupal.views.ajaxView.prototype.filterNestedViews= function() {
93
  // If there is at least one parent with a view class, this view
94
  // is nested (e.g., an attachment). Bail.
95
  return !this.$view.parents('.view').size();
96
};
74
      .filter(jQuery.proxy(this.filterNestedViews, this))
75
      .once(jQuery.proxy(this.attachPagerAjax, this));
76

  
77
    // Add a trigger to update this view specifically. In order to trigger a
78
    // refresh use the following code.
79
    //
80
    // @code
81
    // jQuery('.view-name').trigger('RefreshView');
82
    // @endcode
83
    // Add a trigger to update this view specifically.
84
    var self_settings = this.element_settings;
85
    self_settings.event = 'RefreshView';
86
    this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings);
87
  };
97 88

  
98
/**
99
 * Attach the ajax behavior to each link.
100
 */
101
Drupal.views.ajaxView.prototype.attachPagerAjax = function() {
102
  this.$view.find('ul.pager > li > a, th.views-field a, .attachment .views-summary a')
103
  .each(jQuery.proxy(this.attachPagerLinkAjax, this));
104
};
89
  Drupal.views.ajaxView.prototype.attachExposedFormAjax = function() {
90
    var button = $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form);
91
    button = button[0];
105 92

  
106
/**
107
 * Attach the ajax behavior to a singe link.
108
 */
109
Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) {
110
  var $link = $(link);
111
  var viewData = {};
112
  var href = $link.attr('href');
113
  // Construct an object using the settings defaults and then overriding
114
  // with data specific to the link.
115
  $.extend(
93
    // Call the autocomplete submit before doing AJAX.
94
    $(button).click(function () {
95
      if (Drupal.autocompleteSubmit) {
96
        Drupal.autocompleteSubmit();
97
      }
98
    });
99

  
100
    this.exposedFormAjax = new Drupal.ajax($(button).attr('id'), button, this.element_settings);
101
  };
102

  
103
  Drupal.views.ajaxView.prototype.filterNestedViews = function() {
104
    // If there is at least one parent with a view class, this view
105
    // is nested (e.g., an attachment). Bail.
106
    return !this.$view.parents('.view').length;
107
  };
108

  
109
  /**
110
   * Attach the ajax behavior to each link.
111
   */
112
  Drupal.views.ajaxView.prototype.attachPagerAjax = function() {
113
    this.$view.find('ul.pager > li > a, th.views-field a, .attachment .views-summary a')
114
      .each(jQuery.proxy(this.attachPagerLinkAjax, this));
115
  };
116

  
117
  /**
118
   * Attach the ajax behavior to a singe link.
119
   */
120
  Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) {
121
    var $link = $(link);
122
    var viewData = {};
123
    var href = $link.attr('href');
124
    // Construct an object using the settings defaults and then overriding
125
    // with data specific to the link.
126
    $.extend(
116 127
    viewData,
117 128
    this.settings,
118 129
    Drupal.Views.parseQueryString(href),
119 130
    // Extract argument data from the URL.
120 131
    Drupal.Views.parseViewArgs(href, this.settings.view_base_path)
121
  );
122

  
123
  // For anchor tags, these will go to the target of the anchor rather
124
  // than the usual location.
125
  $.extend(viewData, Drupal.Views.parseViewArgs(href, this.settings.view_base_path));
126

  
127
  this.element_settings.submit = viewData;
128
  this.pagerAjax = new Drupal.ajax(false, $link, this.element_settings);
129
  this.links.push(this.pagerAjax);
130
};
131

  
132
Drupal.ajax.prototype.commands.viewsScrollTop = function (ajax, response, status) {
133
  // Scroll to the top of the view. This will allow users
134
  // to browse newly loaded content after e.g. clicking a pager
135
  // link.
136
  var offset = $(response.selector).offset();
137
  // We can't guarantee that the scrollable object should be
138
  // the body, as the view could be embedded in something
139
  // more complex such as a modal popup. Recurse up the DOM
140
  // and scroll the first element that has a non-zero top.
141
  var scrollTarget = response.selector;
142
  while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
143
    scrollTarget = $(scrollTarget).parent();
144
  }
145
  // Only scroll upward
146
  if (offset.top - 10 < $(scrollTarget).scrollTop()) {
147
    $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
148
  }
149
};
132
    );
133

  
134
    // For anchor tags, these will go to the target of the anchor rather
135
    // than the usual location.
136
    $.extend(viewData, Drupal.Views.parseViewArgs(href, this.settings.view_base_path));
137

  
138
    this.element_settings.submit = viewData;
139
    this.pagerAjax = new Drupal.ajax(false, $link, this.element_settings);
140
    this.links.push(this.pagerAjax);
141
  };
142

  
143
  Drupal.ajax.prototype.commands.viewsScrollTop = function (ajax, response, status) {
144
    // Scroll to the top of the view. This will allow users
145
    // to browse newly loaded content after e.g. clicking a pager
146
    // link.
147
    var offset = $(response.selector).offset();
148
    // We can't guarantee that the scrollable object should be
149
    // the body, as the view could be embedded in something
150
    // more complex such as a modal popup. Recurse up the DOM
151
    // and scroll the first element that has a non-zero top.
152
    var scrollTarget = response.selector;
153
    while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
154
      scrollTarget = $(scrollTarget).parent();
155
    }
156
    // Only scroll upward.
157
    if (offset.top - 10 < $(scrollTarget).scrollTop()) {
158
      $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
159
    }
160
  };
150 161

  
151 162
})(jQuery);

Formats disponibles : Unified diff