No Description

ons-range.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
  2. import _setImmediate from 'babel-runtime/core-js/set-immediate';
  3. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  4. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  5. import _createClass from 'babel-runtime/helpers/createClass';
  6. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  7. import _get from 'babel-runtime/helpers/get';
  8. import _inherits from 'babel-runtime/helpers/inherits';
  9. /*
  10. Copyright 2013-2015 ASIAL CORPORATION
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. */
  21. import onsElements from '../ons/elements';
  22. import util from '../ons/util';
  23. import BaseInputElement from './base/base-input';
  24. var scheme = {
  25. '': 'range--*',
  26. '.range__input': 'range--*__input',
  27. '.range__focus-ring': 'range--*__focus-ring'
  28. };
  29. var activeClassToken = 'range__input--active';
  30. /**
  31. * @element ons-range
  32. * @category form
  33. * @modifier material
  34. * [en]Material Design slider[/en]
  35. * [ja][/ja]
  36. * @description
  37. * [en]
  38. * Range input component. Used to display a draggable slider.
  39. *
  40. * Works very similar to the `<input type="range">` element.
  41. * [/en]
  42. * [ja][/ja]
  43. * @codepen xZQomM
  44. * @tutorial vanilla/Reference/range
  45. * @guide theming.html#modifiers [en]More details about the `modifier` attribute[/en][ja]modifier属性の使い方[/ja]
  46. * @seealso ons-input
  47. * [en]The `<ons-input>` component is used to display text inputs, radio buttons and checkboxes.[/en]
  48. * [ja][/ja]
  49. * @example
  50. * <ons-range value="20"></ons-range>
  51. * <ons-range modifier="material" value="10"></range>
  52. */
  53. var RangeElement = function (_BaseInputElement) {
  54. _inherits(RangeElement, _BaseInputElement);
  55. function RangeElement() {
  56. _classCallCheck(this, RangeElement);
  57. var _this = _possibleConstructorReturn(this, (RangeElement.__proto__ || _Object$getPrototypeOf(RangeElement)).call(this));
  58. _this._onMouseDown = _this._onMouseDown.bind(_this);
  59. _this._onMouseUp = _this._onMouseUp.bind(_this);
  60. _this._onTouchStart = _this._onTouchStart.bind(_this);
  61. _this._onTouchEnd = _this._onTouchEnd.bind(_this);
  62. _this._onInput = _this._update.bind(_this);
  63. _this._onDragstart = _this._onDragstart.bind(_this);
  64. _this._onDragend = _this._onDragend.bind(_this);
  65. return _this;
  66. }
  67. _createClass(RangeElement, [{
  68. key: '_compile',
  69. value: function _compile() {
  70. _get(RangeElement.prototype.__proto__ || _Object$getPrototypeOf(RangeElement.prototype), '_compile', this).call(this);
  71. this._updateDisabled(this.hasAttribute('disabled'));
  72. }
  73. /* Inherited props */
  74. }, {
  75. key: '_update',
  76. value: function _update() {
  77. var input = this._input;
  78. var focusRing = this._focusRing;
  79. input.style.backgroundSize = 100 * this._ratio + '% 2px';
  80. focusRing.value = this.value;
  81. // NOTE: "_zero" attribute is used for CSS styling.
  82. if (input.min === '' && input.value === '0' || input.min === input.value) {
  83. input.setAttribute('_zero', '');
  84. } else {
  85. input.removeAttribute('_zero');
  86. }
  87. ['min', 'max'].forEach(function (attr) {
  88. return focusRing[attr] = input[attr];
  89. });
  90. }
  91. }, {
  92. key: '_onMouseDown',
  93. /* Own props */
  94. value: function _onMouseDown(e) {
  95. var _this2 = this;
  96. this._input.classList.add(activeClassToken);
  97. _setImmediate(function () {
  98. return _this2._input.focus();
  99. });
  100. }
  101. }, {
  102. key: '_onTouchStart',
  103. value: function _onTouchStart(e) {
  104. this._onMouseDown();
  105. }
  106. }, {
  107. key: '_onMouseUp',
  108. value: function _onMouseUp(e) {
  109. this._input.classList.remove(activeClassToken);
  110. }
  111. }, {
  112. key: '_onTouchEnd',
  113. value: function _onTouchEnd(e) {
  114. this._onMouseUp(e);
  115. }
  116. }, {
  117. key: '_onDragstart',
  118. value: function _onDragstart(e) {
  119. e.consumed = true;
  120. e.gesture.stopPropagation();
  121. this._input.classList.add(activeClassToken);
  122. this.addEventListener('drag', this._onDrag);
  123. }
  124. }, {
  125. key: '_onDrag',
  126. value: function _onDrag(e) {
  127. e.stopPropagation();
  128. }
  129. }, {
  130. key: '_onDragend',
  131. value: function _onDragend(e) {
  132. this._input.classList.remove(activeClassToken);
  133. this.removeEventListener('drag', this._onDrag);
  134. }
  135. }, {
  136. key: 'attributeChangedCallback',
  137. value: function attributeChangedCallback(name, last, current) {
  138. if (name === 'disabled') {
  139. this._updateDisabled(current);
  140. }
  141. _get(RangeElement.prototype.__proto__ || _Object$getPrototypeOf(RangeElement.prototype), 'attributeChangedCallback', this).call(this, name, last, current);
  142. }
  143. /**
  144. * @param {boolean} disabled
  145. */
  146. }, {
  147. key: '_updateDisabled',
  148. value: function _updateDisabled(disabled) {
  149. if (disabled) {
  150. this.classList.add('range--disabled');
  151. } else {
  152. this.classList.remove('range--disabled');
  153. }
  154. }
  155. }, {
  156. key: 'connectedCallback',
  157. value: function connectedCallback() {
  158. this._setupListeners(true);
  159. }
  160. }, {
  161. key: 'disconnectedCallback',
  162. value: function disconnectedCallback() {
  163. this._setupListeners(false);
  164. }
  165. }, {
  166. key: '_setupListeners',
  167. value: function _setupListeners(add) {
  168. var action = (add ? 'add' : 'remove') + 'EventListener';
  169. util[action](this, 'touchstart', this._onTouchStart, { passive: true });
  170. this[action]('mousedown', this._onMouseDown);
  171. this[action]('mouseup', this._onMouseUp);
  172. this[action]('touchend', this._onTouchEnd);
  173. this[action]('dragstart', this._onDragstart);
  174. this[action]('dragend', this._onDragend);
  175. this[action]('input', this._onInput);
  176. }
  177. /**
  178. * @attribute disabled
  179. * @description
  180. * [en]Whether the element is disabled or not.[/en]
  181. * [ja]無効化されている場合に`true`。[/ja]
  182. */
  183. /**
  184. * @property disabled
  185. * @type {Boolean}
  186. * @description
  187. * [en]Whether the element is disabled or not.[/en]
  188. * [ja]無効化されている場合に`true`。[/ja]
  189. */
  190. /**
  191. * @property value
  192. * @type {Number}
  193. * @description
  194. * [en]Current value.[/en]
  195. * [ja][/ja]
  196. */
  197. }, {
  198. key: '_scheme',
  199. get: function get() {
  200. return scheme;
  201. }
  202. }, {
  203. key: '_template',
  204. get: function get() {
  205. return '\n <input type="' + this.type + '" class="' + this._defaultClassName + '__input">\n <input type="range" class="range__focus-ring" tabIndex="-1">\n ';
  206. }
  207. }, {
  208. key: '_defaultClassName',
  209. get: function get() {
  210. return 'range';
  211. }
  212. }, {
  213. key: 'type',
  214. get: function get() {
  215. return 'range';
  216. }
  217. }, {
  218. key: '_focusRing',
  219. get: function get() {
  220. return this.children[1];
  221. }
  222. }, {
  223. key: '_ratio',
  224. get: function get() {
  225. // Returns the current ratio.
  226. var min = this._input.min === '' ? 0 : parseInt(this._input.min);
  227. var max = this._input.max === '' ? 100 : parseInt(this._input.max);
  228. return (this.value - min) / (max - min);
  229. }
  230. }], [{
  231. key: 'observedAttributes',
  232. get: function get() {
  233. return ['disabled'].concat(_toConsumableArray(BaseInputElement.observedAttributes));
  234. }
  235. }]);
  236. return RangeElement;
  237. }(BaseInputElement);
  238. export default RangeElement;
  239. onsElements.Range = RangeElement;
  240. customElements.define('ons-range', RangeElement);