jquery-asRange.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /**
  2. * asRange v0.3.4
  3. * https://github.com/amazingSurge/jquery-asRange
  4. *
  5. * Copyright (c) amazingSurge
  6. * Released under the LGPL-3.0 license
  7. */
  8. (function(global, factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(['jquery'], factory);
  11. } else if (typeof exports !== 'undefined') {
  12. factory(require('jquery'));
  13. } else {
  14. var mod = {
  15. exports: {}
  16. };
  17. factory(global.jQuery);
  18. global.jqueryAsRangeEs = mod.exports;
  19. }
  20. })(this, function(_jquery) {
  21. 'use strict';
  22. var _jquery2 = _interopRequireDefault(_jquery);
  23. function _interopRequireDefault(obj) {
  24. return obj && obj.__esModule
  25. ? obj
  26. : {
  27. default: obj
  28. };
  29. }
  30. var _typeof =
  31. typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
  32. ? function(obj) {
  33. return typeof obj;
  34. }
  35. : function(obj) {
  36. return obj &&
  37. typeof Symbol === 'function' &&
  38. obj.constructor === Symbol &&
  39. obj !== Symbol.prototype
  40. ? 'symbol'
  41. : typeof obj;
  42. };
  43. function _classCallCheck(instance, Constructor) {
  44. if (!(instance instanceof Constructor)) {
  45. throw new TypeError('Cannot call a class as a function');
  46. }
  47. }
  48. var _createClass = (function() {
  49. function defineProperties(target, props) {
  50. for (var i = 0; i < props.length; i++) {
  51. var descriptor = props[i];
  52. descriptor.enumerable = descriptor.enumerable || false;
  53. descriptor.configurable = true;
  54. if ('value' in descriptor) descriptor.writable = true;
  55. Object.defineProperty(target, descriptor.key, descriptor);
  56. }
  57. }
  58. return function(Constructor, protoProps, staticProps) {
  59. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  60. if (staticProps) defineProperties(Constructor, staticProps);
  61. return Constructor;
  62. };
  63. })();
  64. var DEFAULTS = {
  65. namespace: 'asRange',
  66. skin: null,
  67. max: 100,
  68. min: 0,
  69. value: null,
  70. step: 10,
  71. limit: true,
  72. range: false,
  73. direction: 'h', // 'v' or 'h'
  74. keyboard: true,
  75. replaceFirst: false, // false, 'inherit', {'inherit': 'default'}
  76. tip: true,
  77. scale: true,
  78. format: function format(value) {
  79. return value;
  80. }
  81. };
  82. function getEventObject(event) {
  83. var e = event.originalEvent;
  84. if (e.touches && e.touches.length && e.touches[0]) {
  85. e = e.touches[0];
  86. }
  87. return e;
  88. }
  89. var Pointer = (function() {
  90. function Pointer($element, id, parent) {
  91. _classCallCheck(this, Pointer);
  92. this.$element = $element;
  93. this.uid = id;
  94. this.parent = parent;
  95. this.options = _jquery2.default.extend(true, {}, this.parent.options);
  96. this.direction = this.options.direction;
  97. this.value = null;
  98. this.classes = {
  99. active: this.parent.namespace + '-pointer_active'
  100. };
  101. }
  102. _createClass(Pointer, [
  103. {
  104. key: 'mousedown',
  105. value: function mousedown(event) {
  106. var axis = this.parent.direction.axis;
  107. var position = this.parent.direction.position;
  108. var offset = this.parent.$wrap.offset();
  109. this.$element.trigger(this.parent.namespace + '::moveStart', this);
  110. this.data = {};
  111. this.data.start = event[axis];
  112. this.data.position = event[axis] - offset[position];
  113. var value = this.parent.getValueFromPosition(this.data.position);
  114. this.set(value);
  115. _jquery2.default.each(this.parent.pointer, function(i, p) {
  116. p.deactive();
  117. });
  118. this.active();
  119. this.mousemove = function(event) {
  120. var eventObj = getEventObject(event);
  121. var value = this.parent.getValueFromPosition(
  122. this.data.position +
  123. (eventObj[axis] || this.data.start) -
  124. this.data.start
  125. );
  126. this.set(value);
  127. event.preventDefault();
  128. return false;
  129. };
  130. this.mouseup = function() {
  131. (0, _jquery2.default)(document).off(
  132. '.asRange mousemove.asRange touchend.asRange mouseup.asRange touchcancel.asRange'
  133. );
  134. this.$element.trigger(this.parent.namespace + '::moveEnd', this);
  135. return false;
  136. };
  137. (0, _jquery2.default)(document)
  138. .on(
  139. 'touchmove.asRange mousemove.asRange',
  140. _jquery2.default.proxy(this.mousemove, this)
  141. )
  142. .on(
  143. 'touchend.asRange mouseup.asRange',
  144. _jquery2.default.proxy(this.mouseup, this)
  145. );
  146. return false;
  147. }
  148. },
  149. {
  150. key: 'active',
  151. value: function active() {
  152. this.$element.addClass(this.classes.active);
  153. }
  154. },
  155. {
  156. key: 'deactive',
  157. value: function deactive() {
  158. this.$element.removeClass(this.classes.active);
  159. }
  160. },
  161. {
  162. key: 'set',
  163. value: function set(value) {
  164. if (this.value === value) {
  165. return;
  166. }
  167. if (this.parent.step) {
  168. value = this.matchStep(value);
  169. }
  170. if (this.options.limit === true) {
  171. value = this.matchLimit(value);
  172. } else {
  173. if (value <= this.parent.min) {
  174. value = this.parent.min;
  175. }
  176. if (value >= this.parent.max) {
  177. value = this.parent.max;
  178. }
  179. }
  180. this.value = value;
  181. this.updatePosition();
  182. this.$element.focus();
  183. this.$element.trigger(this.parent.namespace + '::move', this);
  184. }
  185. },
  186. {
  187. key: 'updatePosition',
  188. value: function updatePosition() {
  189. var position = {};
  190. position[this.parent.direction.position] = this.getPercent() + '%';
  191. this.$element.css(position);
  192. }
  193. },
  194. {
  195. key: 'getPercent',
  196. value: function getPercent() {
  197. return (this.value - this.parent.min) / this.parent.interval * 100;
  198. }
  199. },
  200. {
  201. key: 'get',
  202. value: function get() {
  203. return this.value;
  204. }
  205. },
  206. {
  207. key: 'matchStep',
  208. value: function matchStep(value) {
  209. var step = this.parent.step;
  210. var decimal = step.toString().split('.')[1];
  211. value = Math.round(value / step) * step;
  212. if (decimal) {
  213. value = value.toFixed(decimal.length);
  214. }
  215. return parseFloat(value);
  216. }
  217. },
  218. {
  219. key: 'matchLimit',
  220. value: function matchLimit(value) {
  221. var left = void 0;
  222. var right = void 0;
  223. var pointer = this.parent.pointer;
  224. if (this.uid === 1) {
  225. left = this.parent.min;
  226. } else {
  227. left = pointer[this.uid - 2].value;
  228. }
  229. if (pointer[this.uid] && pointer[this.uid].value !== null) {
  230. right = pointer[this.uid].value;
  231. } else {
  232. right = this.parent.max;
  233. }
  234. if (value <= left) {
  235. value = left;
  236. }
  237. if (value >= right) {
  238. value = right;
  239. }
  240. return value;
  241. }
  242. },
  243. {
  244. key: 'destroy',
  245. value: function destroy() {
  246. this.$element.off('.asRange');
  247. this.$element.remove();
  248. }
  249. }
  250. ]);
  251. return Pointer;
  252. })();
  253. var scale = {
  254. defaults: {
  255. scale: {
  256. valuesNumber: 3,
  257. gap: 1,
  258. grid: 5
  259. }
  260. },
  261. init: function init(instance) {
  262. var opts = _jquery2.default.extend(
  263. {},
  264. this.defaults,
  265. instance.options.scale
  266. );
  267. var scale = opts.scale;
  268. scale.values = [];
  269. scale.values.push(instance.min);
  270. var part = (instance.max - instance.min) / (scale.valuesNumber - 1);
  271. for (var j = 1; j <= scale.valuesNumber - 2; j++) {
  272. scale.values.push(part * j);
  273. }
  274. scale.values.push(instance.max);
  275. var classes = {
  276. scale: instance.namespace + '-scale',
  277. lines: instance.namespace + '-scale-lines',
  278. grid: instance.namespace + '-scale-grid',
  279. inlineGrid: instance.namespace + '-scale-inlineGrid',
  280. values: instance.namespace + '-scale-values'
  281. };
  282. var len = scale.values.length;
  283. var num =
  284. ((scale.grid - 1) * (scale.gap + 1) + scale.gap) * (len - 1) + len;
  285. var perOfGrid = 100 / (num - 1);
  286. var perOfValue = 100 / (len - 1);
  287. this.$scale = (0, _jquery2.default)('<div></div>').addClass(
  288. classes.scale
  289. );
  290. this.$lines = (0, _jquery2.default)('<ul></ul>').addClass(classes.lines);
  291. this.$values = (0, _jquery2.default)('<ul></ul>').addClass(
  292. classes.values
  293. );
  294. for (var i = 0; i < num; i++) {
  295. var $list = void 0;
  296. if (i === 0 || i === num || i % ((num - 1) / (len - 1)) === 0) {
  297. $list = (0, _jquery2.default)(
  298. '<li class="' + classes.grid + '"></li>'
  299. );
  300. } else if (i % scale.grid === 0) {
  301. $list = (0, _jquery2.default)(
  302. '<li class="' + classes.inlineGrid + '"></li>'
  303. );
  304. } else {
  305. $list = (0, _jquery2.default)('<li></li>');
  306. }
  307. // position scale
  308. $list
  309. .css({
  310. left: perOfGrid * i + '%'
  311. })
  312. .appendTo(this.$lines);
  313. }
  314. for (var v = 0; v < len; v++) {
  315. // position value
  316. (0, _jquery2.default)('<li><span>' + scale.values[v] + '</span></li>')
  317. .css({
  318. left: perOfValue * v + '%'
  319. })
  320. .appendTo(this.$values);
  321. }
  322. this.$lines.add(this.$values).appendTo(this.$scale);
  323. this.$scale.appendTo(instance.$wrap);
  324. },
  325. update: function update(instance) {
  326. this.$scale.remove();
  327. this.init(instance);
  328. }
  329. };
  330. var selected = {
  331. defaults: {},
  332. init: function init(instance) {
  333. var _this = this;
  334. this.$arrow = (0, _jquery2.default)('<span></span>').appendTo(
  335. instance.$wrap
  336. );
  337. this.$arrow.addClass(instance.namespace + '-selected');
  338. if (instance.options.range === false) {
  339. instance.p1.$element.on(instance.namespace + '::move', function(
  340. e,
  341. pointer
  342. ) {
  343. _this.$arrow.css({
  344. left: 0,
  345. width: pointer.getPercent() + '%'
  346. });
  347. });
  348. }
  349. if (instance.options.range === true) {
  350. var onUpdate = function onUpdate() {
  351. var width = instance.p2.getPercent() - instance.p1.getPercent();
  352. var left = void 0;
  353. if (width >= 0) {
  354. left = instance.p1.getPercent();
  355. } else {
  356. width = -width;
  357. left = instance.p2.getPercent();
  358. }
  359. _this.$arrow.css({
  360. left: left + '%',
  361. width: width + '%'
  362. });
  363. };
  364. instance.p1.$element.on(instance.namespace + '::move', onUpdate);
  365. instance.p2.$element.on(instance.namespace + '::move', onUpdate);
  366. }
  367. }
  368. };
  369. var tip = {
  370. defaults: {
  371. active: 'always' // 'always' 'onMove'
  372. },
  373. init: function init(instance) {
  374. var that = this;
  375. var opts = _jquery2.default.extend(
  376. {},
  377. this.defaults,
  378. instance.options.tip
  379. );
  380. this.opts = opts;
  381. this.classes = {
  382. tip: instance.namespace + '-tip',
  383. show: instance.namespace + '-tip-show'
  384. };
  385. _jquery2.default.each(instance.pointer, function(i, p) {
  386. var $tip = (0, _jquery2.default)('<span></span>').appendTo(
  387. instance.pointer[i].$element
  388. );
  389. $tip.addClass(that.classes.tip);
  390. if (that.opts.active === 'onMove') {
  391. $tip.css({
  392. display: 'none'
  393. });
  394. p.$element
  395. .on(instance.namespace + '::moveEnd', function() {
  396. that.hide($tip);
  397. return false;
  398. })
  399. .on(instance.namespace + '::moveStart', function() {
  400. that.show($tip);
  401. return false;
  402. });
  403. }
  404. p.$element.on(instance.namespace + '::move', function() {
  405. var value = void 0;
  406. if (instance.options.range) {
  407. value = instance.get()[i];
  408. } else {
  409. value = instance.get();
  410. }
  411. if (typeof instance.options.format === 'function') {
  412. if (instance.options.replaceFirst && typeof value !== 'number') {
  413. if (typeof instance.options.replaceFirst === 'string') {
  414. value = instance.options.replaceFirst;
  415. }
  416. if (_typeof(instance.options.replaceFirst) === 'object') {
  417. for (var key in instance.options.replaceFirst) {
  418. if (
  419. Object.hasOwnProperty(instance.options.replaceFirst, key)
  420. ) {
  421. value = instance.options.replaceFirst[key];
  422. }
  423. }
  424. }
  425. } else {
  426. value = instance.options.format(value);
  427. }
  428. }
  429. $tip.text(value);
  430. return false;
  431. });
  432. });
  433. },
  434. show: function show($tip) {
  435. $tip.addClass(this.classes.show);
  436. $tip.css({
  437. display: 'block'
  438. });
  439. },
  440. hide: function hide($tip) {
  441. $tip.removeClass(this.classes.show);
  442. $tip.css({
  443. display: 'none'
  444. });
  445. }
  446. };
  447. var keyboard = function keyboard() {
  448. var $doc = (0, _jquery2.default)(document);
  449. $doc.on('asRange::ready', function(event, instance) {
  450. var step = void 0;
  451. var keyboard = {
  452. keys: {
  453. UP: 38,
  454. DOWN: 40,
  455. LEFT: 37,
  456. RIGHT: 39,
  457. RETURN: 13,
  458. ESCAPE: 27,
  459. BACKSPACE: 8,
  460. SPACE: 32
  461. },
  462. map: {},
  463. bound: false,
  464. press: function press(e) {
  465. /*eslint consistent-return: "off"*/
  466. var key = e.keyCode || e.which;
  467. if (key in keyboard.map && typeof keyboard.map[key] === 'function') {
  468. keyboard.map[key](e);
  469. return false;
  470. }
  471. },
  472. attach: function attach(map) {
  473. var key = void 0;
  474. var up = void 0;
  475. for (key in map) {
  476. if (map.hasOwnProperty(key)) {
  477. up = key.toUpperCase();
  478. if (up in keyboard.keys) {
  479. keyboard.map[keyboard.keys[up]] = map[key];
  480. } else {
  481. keyboard.map[up] = map[key];
  482. }
  483. }
  484. }
  485. if (!keyboard.bound) {
  486. keyboard.bound = true;
  487. $doc.bind('keydown', keyboard.press);
  488. }
  489. },
  490. detach: function detach() {
  491. keyboard.bound = false;
  492. keyboard.map = {};
  493. $doc.unbind('keydown', keyboard.press);
  494. }
  495. };
  496. if (instance.options.keyboard === true) {
  497. _jquery2.default.each(instance.pointer, function(i, p) {
  498. if (instance.options.step) {
  499. step = instance.options.step;
  500. } else {
  501. step = 1;
  502. }
  503. var left = function left() {
  504. var value = p.value;
  505. p.set(value - step);
  506. };
  507. var right = function right() {
  508. var value = p.value;
  509. p.set(value + step);
  510. };
  511. p.$element
  512. .attr('tabindex', '0')
  513. .on('focus', function() {
  514. keyboard.attach({
  515. left: left,
  516. right: right
  517. });
  518. return false;
  519. })
  520. .on('blur', function() {
  521. keyboard.detach();
  522. return false;
  523. });
  524. });
  525. }
  526. });
  527. };
  528. var components = {};
  529. /**
  530. * Plugin constructor
  531. **/
  532. var asRange = (function() {
  533. function asRange(element, options) {
  534. var _this2 = this;
  535. _classCallCheck(this, asRange);
  536. var metas = {};
  537. this.element = element;
  538. this.$element = (0, _jquery2.default)(element);
  539. if (this.$element.is('input')) {
  540. var value = this.$element.val();
  541. if (typeof value === 'string') {
  542. metas.value = value.split(',');
  543. }
  544. _jquery2.default.each(['min', 'max', 'step'], function(index, key) {
  545. var val = parseFloat(_this2.$element.attr(key));
  546. if (!isNaN(val)) {
  547. metas[key] = val;
  548. }
  549. });
  550. this.$element.css({
  551. display: 'none'
  552. });
  553. this.$wrap = (0, _jquery2.default)('<div></div>');
  554. this.$element.after(this.$wrap);
  555. } else {
  556. this.$wrap = this.$element;
  557. }
  558. this.options = _jquery2.default.extend(
  559. {},
  560. DEFAULTS,
  561. options,
  562. this.$element.data(),
  563. metas
  564. );
  565. this.namespace = this.options.namespace;
  566. this.components = _jquery2.default.extend(true, {}, components);
  567. if (this.options.range) {
  568. this.options.replaceFirst = false;
  569. }
  570. // public properties
  571. this.value = this.options.value;
  572. if (this.value === null) {
  573. this.value = this.options.min;
  574. }
  575. if (!this.options.range) {
  576. if (_jquery2.default.isArray(this.value)) {
  577. this.value = this.value[0];
  578. }
  579. } else if (!_jquery2.default.isArray(this.value)) {
  580. this.value = [this.value, this.value];
  581. } else if (this.value.length === 1) {
  582. this.value[1] = this.value[0];
  583. }
  584. this.min = this.options.min;
  585. this.max = this.options.max;
  586. this.step = this.options.step;
  587. this.interval = this.max - this.min;
  588. // flag
  589. this.initialized = false;
  590. this.updating = false;
  591. this.disabled = false;
  592. if (this.options.direction === 'v') {
  593. this.direction = {
  594. axis: 'pageY',
  595. position: 'top'
  596. };
  597. } else {
  598. this.direction = {
  599. axis: 'pageX',
  600. position: 'left'
  601. };
  602. }
  603. this.$wrap.addClass(this.namespace);
  604. if (this.options.skin) {
  605. this.$wrap.addClass(this.namespace + '_' + this.options.skin);
  606. }
  607. if (this.max < this.min || this.step >= this.interval) {
  608. throw new Error('error options about max min step');
  609. }
  610. this.init();
  611. }
  612. _createClass(
  613. asRange,
  614. [
  615. {
  616. key: 'init',
  617. value: function init() {
  618. this.$wrap.append('<div class="' + this.namespace + '-bar" />');
  619. // build pointers
  620. this.buildPointers();
  621. // initial components
  622. this.components.selected.init(this);
  623. if (this.options.tip !== false) {
  624. this.components.tip.init(this);
  625. }
  626. if (this.options.scale !== false) {
  627. this.components.scale.init(this);
  628. }
  629. // initial pointer value
  630. this.set(this.value);
  631. // Bind events
  632. this.bindEvents();
  633. this._trigger('ready');
  634. this.initialized = true;
  635. }
  636. },
  637. {
  638. key: '_trigger',
  639. value: function _trigger(eventType) {
  640. for (
  641. var _len = arguments.length,
  642. params = Array(_len > 1 ? _len - 1 : 0),
  643. _key = 1;
  644. _key < _len;
  645. _key++
  646. ) {
  647. params[_key - 1] = arguments[_key];
  648. }
  649. var data = [this].concat(params);
  650. // event
  651. this.$element.trigger(this.namespace + ('::' + eventType), data);
  652. // callback
  653. eventType = eventType.replace(/\b\w+\b/g, function(word) {
  654. return word.substring(0, 1).toUpperCase() + word.substring(1);
  655. });
  656. var onFunction = 'on' + eventType;
  657. if (typeof this.options[onFunction] === 'function') {
  658. this.options[onFunction].apply(this, params);
  659. }
  660. }
  661. },
  662. {
  663. key: 'buildPointers',
  664. value: function buildPointers() {
  665. this.pointer = [];
  666. var pointerCount = 1;
  667. if (this.options.range) {
  668. pointerCount = 2;
  669. }
  670. for (var i = 1; i <= pointerCount; i++) {
  671. var $pointer = (0, _jquery2.default)(
  672. '<div class="' +
  673. this.namespace +
  674. '-pointer ' +
  675. this.namespace +
  676. '-pointer-' +
  677. i +
  678. '"></div>'
  679. ).appendTo(this.$wrap);
  680. var p = new Pointer($pointer, i, this);
  681. this.pointer.push(p);
  682. }
  683. // alias of pointer
  684. this.p1 = this.pointer[0];
  685. if (this.options.range) {
  686. this.p2 = this.pointer[1];
  687. }
  688. }
  689. },
  690. {
  691. key: 'bindEvents',
  692. value: function bindEvents() {
  693. var _this3 = this;
  694. var that = this;
  695. this.$wrap.on('touchstart.asRange mousedown.asRange', function(
  696. event
  697. ) {
  698. /*eslint consistent-return: "off"*/
  699. if (that.disabled === true) {
  700. return;
  701. }
  702. event = getEventObject(event);
  703. var rightclick = event.which
  704. ? event.which === 3
  705. : event.button === 2;
  706. if (rightclick) {
  707. return false;
  708. }
  709. var offset = that.$wrap.offset();
  710. var start =
  711. event[that.direction.axis] - offset[that.direction.position];
  712. var p = that.getAdjacentPointer(start);
  713. p.mousedown(event);
  714. return false;
  715. });
  716. if (this.$element.is('input')) {
  717. this.$element.on(this.namespace + '::change', function() {
  718. var value = _this3.get();
  719. _this3.$element.val(value);
  720. });
  721. }
  722. _jquery2.default.each(this.pointer, function(i, p) {
  723. p.$element.on(_this3.namespace + '::move', function() {
  724. that.value = that.get();
  725. if (!that.initialized || that.updating) {
  726. return false;
  727. }
  728. that._trigger('change', that.value);
  729. return false;
  730. });
  731. });
  732. }
  733. },
  734. {
  735. key: 'getValueFromPosition',
  736. value: function getValueFromPosition(px) {
  737. if (px > 0) {
  738. return this.min + px / this.getLength() * this.interval;
  739. }
  740. return 0;
  741. }
  742. },
  743. {
  744. key: 'getAdjacentPointer',
  745. value: function getAdjacentPointer(start) {
  746. var value = this.getValueFromPosition(start);
  747. if (this.options.range) {
  748. var p1 = this.p1.value;
  749. var p2 = this.p2.value;
  750. var diff = Math.abs(p1 - p2);
  751. if (p1 <= p2) {
  752. if (value > p1 + diff / 2) {
  753. return this.p2;
  754. }
  755. return this.p1;
  756. }
  757. if (value > p2 + diff / 2) {
  758. return this.p1;
  759. }
  760. return this.p2;
  761. }
  762. return this.p1;
  763. }
  764. },
  765. {
  766. key: 'getLength',
  767. value: function getLength() {
  768. if (this.options.direction === 'v') {
  769. return this.$wrap.height();
  770. }
  771. return this.$wrap.width();
  772. }
  773. },
  774. {
  775. key: 'update',
  776. value: function update(options) {
  777. var _this4 = this;
  778. this.updating = true;
  779. _jquery2.default.each(
  780. ['max', 'min', 'step', 'limit', 'value'],
  781. function(key, value) {
  782. if (options[value]) {
  783. _this4[value] = options[value];
  784. }
  785. }
  786. );
  787. if (options.max || options.min) {
  788. this.setInterval(options.min, options.max);
  789. }
  790. if (!options.value) {
  791. this.value = options.min;
  792. }
  793. _jquery2.default.each(this.components, function(key, value) {
  794. if (typeof value.update === 'function') {
  795. value.update(_this4);
  796. }
  797. });
  798. this.set(this.value);
  799. this._trigger('update');
  800. this.updating = false;
  801. }
  802. },
  803. {
  804. key: 'get',
  805. value: function get() {
  806. var value = [];
  807. _jquery2.default.each(this.pointer, function(i, p) {
  808. value[i] = p.get();
  809. });
  810. if (this.options.range) {
  811. return value;
  812. }
  813. if (value[0] === this.options.min) {
  814. if (typeof this.options.replaceFirst === 'string') {
  815. value[0] = this.options.replaceFirst;
  816. }
  817. if (_typeof(this.options.replaceFirst) === 'object') {
  818. for (var key in this.options.replaceFirst) {
  819. if (Object.hasOwnProperty(this.options.replaceFirst, key)) {
  820. value[0] = key;
  821. }
  822. }
  823. }
  824. }
  825. return value[0];
  826. }
  827. },
  828. {
  829. key: 'set',
  830. value: function set(value) {
  831. if (this.options.range) {
  832. if (typeof value === 'number') {
  833. value = [value];
  834. }
  835. if (!_jquery2.default.isArray(value)) {
  836. return;
  837. }
  838. _jquery2.default.each(this.pointer, function(i, p) {
  839. p.set(value[i]);
  840. });
  841. } else {
  842. this.p1.set(value);
  843. }
  844. this.value = value;
  845. }
  846. },
  847. {
  848. key: 'val',
  849. value: function val(value) {
  850. if (value) {
  851. this.set(value);
  852. return this;
  853. }
  854. return this.get();
  855. }
  856. },
  857. {
  858. key: 'setInterval',
  859. value: function setInterval(start, end) {
  860. this.min = start;
  861. this.max = end;
  862. this.interval = end - start;
  863. }
  864. },
  865. {
  866. key: 'enable',
  867. value: function enable() {
  868. this.disabled = false;
  869. this.$wrap.removeClass(this.namespace + '_disabled');
  870. this._trigger('enable');
  871. return this;
  872. }
  873. },
  874. {
  875. key: 'disable',
  876. value: function disable() {
  877. this.disabled = true;
  878. this.$wrap.addClass(this.namespace + '_disabled');
  879. this._trigger('disable');
  880. return this;
  881. }
  882. },
  883. {
  884. key: 'destroy',
  885. value: function destroy() {
  886. _jquery2.default.each(this.pointer, function(i, p) {
  887. p.destroy();
  888. });
  889. this.$wrap.destroy();
  890. this._trigger('destroy');
  891. }
  892. }
  893. ],
  894. [
  895. {
  896. key: 'registerComponent',
  897. value: function registerComponent(component, methods) {
  898. components[component] = methods;
  899. }
  900. },
  901. {
  902. key: 'setDefaults',
  903. value: function setDefaults(options) {
  904. _jquery2.default.extend(
  905. DEFAULTS,
  906. _jquery2.default.isPlainObject(options) && options
  907. );
  908. }
  909. }
  910. ]
  911. );
  912. return asRange;
  913. })();
  914. asRange.registerComponent('scale', scale);
  915. asRange.registerComponent('selected', selected);
  916. asRange.registerComponent('tip', tip);
  917. keyboard();
  918. var info = {
  919. version: '0.3.4'
  920. };
  921. var NAMESPACE = 'asRange';
  922. var OtherAsRange = _jquery2.default.fn.asRange;
  923. var jQueryAsRange = function jQueryAsRange(options) {
  924. for (
  925. var _len2 = arguments.length,
  926. args = Array(_len2 > 1 ? _len2 - 1 : 0),
  927. _key2 = 1;
  928. _key2 < _len2;
  929. _key2++
  930. ) {
  931. args[_key2 - 1] = arguments[_key2];
  932. }
  933. if (typeof options === 'string') {
  934. var method = options;
  935. if (/^_/.test(method)) {
  936. return false;
  937. } else if (
  938. /^(get)$/.test(method) ||
  939. (method === 'val' && args.length === 0)
  940. ) {
  941. var instance = this.first().data(NAMESPACE);
  942. if (instance && typeof instance[method] === 'function') {
  943. return instance[method].apply(instance, args);
  944. }
  945. } else {
  946. return this.each(function() {
  947. var instance = _jquery2.default.data(this, NAMESPACE);
  948. if (instance && typeof instance[method] === 'function') {
  949. instance[method].apply(instance, args);
  950. }
  951. });
  952. }
  953. }
  954. return this.each(function() {
  955. if (!(0, _jquery2.default)(this).data(NAMESPACE)) {
  956. (0, _jquery2.default)(this).data(NAMESPACE, new asRange(this, options));
  957. }
  958. });
  959. };
  960. _jquery2.default.fn.asRange = jQueryAsRange;
  961. _jquery2.default.asRange = _jquery2.default.extend(
  962. {
  963. setDefaults: asRange.setDefaults,
  964. noConflict: function noConflict() {
  965. _jquery2.default.fn.asRange = OtherAsRange;
  966. return jQueryAsRange;
  967. }
  968. },
  969. info
  970. );
  971. });