Sin descripción

ons-progress-circular.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  5. import _inherits from 'babel-runtime/helpers/inherits';
  6. /*
  7. Copyright 2013-2015 ASIAL CORPORATION
  8. Licensed under the Apache License, Version 2.0 (the "License");
  9. you may not use this file except in compliance with the License.
  10. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. */
  18. import onsElements from '../ons/elements';
  19. import util from '../ons/util';
  20. import autoStyle from '../ons/autostyle';
  21. import ModifierUtil from '../ons/internal/modifier-util';
  22. import BaseElement from './base/base-element';
  23. import contentReady from '../ons/content-ready';
  24. var scheme = {
  25. '.progress-circular': 'progress-circular--*',
  26. '.progress-circular__background': 'progress-circular--*__background',
  27. '.progress-circular__primary': 'progress-circular--*__primary',
  28. '.progress-circular__secondary': 'progress-circular--*__secondary'
  29. };
  30. var template = util.createElement('\n <svg class="progress-circular">\n <circle class="progress-circular__background" />\n <circle class="progress-circular__secondary" cx="50%" cy="50%" r="40%" />\n <circle class="progress-circular__primary" cx="50%" cy="50%" r="40%" />\n </svg>\n');
  31. var INDET = 'indeterminate';
  32. /**
  33. * @element ons-progress-circular
  34. * @category visual
  35. * @description
  36. * [en]
  37. * This component displays a circular progress indicator. It can either be used to show how much of a task has been completed or to show a looping animation to indicate that an operation is currently running.
  38. * [/en]
  39. * [ja][/ja]
  40. * @codepen EVzMjR
  41. * @tutorial vanilla/Reference/progress-circular
  42. * @seealso ons-progress-bar
  43. * [en]The `<ons-progress-bar>` component displays a bar progress indicator.[/en]
  44. * [ja][/ja]
  45. * @example
  46. * <ons-progress-circular
  47. * value="55"
  48. * secondary-value="87">
  49. * </ons-progress-circular>
  50. *
  51. * <ons-progress-circular
  52. * indeterminate>
  53. * </ons-progress-circular>
  54. */
  55. var ProgressCircularElement = function (_BaseElement) {
  56. _inherits(ProgressCircularElement, _BaseElement);
  57. /**
  58. * @attribute modifier
  59. * @type {String}
  60. * @description
  61. * [en]Change the appearance of the progress indicator.[/en]
  62. * [ja]プログレスインジケータの見た目を変更します。[/ja]
  63. */
  64. /**
  65. * @attribute value
  66. * @type {Number}
  67. * @description
  68. * [en]Current progress. Should be a value between 0 and 100.[/en]
  69. * [ja]現在の進行状況の値を指定します。0から100の間の値を指定して下さい。[/ja]
  70. */
  71. /**
  72. * @attribute secondary-value
  73. * @type {Number}
  74. * @description
  75. * [en]Current secondary progress. Should be a value between 0 and 100.[/en]
  76. * [ja]現在の2番目の進行状況の値を指定します。0から100の間の値を指定して下さい。[/ja]
  77. */
  78. /**
  79. * @attribute indeterminate
  80. * @description
  81. * [en]If this attribute is set, an infinite looping animation will be shown.[/en]
  82. * [ja]この属性が設定された場合、ループするアニメーションが表示されます。[/ja]
  83. */
  84. function ProgressCircularElement() {
  85. _classCallCheck(this, ProgressCircularElement);
  86. var _this = _possibleConstructorReturn(this, (ProgressCircularElement.__proto__ || _Object$getPrototypeOf(ProgressCircularElement)).call(this));
  87. contentReady(_this, function () {
  88. return _this._compile();
  89. });
  90. return _this;
  91. }
  92. _createClass(ProgressCircularElement, [{
  93. key: 'attributeChangedCallback',
  94. value: function attributeChangedCallback(name, last, current) {
  95. if (name === 'modifier') {
  96. ModifierUtil.onModifierChanged(last, current, this, scheme);
  97. this.hasAttribute(INDET) && this._updateDeterminate();
  98. } else if (name === 'value' || name === 'secondary-value') {
  99. this._updateValue();
  100. } else if (name === INDET) {
  101. this._updateDeterminate();
  102. }
  103. }
  104. }, {
  105. key: '_updateDeterminate',
  106. value: function _updateDeterminate() {
  107. var _this2 = this;
  108. contentReady(this, function () {
  109. return util.toggleModifier(_this2, INDET, { force: _this2.hasAttribute(INDET) });
  110. });
  111. }
  112. }, {
  113. key: '_updateValue',
  114. value: function _updateValue() {
  115. var _this3 = this;
  116. if (this.hasAttribute('value')) {
  117. contentReady(this, function () {
  118. var per = Math.ceil(_this3.getAttribute('value') * 251.32 * 0.01);
  119. _this3._primary.style['stroke-dasharray'] = per + '%, 251.32%';
  120. });
  121. }
  122. if (this.hasAttribute('secondary-value')) {
  123. contentReady(this, function () {
  124. var per = Math.ceil(_this3.getAttribute('secondary-value') * 251.32 * 0.01);
  125. _this3._secondary.style.display = null;
  126. _this3._secondary.style['stroke-dasharray'] = per + '%, 251.32%';
  127. });
  128. } else {
  129. contentReady(this, function () {
  130. _this3._secondary.style.display = 'none';
  131. });
  132. }
  133. }
  134. /**
  135. * @property value
  136. * @type {Number}
  137. * @description
  138. * [en]Current progress. Should be a value between 0 and 100.[/en]
  139. * [ja]現在の進行状況の値を指定します。0から100の間の値を指定して下さい。[/ja]
  140. */
  141. }, {
  142. key: '_compile',
  143. value: function _compile() {
  144. if (this._isCompiled()) {
  145. this._template = util.findChild(this, '.progress-circular');
  146. } else {
  147. this._template = template.cloneNode(true);
  148. }
  149. this._primary = util.findChild(this._template, '.progress-circular__primary');
  150. this._secondary = util.findChild(this._template, '.progress-circular__secondary');
  151. this._updateDeterminate();
  152. this._updateValue();
  153. this.appendChild(this._template);
  154. autoStyle.prepare(this);
  155. ModifierUtil.initModifier(this, scheme);
  156. }
  157. }, {
  158. key: '_isCompiled',
  159. value: function _isCompiled() {
  160. if (!util.findChild(this, '.progress-circular')) {
  161. return false;
  162. }
  163. var svg = util.findChild(this, '.progress-circular');
  164. if (!util.findChild(svg, '.progress-circular__secondary')) {
  165. return false;
  166. }
  167. if (!util.findChild(svg, '.progress-circular__primary')) {
  168. return false;
  169. }
  170. return true;
  171. }
  172. }, {
  173. key: 'value',
  174. set: function set(value) {
  175. if (typeof value !== 'number' || value < 0 || value > 100) {
  176. util.throw('Invalid value');
  177. }
  178. this.setAttribute('value', Math.floor(value));
  179. },
  180. get: function get() {
  181. return parseInt(this.getAttribute('value') || '0');
  182. }
  183. /**
  184. * @property secondaryValue
  185. * @type {Number}
  186. * @description
  187. * [en]Current secondary progress. Should be a value between 0 and 100.[/en]
  188. * [ja]現在の2番目の進行状況の値を指定します。0から100の間の値を指定して下さい。[/ja]
  189. */
  190. }, {
  191. key: 'secondaryValue',
  192. set: function set(value) {
  193. if (typeof value !== 'number' || value < 0 || value > 100) {
  194. util.throw('Invalid value');
  195. }
  196. this.setAttribute('secondary-value', Math.floor(value));
  197. },
  198. get: function get() {
  199. return parseInt(this.getAttribute('secondary-value') || '0');
  200. }
  201. /**
  202. * @property indeterminate
  203. * @type {Boolean}
  204. * @description
  205. * [en]If this property is `true`, an infinite looping animation will be shown.[/en]
  206. * [ja]この属性が設定された場合、ループするアニメーションが表示されます。[/ja]
  207. */
  208. }, {
  209. key: 'indeterminate',
  210. set: function set(value) {
  211. if (value) {
  212. this.setAttribute(INDET, '');
  213. } else {
  214. this.removeAttribute(INDET);
  215. }
  216. },
  217. get: function get() {
  218. return this.hasAttribute(INDET);
  219. }
  220. }], [{
  221. key: 'observedAttributes',
  222. get: function get() {
  223. return ['modifier', 'value', 'secondary-value', INDET];
  224. }
  225. }]);
  226. return ProgressCircularElement;
  227. }(BaseElement);
  228. export default ProgressCircularElement;
  229. onsElements.ProgressCircular = ProgressCircularElement;
  230. customElements.define('ons-progress-circular', ProgressCircularElement);