Switch to unified view

a b/docs/static/js/bulma-slider.js
1
(function webpackUniversalModuleDefinition(root, factory) {
2
    if(typeof exports === 'object' && typeof module === 'object')
3
        module.exports = factory();
4
    else if(typeof define === 'function' && define.amd)
5
        define([], factory);
6
    else if(typeof exports === 'object')
7
        exports["bulmaSlider"] = factory();
8
    else
9
        root["bulmaSlider"] = factory();
10
})(typeof self !== 'undefined' ? self : this, function() {
11
return /******/ (function(modules) { // webpackBootstrap
12
/******/    // The module cache
13
/******/    var installedModules = {};
14
/******/
15
/******/    // The require function
16
/******/    function __webpack_require__(moduleId) {
17
/******/
18
/******/        // Check if module is in cache
19
/******/        if(installedModules[moduleId]) {
20
/******/            return installedModules[moduleId].exports;
21
/******/        }
22
/******/        // Create a new module (and put it into the cache)
23
/******/        var module = installedModules[moduleId] = {
24
/******/            i: moduleId,
25
/******/            l: false,
26
/******/            exports: {}
27
/******/        };
28
/******/
29
/******/        // Execute the module function
30
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31
/******/
32
/******/        // Flag the module as loaded
33
/******/        module.l = true;
34
/******/
35
/******/        // Return the exports of the module
36
/******/        return module.exports;
37
/******/    }
38
/******/
39
/******/
40
/******/    // expose the modules object (__webpack_modules__)
41
/******/    __webpack_require__.m = modules;
42
/******/
43
/******/    // expose the module cache
44
/******/    __webpack_require__.c = installedModules;
45
/******/
46
/******/    // define getter function for harmony exports
47
/******/    __webpack_require__.d = function(exports, name, getter) {
48
/******/        if(!__webpack_require__.o(exports, name)) {
49
/******/            Object.defineProperty(exports, name, {
50
/******/                configurable: false,
51
/******/                enumerable: true,
52
/******/                get: getter
53
/******/            });
54
/******/        }
55
/******/    };
56
/******/
57
/******/    // getDefaultExport function for compatibility with non-harmony modules
58
/******/    __webpack_require__.n = function(module) {
59
/******/        var getter = module && module.__esModule ?
60
/******/            function getDefault() { return module['default']; } :
61
/******/            function getModuleExports() { return module; };
62
/******/        __webpack_require__.d(getter, 'a', getter);
63
/******/        return getter;
64
/******/    };
65
/******/
66
/******/    // Object.prototype.hasOwnProperty.call
67
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
68
/******/
69
/******/    // __webpack_public_path__
70
/******/    __webpack_require__.p = "";
71
/******/
72
/******/    // Load entry module and return exports
73
/******/    return __webpack_require__(__webpack_require__.s = 0);
74
/******/ })
75
/************************************************************************/
76
/******/ ([
77
/* 0 */
78
/***/ (function(module, __webpack_exports__, __webpack_require__) {
79
80
"use strict";
81
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
82
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "isString", function() { return isString; });
83
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__events__ = __webpack_require__(1);
84
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
85
86
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
87
88
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
89
90
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
91
92
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
93
94
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
95
96
97
98
var isString = function isString(unknown) {
99
  return typeof unknown === 'string' || !!unknown && (typeof unknown === 'undefined' ? 'undefined' : _typeof(unknown)) === 'object' && Object.prototype.toString.call(unknown) === '[object String]';
100
};
101
102
var bulmaSlider = function (_EventEmitter) {
103
  _inherits(bulmaSlider, _EventEmitter);
104
105
  function bulmaSlider(selector) {
106
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
107
108
    _classCallCheck(this, bulmaSlider);
109
110
    var _this = _possibleConstructorReturn(this, (bulmaSlider.__proto__ || Object.getPrototypeOf(bulmaSlider)).call(this));
111
112
    _this.element = typeof selector === 'string' ? document.querySelector(selector) : selector;
113
    // An invalid selector or non-DOM node has been provided.
114
    if (!_this.element) {
115
      throw new Error('An invalid selector or non-DOM node has been provided.');
116
    }
117
118
    _this._clickEvents = ['click'];
119
    /// Set default options and merge with instance defined
120
    _this.options = _extends({}, options);
121
122
    _this.onSliderInput = _this.onSliderInput.bind(_this);
123
124
    _this.init();
125
    return _this;
126
  }
127
128
  /**
129
   * Initiate all DOM element containing selector
130
   * @method
131
   * @return {Array} Array of all slider instances
132
   */
133
134
135
  _createClass(bulmaSlider, [{
136
    key: 'init',
137
138
139
    /**
140
     * Initiate plugin
141
     * @method init
142
     * @return {void}
143
     */
144
    value: function init() {
145
      this._id = 'bulmaSlider' + new Date().getTime() + Math.floor(Math.random() * Math.floor(9999));
146
      this.output = this._findOutputForSlider();
147
148
      this._bindEvents();
149
150
      if (this.output) {
151
        if (this.element.classList.contains('has-output-tooltip')) {
152
          // Get new output position
153
          var newPosition = this._getSliderOutputPosition();
154
155
          // Set output position
156
          this.output.style['left'] = newPosition.position;
157
        }
158
      }
159
160
      this.emit('bulmaslider:ready', this.element.value);
161
    }
162
  }, {
163
    key: '_findOutputForSlider',
164
    value: function _findOutputForSlider() {
165
      var _this2 = this;
166
167
      var result = null;
168
      var outputs = document.getElementsByTagName('output') || [];
169
170
      Array.from(outputs).forEach(function (output) {
171
        if (output.htmlFor == _this2.element.getAttribute('id')) {
172
          result = output;
173
          return true;
174
        }
175
      });
176
      return result;
177
    }
178
  }, {
179
    key: '_getSliderOutputPosition',
180
    value: function _getSliderOutputPosition() {
181
      // Update output position
182
      var newPlace, minValue;
183
184
      var style = window.getComputedStyle(this.element, null);
185
      // Measure width of range input
186
      var sliderWidth = parseInt(style.getPropertyValue('width'), 10);
187
188
      // Figure out placement percentage between left and right of input
189
      if (!this.element.getAttribute('min')) {
190
        minValue = 0;
191
      } else {
192
        minValue = this.element.getAttribute('min');
193
      }
194
      var newPoint = (this.element.value - minValue) / (this.element.getAttribute('max') - minValue);
195
196
      // Prevent bubble from going beyond left or right (unsupported browsers)
197
      if (newPoint < 0) {
198
        newPlace = 0;
199
      } else if (newPoint > 1) {
200
        newPlace = sliderWidth;
201
      } else {
202
        newPlace = sliderWidth * newPoint;
203
      }
204
205
      return {
206
        'position': newPlace + 'px'
207
      };
208
    }
209
210
    /**
211
     * Bind all events
212
     * @method _bindEvents
213
     * @return {void}
214
     */
215
216
  }, {
217
    key: '_bindEvents',
218
    value: function _bindEvents() {
219
      if (this.output) {
220
        // Add event listener to update output when slider value change
221
        this.element.addEventListener('input', this.onSliderInput, false);
222
      }
223
    }
224
  }, {
225
    key: 'onSliderInput',
226
    value: function onSliderInput(e) {
227
      e.preventDefault();
228
229
      if (this.element.classList.contains('has-output-tooltip')) {
230
        // Get new output position
231
        var newPosition = this._getSliderOutputPosition();
232
233
        // Set output position
234
        this.output.style['left'] = newPosition.position;
235
      }
236
237
      // Check for prefix and postfix
238
      var prefix = this.output.hasAttribute('data-prefix') ? this.output.getAttribute('data-prefix') : '';
239
      var postfix = this.output.hasAttribute('data-postfix') ? this.output.getAttribute('data-postfix') : '';
240
241
      // Update output with slider value
242
      this.output.value = prefix + this.element.value + postfix;
243
244
      this.emit('bulmaslider:ready', this.element.value);
245
    }
246
  }], [{
247
    key: 'attach',
248
    value: function attach() {
249
      var _this3 = this;
250
251
      var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'input[type="range"].slider';
252
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
253
254
      var instances = new Array();
255
256
      var elements = isString(selector) ? document.querySelectorAll(selector) : Array.isArray(selector) ? selector : [selector];
257
      elements.forEach(function (element) {
258
        if (typeof element[_this3.constructor.name] === 'undefined') {
259
          var instance = new bulmaSlider(element, options);
260
          element[_this3.constructor.name] = instance;
261
          instances.push(instance);
262
        } else {
263
          instances.push(element[_this3.constructor.name]);
264
        }
265
      });
266
267
      return instances;
268
    }
269
  }]);
270
271
  return bulmaSlider;
272
}(__WEBPACK_IMPORTED_MODULE_0__events__["a" /* default */]);
273
274
/* harmony default export */ __webpack_exports__["default"] = (bulmaSlider);
275
276
/***/ }),
277
/* 1 */
278
/***/ (function(module, __webpack_exports__, __webpack_require__) {
279
280
"use strict";
281
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
282
283
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
284
285
var EventEmitter = function () {
286
  function EventEmitter() {
287
    var listeners = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
288
289
    _classCallCheck(this, EventEmitter);
290
291
    this._listeners = new Map(listeners);
292
    this._middlewares = new Map();
293
  }
294
295
  _createClass(EventEmitter, [{
296
    key: "listenerCount",
297
    value: function listenerCount(eventName) {
298
      if (!this._listeners.has(eventName)) {
299
        return 0;
300
      }
301
302
      var eventListeners = this._listeners.get(eventName);
303
      return eventListeners.length;
304
    }
305
  }, {
306
    key: "removeListeners",
307
    value: function removeListeners() {
308
      var _this = this;
309
310
      var eventName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
311
      var middleware = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
312
313
      if (eventName !== null) {
314
        if (Array.isArray(eventName)) {
315
          name.forEach(function (e) {
316
            return _this.removeListeners(e, middleware);
317
          });
318
        } else {
319
          this._listeners.delete(eventName);
320
321
          if (middleware) {
322
            this.removeMiddleware(eventName);
323
          }
324
        }
325
      } else {
326
        this._listeners = new Map();
327
      }
328
    }
329
  }, {
330
    key: "middleware",
331
    value: function middleware(eventName, fn) {
332
      var _this2 = this;
333
334
      if (Array.isArray(eventName)) {
335
        name.forEach(function (e) {
336
          return _this2.middleware(e, fn);
337
        });
338
      } else {
339
        if (!Array.isArray(this._middlewares.get(eventName))) {
340
          this._middlewares.set(eventName, []);
341
        }
342
343
        this._middlewares.get(eventName).push(fn);
344
      }
345
    }
346
  }, {
347
    key: "removeMiddleware",
348
    value: function removeMiddleware() {
349
      var _this3 = this;
350
351
      var eventName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
352
353
      if (eventName !== null) {
354
        if (Array.isArray(eventName)) {
355
          name.forEach(function (e) {
356
            return _this3.removeMiddleware(e);
357
          });
358
        } else {
359
          this._middlewares.delete(eventName);
360
        }
361
      } else {
362
        this._middlewares = new Map();
363
      }
364
    }
365
  }, {
366
    key: "on",
367
    value: function on(name, callback) {
368
      var _this4 = this;
369
370
      var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
371
372
      if (Array.isArray(name)) {
373
        name.forEach(function (e) {
374
          return _this4.on(e, callback);
375
        });
376
      } else {
377
        name = name.toString();
378
        var split = name.split(/,|, | /);
379
380
        if (split.length > 1) {
381
          split.forEach(function (e) {
382
            return _this4.on(e, callback);
383
          });
384
        } else {
385
          if (!Array.isArray(this._listeners.get(name))) {
386
            this._listeners.set(name, []);
387
          }
388
389
          this._listeners.get(name).push({ once: once, callback: callback });
390
        }
391
      }
392
    }
393
  }, {
394
    key: "once",
395
    value: function once(name, callback) {
396
      this.on(name, callback, true);
397
    }
398
  }, {
399
    key: "emit",
400
    value: function emit(name, data) {
401
      var _this5 = this;
402
403
      var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
404
405
      name = name.toString();
406
      var listeners = this._listeners.get(name);
407
      var middlewares = null;
408
      var doneCount = 0;
409
      var execute = silent;
410
411
      if (Array.isArray(listeners)) {
412
        listeners.forEach(function (listener, index) {
413
          // Start Middleware checks unless we're doing a silent emit
414
          if (!silent) {
415
            middlewares = _this5._middlewares.get(name);
416
            // Check and execute Middleware
417
            if (Array.isArray(middlewares)) {
418
              middlewares.forEach(function (middleware) {
419
                middleware(data, function () {
420
                  var newData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
421
422
                  if (newData !== null) {
423
                    data = newData;
424
                  }
425
                  doneCount++;
426
                }, name);
427
              });
428
429
              if (doneCount >= middlewares.length) {
430
                execute = true;
431
              }
432
            } else {
433
              execute = true;
434
            }
435
          }
436
437
          // If Middleware checks have been passed, execute
438
          if (execute) {
439
            if (listener.once) {
440
              listeners[index] = null;
441
            }
442
            listener.callback(data);
443
          }
444
        });
445
446
        // Dirty way of removing used Events
447
        while (listeners.indexOf(null) !== -1) {
448
          listeners.splice(listeners.indexOf(null), 1);
449
        }
450
      }
451
    }
452
  }]);
453
454
  return EventEmitter;
455
}();
456
457
/* harmony default export */ __webpack_exports__["a"] = (EventEmitter);
458
459
/***/ })
460
/******/ ])["default"];
461
});