all files / lib/ prelodr.js

100% Statements 155/155
95.24% Branches 80/84
100% Functions 33/33
100% Lines 126/126
20 statements, 2 functions, 19 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337                                        18×       22× 22×     24×   24× 21×     24×     41×   41× 26×     41×     18× 18× 18× 18× 18×             18×       12×                                                                               24×                   15×   15× 15×   15× 15× 15× 15×   15× 11× 11× 11×     15×   15× 15× 15× 15×   15× 15× 15× 15×   15×     15× 13×                                       18×   18× 13×   13× 13× 13×   13×                         13×           13×   13×     13×                                                                                                                                        
/**
 * Prelodr v1.0.5
 * http://git.io/prelodr
 *
 * @author    José Luis Quintana | quintana.io
 * @license   MIT
 */
 
/**
 * Prelodr class
 */
class Prelodr {
 
  /**
   * Constructor class
   * @param {HTMLElement} container - Container element for Prelodr.
   * Default is body
   * @param {Object} options - Default options object
   * @constructor
   */
  constructor(container, options) {
    this.queu = {
      queue: [],
      offset: 0,
      add: function (el) {
        this.queue.push(el);
        this.offset += 1;
      },
      shift: function () {
        let el = null;
 
        if (this.queue.length > 0) {
          el = this.queue.shift();
        }
 
        return el;
      },
      first: function () {
        let el = null;
 
        if (this.queue.length > 0) {
          el = this.queue[0];
        }
 
        return el;
      }
    };
    this._interval = 0;
    this.isShown = false;
    this.isAnimating = false;
    this.isStart = false;
    this.options = {
      duration: 750,
      prefixClass: 'prelodr',
      show: null,
      hide: null
    };
 
    if (typeof container === 'object') {
      if (container.nodeName) {
        this.setContainer(container);
        this.setOptions(options);
      } else {
        this.setContainer(window.document.body);
        this.setOptions(container);
      }
    } else {
      this.container = window.document.body;
    }
  }
 
  /**
   * Set the default options
   *
   * @param {Object} options - Options object
   */
  setOptions(options) {
    this.options = this._merge(this.options, options);
  }
 
  /**
   * Marge two hash objects
   *
   * @param {Object} a - First object
   * @param {Object} b - Second object
   * @return {Object}
   */
  _merge(a, b) {
    let i;
 
    if (b) {
      for (i in b) {
        Eif (b && b[i]) {
          a[i] = b[i];
        }
      }
    }
 
    return a;
  }
 
  /**
   * Set container for Prelodr
   *
   * @param {HTMLElement} container - The container for Prelodr.
   */
  setContainer(container) {
    this.container = container;
  }
 
  /**
   * Get ramdom string id
   * @return {String} - String Id
   */
  _getId() {
    return Math.random().toString(36).slice(2);
  }
 
  /**
   * Create and show preloader elements
   * @param  {Prelodr} me - Prelodr object
   * @param  {String} text - Text for prelodr
   * @param  {Function} cb - Callback
   */
  _show(text, cb) {
    this.wrapper = window.document.createElement('span');
 
    const spanText = window.document.createElement('span');
    const progressbar = window.document.createElement('span');
 
    spanText.appendChild(window.document.createTextNode(text));
    this.wrapper.appendChild(spanText);
    spanText.appendChild(progressbar);
    progressbar.className = `${this.options.prefixClass}-progressbar`;
 
    if (!this.element) {
      this.element = window.document.createElement('span');
      this.element.className = this.options.prefixClass;
      this.container.appendChild(this.element);
    }
 
    this.element.appendChild(this.wrapper);
 
    setTimeout(() => {
      const cls = `${this.options.prefixClass} ${this.options.prefixClass}-in`;
      this.wrapper.children[0].className = `${this.options.prefixClass}-in`;
      this.element.className = cls;
 
      setTimeout(() => {
        this.isShown = true;
        this.isAnimating = false;
        this.queu.shift();
 
        if (this.options.show) {
          this.options.show(this, this.element);
        }
 
        if (cb) {
          cb();
        }
      }, this.options.duration);
    }, 10);
  }
 
