Projet

Général

Profil

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

Thomas Jund, 16 juillet 2020 11:17

Télécharger (2,33 ko)

Voir les différences:

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

Launch callbacks when page scroll below or above a limit
 combo/public/static/js/combo.public.js | 45 ++++++++++++++++++++++++--
 1 file changed, 42 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 below a limit
80
 *     new ComboScrollY({
81
 *       limit: int,
82
 *       below: callback function,
83
 *       above: callback function
84
 *     })
85
 */
86
function ComboScrollY(options) {
87
  this.defaults = {
88
    limit: 0
89
  };
90
  this.options = options;
91
  this.init();
92
};
93
ComboScrollY.prototype.init = function(){
94
  this.options = $.extend({}, this.defaults, this.options);
95
  this.callbacks();
96
  window.addEventListener('scroll', this.callbacks.bind(this));
97
};
98
ComboScrollY.prototype.position = function(){
99
    return (window.pageYOffset <= this.options.limit) ? "above" : "below";
100
};
101
ComboScrollY.prototype.update = function(){
102
    return (this.position() === this.last_callback_position) ? false : true;
103
};
104
ComboScrollY.prototype.callbacks = function() {
105
  if (this.update()) {
106
    if (this.position() === "below") {
107
      this.options.below();
108
      this.last_callback_position = "below";
109
    } else {
110
      this.options.above();
111
      this.last_callback_position = "above";
112
    }
113
  }
114
};
115

  
116

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

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