slider_config_widget_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  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. var App = require('app');
  19. var validator = require('utils/validator');
  20. /**
  21. * Slider-view for configs
  22. * Used to numeric values
  23. * Config value attributes should contain minimum and maximum limits for value
  24. * @type {App.ConfigWidgetView}
  25. */
  26. App.SliderConfigWidgetView = App.ConfigWidgetView.extend({
  27. templateName: require('templates/common/configs/widgets/slider_config_widget'),
  28. /**
  29. * Slider-object created on the <code>initSlider</code>
  30. * @type {Object}
  31. */
  32. slider: null,
  33. /**
  34. * Determines if widget controls should be disabled
  35. * @type {boolean}
  36. */
  37. disabled: false,
  38. /**
  39. * Mirror of the config-value shown in the input on the left of the slider
  40. * @type {number}
  41. */
  42. mirrorValue: 0,
  43. /**
  44. * Determines if used-input <code>mirrorValue</code> is valid
  45. * Calculated on the <code>mirrorValueObs</code>
  46. * @type {boolean}
  47. */
  48. isMirrorValueValid: true,
  49. /**
  50. * Function used to parse config value (based on <code>config.stackConfigProperty.valueAttributes.type</code>)
  51. * For integer - parseInt, for float - parseFloat
  52. * @type {Function}
  53. */
  54. parseFunction: function () {
  55. return this.get('config.stackConfigProperty.valueAttributes.type') === 'int' ? parseInt : parseFloat;
  56. }.property('config.stackConfigProperty.valueAttributes.type'),
  57. /**
  58. * Function used to validate config value (based on <code>config.stackConfigProperty.valueAttributes.type</code>)
  59. * For integer - validator.isValidInt, for float - validator.isValidFloat
  60. * @type {Function}
  61. */
  62. validateFunction: function () {
  63. return this.get('config.stackConfigProperty.valueAttributes.type') === 'int' ? validator.isValidInt : validator.isValidFloat;
  64. }.property('config.stackConfigProperty.valueAttributes.type'),
  65. /**
  66. * Enable/disable slider state
  67. * @method toggleWidgetState
  68. */
  69. toggleWidgetState: function () {
  70. var slider = this.get('slider');
  71. this.get('config.isEditable') ? slider.enable() : slider.disable();
  72. this.set('disabled', !this.get('config.isEditable'));
  73. }.observes('config.isEditable'),
  74. willInsertElement: function () {
  75. this._super();
  76. this.addObserver('mirrorValue', this, this.mirrorValueObs);
  77. },
  78. didInsertElement: function () {
  79. this._super();
  80. this.set('mirrorValue', this.get('config.value'));
  81. this.prepareValueAttributes();
  82. this.initSlider();
  83. this.toggleWidgetState();
  84. this.initPopover();
  85. this.addObserver('config.stackConfigProperty.valueAttributes.minimum', this, this.changeBoundaries);
  86. this.addObserver('config.stackConfigProperty.valueAttributes.maximum', this, this.changeBoundaries);
  87. this.addObserver('config.stackConfigProperty.valueAttributes.step', this, this.changeBoundaries);
  88. },
  89. willDestroyElement: function() {
  90. this.removeObserver('config.stackConfigProperty.valueAttributes.step', this, this.changeBoundaries);
  91. this.removeObserver('config.stackConfigProperty.valueAttributes.maximum', this, this.changeBoundaries);
  92. this.removeObserver('config.stackConfigProperty.valueAttributes.minimum', this, this.changeBoundaries);
  93. },
  94. /**
  95. * view class for text box that is used with slider widget
  96. * @type {Em.TextField}
  97. */
  98. MirrorValueView: Em.TextField.extend({
  99. focusOut: function() {
  100. this.get('parentView').sendRequestRorDependentConfigs(this.get('parentView.config'));
  101. }
  102. }),
  103. /**
  104. * Check if <code>mirrorValue</code> was updated by user
  105. * Validate it. If value is correct, set it to slider and config.value
  106. * @method mirrorValueObs
  107. */
  108. mirrorValueObs: function () {
  109. var mirrorValue = this.get('mirrorValue'),
  110. slider = this.get('slider'),
  111. min = this.get('config.stackConfigProperty.valueAttributes.minimum'),
  112. max = this.get('config.stackConfigProperty.valueAttributes.maximum'),
  113. validationFunction = this.get('validateFunction'),
  114. parseFunction = this.get('parseFunction');
  115. if (validationFunction(mirrorValue)) {
  116. var parsed = parseFunction(mirrorValue);
  117. if (parsed >= min && parsed <= max) {
  118. this.set('isMirrorValueValid', true);
  119. this.set('config.errorMessage', '');
  120. this.set('config.value', '' + parsed);
  121. if (slider) {
  122. slider.setValue(parsed);
  123. }
  124. }
  125. else {
  126. this.set('isMirrorValueValid', false);
  127. this.set('config.errorMessage', 'Invalid value');
  128. }
  129. }
  130. else {
  131. this.set('isMirrorValueValid', false);
  132. this.set('config.errorMessage', 'Invalid value');
  133. }
  134. },
  135. /**
  136. * @override
  137. * @method setValue
  138. * set widget value same as config value
  139. */
  140. setValue: function() {
  141. var parseFunction = this.get('parseFunction');
  142. this.set('mirrorValue', parseFunction(this.get('config.value')));
  143. },
  144. /**
  145. * valueAttributes are strings, but should be numbers
  146. * parse them using <code>parseFunction</code>
  147. * @method prepareValueAttributes
  148. */
  149. prepareValueAttributes: function () {
  150. var valueAttributes = this.get('config.stackConfigProperty.valueAttributes'),
  151. parseFunction = this.get('parseFunction');
  152. if (!valueAttributes) return;
  153. Em.set(valueAttributes, 'maximum', parseFunction(valueAttributes.maximum));
  154. Em.set(valueAttributes, 'minimum', parseFunction(valueAttributes.minimum));
  155. },
  156. /**
  157. * Draw slider for current config
  158. * @method initSlider
  159. */
  160. initSlider: function () {
  161. var self = this,
  162. config = this.get('config'),
  163. valueAttributes = config.get('stackConfigProperty.valueAttributes'),
  164. unit = Em.getWithDefault(valueAttributes, 'unit', ''),
  165. parseFunction = this.get('parseFunction'),
  166. ticks = [valueAttributes.minimum],
  167. ticksLabels = [],
  168. defaultValue = this.valueForTick(+config.get('defaultValue')),
  169. defaultValueMirroredId,
  170. defaultValueId;
  171. // ticks and labels
  172. for (var i = 1; i <= 3; i++) {
  173. var val = (valueAttributes.minimum + valueAttributes.maximum) / 4 * i;
  174. // if value's type is float, ticks may be float too
  175. ticks.push(this.valueForTick(val));
  176. }
  177. ticks.push(valueAttributes.maximum);
  178. ticks.forEach(function (tick, index) {
  179. ticksLabels.push(index % 2 === 0 ? tick + ' ' + unit : '');
  180. });
  181. // process additional tick for default value if it not defined in previous computation
  182. if (!ticks.contains(defaultValue)) {
  183. // push default value
  184. ticks.push(defaultValue);
  185. // and resort array
  186. ticks = ticks.sort(function(a,b) { return a-b; });
  187. defaultValueId = ticks.indexOf(defaultValue);
  188. // to save nice tick labels layout we should add new tick value which is mirrored by index to default value
  189. defaultValueMirroredId = ticks.length - defaultValueId;
  190. // push empty label for default value tick
  191. ticksLabels.insertAt(defaultValueId, '');
  192. // push empty to mirrored position
  193. ticksLabels.insertAt(defaultValueMirroredId, '');
  194. // for saving correct sliding need to add value to mirrored position which is average between previous
  195. // and next value
  196. ticks.insertAt(defaultValueMirroredId, (ticks[defaultValueMirroredId] + ticks[defaultValueMirroredId - 1])/2);
  197. // get new index for default value
  198. defaultValueId = ticks.indexOf(defaultValue);
  199. } else {
  200. defaultValueId = ticks.indexOf(defaultValue);
  201. }
  202. var slider = new Slider(this.$('input.slider-input')[0], {
  203. value: parseFunction(this.get('config.value')),
  204. ticks: ticks,
  205. tooltip: 'hide',
  206. ticks_labels: ticksLabels,
  207. ticks_snap_bounds: Em.get(valueAttributes, 'type') === 'int' ? 1 : 0.1,
  208. step: Em.get(valueAttributes, 'type') === 'int' ? 1 : 0.1
  209. });
  210. slider.on('change', function (obj) {
  211. var val = parseFunction(obj.newValue);
  212. self.set('config.value', '' + val);
  213. self.set('mirrorValue', val);
  214. }).on('slideStop', function() {
  215. /**
  216. * action to run sendRequestRorDependentConfigs when
  217. * we have changed config value within slider
  218. */
  219. self.sendRequestRorDependentConfigs(self.get('config'));
  220. });
  221. this.set('slider', slider);
  222. var sliderTicks = this.$('.ui-slider-wrapper:eq(0) .slider-tick');
  223. sliderTicks.eq(defaultValueId).addClass('slider-tick-default').on('click', function() {
  224. self.restoreValue();
  225. });
  226. // if mirrored value was added need to hide the tick for it
  227. if (defaultValueMirroredId) {
  228. sliderTicks.eq(defaultValueMirroredId).hide();
  229. }
  230. // hide some ticks. can't do this via css
  231. if (defaultValueId == 0) {
  232. sliderTicks.last().hide();
  233. } else
  234. if (defaultValueId == ticks.length - 1) {
  235. sliderTicks.first().hide();
  236. }
  237. else {
  238. sliderTicks.first().hide();
  239. sliderTicks.last().hide();
  240. }
  241. },
  242. /**
  243. * Convert value according to property attribute unit.
  244. *
  245. * @method valueForTick
  246. * @param {Number} val
  247. * @private
  248. * @returns {Number}
  249. */
  250. valueForTick: function(val) {
  251. return this.get('config.stackConfigProperty.valueAttributes').type === 'int' ? Math.round(val) : parseFloat(val.toFixed(1));
  252. },
  253. /**
  254. * Restore <code>defaultValue</code> for config
  255. * Restore <code>mirrorValue</code> too
  256. * @method restoreValue
  257. */
  258. restoreValue: function () {
  259. this._super();
  260. var parseFunction = this.get('parseFunction'),
  261. val = parseFunction(this.get('config.value'));
  262. this.get('slider').setValue(val);
  263. this.set('mirrorValue', val);
  264. },
  265. /**
  266. * Run changeBoundariesOnce only once
  267. * @method changeBoundaries
  268. */
  269. changeBoundaries: function() {
  270. Em.run.once(this, 'changeBoundariesOnce');
  271. },
  272. /**
  273. * recreate widget in case max or min values were changed
  274. * @method changeBoundariesOnce
  275. */
  276. changeBoundariesOnce: function () {
  277. if ($.mocho) {
  278. //temp fix as it can broke test that doesn't have any connection with this method
  279. return;
  280. }
  281. var self = this;
  282. var valueAttributes = this.get('config.stackConfigProperty.valueAttributes');
  283. self.prepareValueAttributes();
  284. if (self.get('slider')) {
  285. self.get('slider').destroy();
  286. self.initSlider();
  287. if (self.get('config.value') > Em.get(valueAttributes, 'maximum')) {
  288. self.set('mirrorValue', Em.get(valueAttributes, 'maximum'))
  289. }
  290. if (self.get('config.value') < Em.get(valueAttributes, 'minimum')) {
  291. self.set('mirrorValue', Em.get(valueAttributes, 'minimum'))
  292. }
  293. self.toggleWidgetState();
  294. }
  295. }
  296. });