Projet

Général

Profil

0001-JS-introduce-combo_scrollY-global-function-45040.patch

Thomas Jund, 13 juillet 2020 12:36

Télécharger (2,32 ko)

Voir les différences:

Subject: [PATCH] JS: introduce combo_scrollY global function (#45040)

Launch callback when page scroll below or above a limit
 combo/public/static/js/combo.public.js | 44 ++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 3 deletions(-)
combo/public/static/js/combo.public.js
76 76
    return search;
77 77
}
78 78

  
79
/*  Launch callbacks when scroll move above and/or below a limit
80
 *     new Combo_scroll_Y({
81
 *       limit: int,
82
 *       below: callback function,
83
 *       above: callback function
84
 *     })
85
 */
86
function Combo_scroll_Y(options) {
87
  this.defaults = {
88
    limit: 0
89
  };
90
  this.options = options;
91
  this.init();
92
};
93
Combo_scroll_Y.prototype.init = function(){
94
  this.options = $.extend({}, this.defaults, this.options);
95
  window.addEventListener('scroll', this.callbacks.bind(this));
96
};
97
Combo_scroll_Y.prototype.position = function(){
98
    return (window.pageYOffset <= this.options.limit) ? "above" : "below";
99
};
100
Combo_scroll_Y.prototype.update = function(){
101
    return (this.position() === this.last_callback_position) ? false : true;
102
};
103
Combo_scroll_Y.prototype.callbacks = function() {
104
  if (this.update()) {
105
    if (this.position() === "below") {
106
      this.options.below();
107
      this.last_callback_position = "below";
108
    } else {
109
      this.options.above();
110
      this.last_callback_position = "above";
111
    }
112
  }
113
};
114

  
115

  
79 116
$(function() {
80 117
  $('[data-ajax-cell-refresh]').each(function(idx, elem) {
81 118
    var $elem = $(elem);
......
247 284
  $(document).on('combo:cell-loaded', function(ev, cell) { prepare_foldable(cell); });
248 285

  
249 286
  /* add a scrolled class to body once the user scrolled the page a bit */
250
  $(window).on('scroll', function() {
251
    if ($(window).scrollTop() == 0) {
287
  var body_is_scrolled = new Combo_scroll_Y({
288
    above: function(){
252 289
      $('body').removeClass('scrolled');
253
    } else {
290
    },
291
    below: function(){
254 292
      $('body').addClass('scrolled');
255 293
    }
256 294
  });
257
-