  /**
   * Hide prelodr elements
   * @param  {Prelodr} me - Prelodr object
   * @param  {Function} cb - Callback
   */
  _hide(cb) {
    Eif (this.isShown) {
      this.isShown = false;
      this._prepOut(cb);
    }
  }
 
  /**
   * Walk queue
   * @param  {Queu} queu - Queu object
   */
  _queueWalk() {
    const one = this.queu.first();
 
    if (one && one.is === 'in') {
      this.isShown = true;
 
      one.fn(() => {
        const next = this.queu.first();
        this.isStart = false;
 
        if (next && next.is === 'out') {
          next.fn(() => {
            this._queueWalk();
          });
        }
      });
    }
  }
 
  /**
   * Show the Prelodr
   * @param  {String} text - Text for prelodr
   * @return {Prelodr} - Prelodr object
   */
  in(text) {
    const obj = {
      id: this._getId(),
      is: 'in',
      fn: cb => this._show(text, cb)
    };
 
    this.queu.add(obj);
 
    if (!this.isStart) {
      this.isStart = true;
      this._queueWalk();
    }
 
    return this;
  }
 
  /**
   * Hide the Prelodr
   * @return {Prelodr} - Prelodr object
   */
  out(fn) {
    this.queu.add({
      id: this._getId(),
      is: 'out',
      fn: cb => {
        if (fn && typeof fn === 'function') {
          fn(() => {
            this._hide(cb);
          });
        } else {
          this._hide(cb);
        }
      }
    });
 
    if (!this.isStart) {
      const one = this.queu.first();
 
      Eif (one && one.is === 'out') {
        one.fn(this._queueWalk);
      }
    }
 
    return this;
  }
 
  /**
   * Prepare to hide the Prelodr
   * @param  {Function} cb Callback
   */
  _prepOut(cb) {
    this.isAnimating = true;
    this.queu.shift();
 
    const el = this.queu.first();
    this.wrapper.children[0].className = '';
 
    setTimeout(() => {
      if (this.options.hide) {
        this.options.hide(this, []);
      }
 
      if (el) {
        this.element.removeChild(this.wrapper);
      } else {
        const cls = `${this.options.prefixClass} ${this.options.prefixClass}-out`;
        this.element.className = cls;
 
        setTimeout(() => {
          this.container.removeChild(this.element);
          this.element = null;
        }, this.options.duration / 1.5);
      }
 
      this.isAnimating = false;
      this.isShown = false;
 
      if (cb) {
        cb();
      }
    }, this.options.duration);
  }
 
  /**
   * Check if Prelodr is visible
   *
   * @return {Boolean}
   */
  isVisible() {
    return this.isShown;
  }
 
}
 
(() => {
  /* istanbul ignore else */
  Eif (typeof module === 'object' && typeof module.exports === 'object') {
    module.exports = Prelodr;
  /* istanbul ignore else */
  } else if (typeof define === 'function' && define.amd) {
    define([], () => {
      return Prelodr;
    });
  } else {
    window.Prelodr = Prelodr;
 
    if (window.jQuery) {
      window.jQuery.fn.prelodr = function (options) {
        let fn;
        let prelodr;
 
        if (typeof options === 'string') {
          prelodr = this.data('prelodr');
          fn = prelodr[options];
 
          if (prelodr !== 'undefined' && typeof options === 'string' && fn) {
            return fn.apply(prelodr, Array.prototype.slice.call(arguments, 1));
          }
 
          const str = `Method ${options} is not supported by jQuery.prelodr.`;
          window.jQuery.error(str);
        } else {
          prelodr = new window.Prelodr(this[0], options);
          this.data('prelodr', prelodr);
        }
 
        return this;
      };
    }
  }
})();