slider_config_widget_view.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. classNames: ['widget-config'],
  28. templateName: require('templates/common/configs/widgets/slider_config_widget'),
  29. supportSwitchToTextBox: true,
  30. /**
  31. * Slider-object created on the <code>initSlider</code>
  32. * @type {Object}
  33. */
  34. slider: null,
  35. /**
  36. * Mirror of the config-value shown in the input on the left of the slider
  37. * @type {number}
  38. */
  39. mirrorValue: 0,
  40. /**
  41. * Previous mirrorValue
  42. * @type {number}
  43. */
  44. prevMirrorValue: 0,
  45. /**
  46. * Determines if used-input <code>mirrorValue</code> is valid
  47. * Calculated on the <code>mirrorValueObs</code>
  48. * @type {boolean}
  49. */
  50. isMirrorValueValid: true,
  51. /**
  52. * Unit label to display.
  53. * @type {String}
  54. */
  55. unitLabel: '',
  56. /**
  57. * List of widget's properties which <code>changeBoundaries</code>-method should observe
  58. * @type {string[]}
  59. */
  60. changeBoundariesProperties: ['maxMirrorValue', 'widgetRecommendedValue','minMirrorValue', 'mirrorStep'],
  61. /**
  62. * Flag to check if value should be changed to recommended or saved.
  63. * @type {boolean}
  64. */
  65. isRestoring: false,
  66. /**
  67. * max allowed value transformed form config unit to widget unit
  68. * @type {Number}
  69. */
  70. maxMirrorValue: function() {
  71. var parseFunction = this.get('mirrorValueParseFunction');
  72. var maximum = this.getValueAttributeByGroup('maximum');
  73. var max = this.widgetValueByConfigAttributes(maximum);
  74. return parseFunction(max);
  75. }.property('config.stackConfigProperty.valueAttributes.maximum', 'controller.forceUpdateBoundaries'),
  76. /**
  77. * min allowed value transformed form config unit to widget unit
  78. * @type {Number}
  79. */
  80. minMirrorValue: function() {
  81. var parseFunction = this.get('mirrorValueParseFunction');
  82. var minimum = this.getValueAttributeByGroup('minimum');
  83. var min = this.widgetValueByConfigAttributes(minimum);
  84. return parseFunction(min);
  85. }.property('config.stackConfigProperty.valueAttributes.minimum', 'controller.forceUpdateBoundaries'),
  86. /**
  87. *
  88. * if group is default look into (ex. for maximum)
  89. * <code>config.stackConfigProperty.valueAttributes.maximum<code>
  90. * if group is not default look into
  91. * <code>config.stackConfigProperty.valueAttributes.{group.name}.maximum<code>
  92. * @param {String} attribute - name of attribute, for current moment
  93. * can be ["maximum","minimum","increment_step"] but allows to use other it there will be available
  94. * @returns {string}
  95. */
  96. getValueAttributeByGroup: function(attribute) {
  97. var parseFunction = this.get('parseFunction');
  98. var configValue = this.get('config.value');
  99. var defaultGroupAttr = this.get('config.stackConfigProperty.valueAttributes');
  100. var groupAttr = this.get('configGroup') && defaultGroupAttr[this.get('configGroup.name')];
  101. var boundary = (groupAttr && !Em.isNone(groupAttr[attribute])) ? groupAttr[attribute] : defaultGroupAttr[attribute];
  102. if (!this.get('referToSelectedGroup')) {
  103. if (attribute === 'minimum') {
  104. if (parseFunction(configValue) < parseFunction(boundary)) {
  105. return configValue;
  106. }
  107. } else if (attribute === 'maximum') {
  108. if (parseFunction(configValue) > parseFunction(boundary)) {
  109. return configValue;
  110. }
  111. }
  112. }
  113. return boundary;
  114. },
  115. /**
  116. * step transformed form config units to widget units
  117. * @type {Number}
  118. */
  119. mirrorStep: function() {
  120. var parseFunction = this.get('mirrorValueParseFunction');
  121. var step = this.widgetValueByConfigAttributes(this.get('config.stackConfigProperty.valueAttributes.increment_step'));
  122. return step ? parseFunction(step) : this.get('unitType') === 'int' ? 1 : 0.1;
  123. }.property('config.stackConfigProperty.valueAttributes.increment_step'),
  124. /**
  125. * Default value of config property transformed according widget format
  126. * @returns {Number}
  127. */
  128. widgetDefaultValue: function () {
  129. var parseFunction = this.get('mirrorValueParseFunction');
  130. return parseFunction(this.widgetValueByConfigAttributes(this.get('config.savedValue')));
  131. }.property('config.savedValue'),
  132. /**
  133. * Default value of config property transformed according widget format
  134. * @returns {Number}
  135. */
  136. widgetRecommendedValue: function () {
  137. var parseFunction = this.get('mirrorValueParseFunction');
  138. return parseFunction(this.widgetValueByConfigAttributes(this.get('config.recommendedValue')));
  139. }.property('config.recommendedValue'),
  140. /**
  141. * unit type of widget
  142. * @type {String}
  143. */
  144. unitType: function () {
  145. var widgetUnit = this.get('config.stackConfigProperty.widget.units.length') && this.get('config.stackConfigProperty.widget.units')[0]['unit-name'].toLowerCase();
  146. var configUnit = this.get('config.stackConfigProperty.valueAttributes.type').toLowerCase();
  147. if (widgetUnit) {
  148. return this.get('units').indexOf(widgetUnit) > this.get('units').indexOf(configUnit) ? 'float' : this.get('config.stackConfigProperty.valueAttributes.type')
  149. } else {
  150. return 'float';
  151. }
  152. }.property('config.stackConfigProperty.widget.units.@each.unit-name'),
  153. /**
  154. * Function used to parse widget mirror value
  155. * For integer - parseInt, for float - parseFloat
  156. * @type {Function}
  157. */
  158. mirrorValueParseFunction: function () {
  159. return this.get('unitType') === 'int' ? parseInt : parseFloat;
  160. }.property('unitType'),
  161. /**
  162. * Function used to validate widget mirror value
  163. * For integer - validator.isValidInt, for float - validator.isValidFloat
  164. * @type {Function}
  165. */
  166. mirrorValueValidateFunction: function () {
  167. return this.get('unitType') === 'int' ? validator.isValidInt : validator.isValidFloat;
  168. }.property('unitType'),
  169. /**
  170. * Function used to parse config value (based on <code>config.stackConfigProperty.valueAttributes.type</code>)
  171. * For integer - parseInt, for float - parseFloat
  172. * @type {Function}
  173. */
  174. parseFunction: function () {
  175. return this.get('config.stackConfigProperty.valueAttributes.type') === 'int' ? parseInt : parseFloat;
  176. }.property('config.stackConfigProperty.valueAttributes.type'),
  177. /**
  178. * Function used to validate config value (based on <code>config.stackConfigProperty.valueAttributes.type</code>)
  179. * For integer - validator.isValidInt, for float - validator.isValidFloat
  180. * @type {Function}
  181. */
  182. validateFunction: function () {
  183. return this.get('config.stackConfigProperty.valueAttributes.type') === 'int' ? validator.isValidInt : validator.isValidFloat;
  184. }.property('config.stackConfigProperty.valueAttributes.type'),
  185. /**
  186. * Enable/disable slider state
  187. * @method toggleWidgetState
  188. */
  189. toggleWidgetState: function () {
  190. var slider = this.get('slider');
  191. this.get('config.isEditable') ? slider.enable() : slider.disable();
  192. this._super();
  193. }.observes('config.isEditable'),
  194. willInsertElement: function () {
  195. this._super();
  196. this.prepareValueConverter();
  197. this.addObserver('mirrorValue', this, this.mirrorValueObs);
  198. },
  199. didInsertElement: function () {
  200. this._super();
  201. this.setValue();
  202. this.initSlider();
  203. this.toggleWidgetState();
  204. this.initPopover();
  205. var self = this;
  206. this.get('changeBoundariesProperties').forEach(function(property) {
  207. self.addObserver(property, self, self.changeBoundaries);
  208. });
  209. },
  210. willDestroyElement: function() {
  211. this.$('[data-toggle=tooltip]').tooltip('destroy');
  212. var self = this;
  213. this.get('changeBoundariesProperties').forEach(function(property) {
  214. self.removeObserver(property, self, self.changeBoundaries);
  215. });
  216. this.removeObserver('mirrorValue', this, this.mirrorValueObs);
  217. if (this.get('slider')) {
  218. try {
  219. if (self.get('slider')) {
  220. self.get('slider').destroy();
  221. }
  222. } catch (e) {
  223. console.error('error while clearing slider for config: ' + self.get('config.name'));
  224. }
  225. }
  226. },
  227. /**
  228. * Check if <code>mirrorValue</code> was updated by user
  229. * Validate it. If value is correct, set it to slider and config.value
  230. * @method mirrorValueObs
  231. */
  232. mirrorValueObs: function () {
  233. var mirrorValue = this.get('mirrorValue'),
  234. slider = this.get('slider'),
  235. min = this.get('minMirrorValue'),
  236. max = this.get('maxMirrorValue'),
  237. validationFunction = this.get('mirrorValueValidateFunction'),
  238. parseFunction = this.get('mirrorValueParseFunction');
  239. if (validationFunction(mirrorValue)) {
  240. var parsed = parseFunction(mirrorValue);
  241. if (parsed > max) {
  242. this.set('isMirrorValueValid', false);
  243. this.get('config').setProperties({
  244. warnMessage: Em.I18n.t('config.warnMessage.outOfBoundaries.greater').format(max + this.get('unitLabel')),
  245. warn: true
  246. });
  247. } else if (parsed < min) {
  248. this.set('isMirrorValueValid', false);
  249. this.get('config').setProperties({
  250. warnMessage: Em.I18n.t('config.warnMessage.outOfBoundaries.less').format(min + this.get('unitLabel')),
  251. warn: true
  252. });
  253. } else {
  254. this.set('isMirrorValueValid', !this.get('config.error'));
  255. this.set('config.value', '' + this.configValueByWidget(parsed));
  256. if (slider) {
  257. slider.setValue(parsed);
  258. }
  259. }
  260. // avoid precision during restore value
  261. if (!Em.isNone(this.get('config.savedValue')) && parsed == parseFunction(this.widgetValueByConfigAttributes(this.get('config.savedValue')))) {
  262. this.set('config.value', this.get('config.savedValue'));
  263. }
  264. // ignore precision during set recommended value
  265. if (!Em.isNone(this.get('config.recommendedValue')) && parsed == parseFunction(this.widgetValueByConfigAttributes(this.get('config.recommendedValue')))) {
  266. this.set('config.value', this.get('config.recommendedValue'));
  267. }
  268. } else {
  269. this.set('isMirrorValueValid', false);
  270. this.set('config.errorMessage', 'Invalid value');
  271. }
  272. },
  273. /**
  274. * set widget value same as config value
  275. * @override
  276. * @method setValue
  277. */
  278. setValue: function(value) {
  279. var parseFunction = this.get('parseFunction');
  280. value = value || parseFunction(this.get('config.value'));
  281. this.set('mirrorValue', this.widgetValueByConfigAttributes(value));
  282. this.set('prevMirrorValue', this.get('mirrorValue'));
  283. },
  284. /**
  285. * Setup convert table according to widget unit-name and property type.
  286. * Set label for unit to display.
  287. * @method prepareValueConverter
  288. */
  289. prepareValueConverter: function() {
  290. var widgetUnit = this._converterGetWidgetUnits();
  291. if (['int', 'float'].contains(this._converterGetPropertyAttributes()) && widgetUnit == 'percent') {
  292. this.set('currentDimensionType', 'percent.percent_' + this._converterGetPropertyAttributes());
  293. }
  294. this.set('unitLabel', Em.getWithDefault(this.get('unitLabelMap'), widgetUnit, widgetUnit));
  295. },
  296. /**
  297. * Draw slider for current config
  298. * @method initSlider
  299. */
  300. initSlider: function () {
  301. var self = this,
  302. config = this.get('config'),
  303. valueAttributes = config.get('stackConfigProperty.valueAttributes'),
  304. parseFunction = this.get('parseFunction'),
  305. ticks = [this.valueForTick(this.get('minMirrorValue'))],
  306. ticksLabels = [],
  307. maxMirrorValue = this.get('maxMirrorValue'),
  308. minMirrorValue = this.get('minMirrorValue'),
  309. mirrorStep = this.get('mirrorStep'),
  310. recommendedValue = this.valueForTick(+this.get('widgetRecommendedValue')),
  311. range = Math.floor((maxMirrorValue - minMirrorValue) / mirrorStep) * mirrorStep,
  312. isOriginalSCP = config.get('isOriginalSCP'),
  313. // for little odd numbers in range 4..23 and widget type 'int' use always 4 ticks
  314. isSmallInt = this.get('unitType') == 'int' && range > 4 && range < 23 && range % 2 == 1,
  315. recommendedValueMirroredId,
  316. recommendedValueId;
  317. // ticks and labels
  318. for (var i = 1; i <= 3; i++) {
  319. var val = minMirrorValue + this.valueForTickProportionalToStep(range * (i / (isSmallInt ? 3 : 4)));
  320. // if value's type is float, ticks may be float too
  321. ticks.push(this._extraRound(val));
  322. }
  323. ticks.push(this.valueForTick(maxMirrorValue));
  324. ticks = ticks.uniq();
  325. ticks.forEach(function (tick, index, items) {
  326. var label = '';
  327. if ((items.length < 5 || index % 2 === 0 || items.length - 1 == index)) {
  328. label = this.formatTickLabel(tick, ' ');
  329. }
  330. ticksLabels.push(label);
  331. }, this);
  332. ticks = ticks.uniq();
  333. if (!(this.get('controller.isCompareMode') && !isOriginalSCP)) {
  334. // default marker should be added only if recommendedValue is in range [min, max]
  335. if (recommendedValue <= maxMirrorValue && recommendedValue >= minMirrorValue && recommendedValue != '') {
  336. // process additional tick for default value if it not defined in previous computation
  337. if (!ticks.contains(recommendedValue)) {
  338. // push default value
  339. ticks.push(recommendedValue);
  340. // and resort array
  341. ticks = ticks.sort(function (a, b) {
  342. return a - b;
  343. });
  344. recommendedValueId = ticks.indexOf(recommendedValue);
  345. // to save nice tick labels layout we should add new tick value which is mirrored by index to default value
  346. recommendedValueMirroredId = ticks.length - recommendedValueId;
  347. // push mirrored default value behind default
  348. if (recommendedValueId == recommendedValueMirroredId) {
  349. recommendedValueMirroredId--;
  350. }
  351. // push empty label for default value tick
  352. ticksLabels.insertAt(recommendedValueId, '');
  353. // push empty to mirrored position
  354. ticksLabels.insertAt(recommendedValueMirroredId, '');
  355. // for saving correct sliding need to add value to mirrored position which is average between previous
  356. // and next value
  357. ticks.insertAt(recommendedValueMirroredId, this.valueForTick((ticks[recommendedValueMirroredId] + ticks[recommendedValueMirroredId - 1]) / 2));
  358. // get new index for default value
  359. recommendedValueId = ticks.indexOf(recommendedValue);
  360. }
  361. else {
  362. recommendedValueId = ticks.indexOf(recommendedValue);
  363. }
  364. }
  365. }
  366. /**
  367. * Slider some times change config value while being created,
  368. * this may happens when slider recreating couple times during small period.
  369. * To cover this situation need to reset config value after slider initializing
  370. * @type {String}
  371. */
  372. var correctConfigValue = this.get('config.value');
  373. var slider = new Slider(this.$('input.slider-input')[0], {
  374. value: this.get('mirrorValue'),
  375. ticks: ticks,
  376. tooltip: 'always',
  377. ticks_labels: ticksLabels,
  378. step: mirrorStep,
  379. formatter: function (val) {
  380. var labelValue = Em.isArray(val) ? val[0] : val;
  381. return self.formatTickLabel(labelValue);
  382. }
  383. });
  384. /**
  385. * Resetting config value, look for <code>correctConfigValue<code>
  386. * for more info
  387. */
  388. this.set('config.value', correctConfigValue);
  389. slider.on('change', this.onSliderChange.bind(this))
  390. .on('slideStop', function() {
  391. /**
  392. * action to run sendRequestRorDependentConfigs when
  393. * we have changed config value within slider
  394. */
  395. if (self.get('prevMirrorValue') != self.get('mirrorValue')) {
  396. self.sendRequestRorDependentConfigs(self.get('config'));
  397. }
  398. });
  399. this.set('slider', slider);
  400. var sliderTicks = this.$('.ui-slider-wrapper:eq(0) .slider-tick');
  401. if (recommendedValueId) {
  402. sliderTicks.eq(recommendedValueId).addClass('slider-tick-default').on('mousedown', function(e) {
  403. if (self.get('disabled')) return false;
  404. self.setRecommendedValue();
  405. e.stopPropagation();
  406. return false;
  407. });
  408. // create label for default value and align it
  409. // defaultSliderTick.append('<span>{0}</span>'.format(recommendedValue + this.get('unitLabel')));
  410. // defaultSliderTick.find('span').css('marginLeft', -defaultSliderTick.find('span').width()/2 + 'px');
  411. // if mirrored value was added need to hide the tick for it
  412. if (recommendedValueMirroredId) {
  413. sliderTicks.eq(recommendedValueMirroredId).hide();
  414. }
  415. }
  416. // mark last tick to fix it style
  417. sliderTicks.last().addClass('last');
  418. },
  419. /**
  420. * Callback function triggered on slider change event.
  421. * Set config property and widget value with new one, or ignore changes in case value restoration executed by
  422. * <code>restoreValue</code>, <code>setRecommendedValue</code>.
  423. *
  424. * @param {Object} e - object that contains <code>oldValue</code> and <code>newValue</code> attributes.
  425. * @method onSliderChange
  426. */
  427. onSliderChange: function(e) {
  428. if (!this.get('isRestoring')) {
  429. var val = this.get('mirrorValueParseFunction')(e.newValue);
  430. this.set('config.value', '' + this.configValueByWidget(val));
  431. this.set('mirrorValue', val);
  432. } else {
  433. this.set('isRestoring', false);
  434. }
  435. },
  436. /**
  437. * Convert value according to property attribute unit.
  438. *
  439. * @method valueForTick
  440. * @param {Number} val
  441. * @private
  442. * @returns {Number}
  443. */
  444. valueForTick: function(val) {
  445. return this.get('unitType') === 'int' ? Math.round(val) : this._extraRound(val);
  446. },
  447. /**
  448. * Convert value according to property attribute unit
  449. * Also returned value is proportional to the <code>mirrorStep</code>
  450. *
  451. * @param {Number} val
  452. * @private
  453. * @returns {Number}
  454. */
  455. valueForTickProportionalToStep: function (val) {
  456. if (this.get('unitType') === 'int') {
  457. return Math.round(val);
  458. }
  459. var mirrorStep = this.get('mirrorStep');
  460. var r = Math.round(val / mirrorStep);
  461. return this._extraRound(r * mirrorStep);
  462. },
  463. /**
  464. * Round number to 3 digits after "."
  465. * Used for all slider's ticks
  466. * @param {Number} v
  467. * @returns {Number} number with 3 digits after "."
  468. * @private
  469. * @method _extraRound
  470. */
  471. _extraRound: function(v) {
  472. return parseFloat(v.toFixed(3));
  473. },
  474. /**
  475. * Restore <code>savedValue</code> for config
  476. * Restore <code>mirrorValue</code> too
  477. * @method restoreValue
  478. */
  479. restoreValue: function () {
  480. this._super();
  481. this.set('isRestoring', true);
  482. this.get('slider').setValue(this.get('widgetDefaultValue'));
  483. if (this.get('config.value') === this.get('config.savedValue')) {
  484. this.set('isRestoring', false);
  485. }
  486. },
  487. /**
  488. * @method setRecommendedValue
  489. */
  490. setRecommendedValue: function () {
  491. this._super();
  492. this.set('isRestoring', true);
  493. this.get('slider').setValue(this.get('widgetRecommendedValue'));
  494. if (this.get('config.value') === this.get('config.recommendedValue')) {
  495. this.set('isRestoring', false);
  496. }
  497. },
  498. /**
  499. * Determines if config-value was changed
  500. * @type {boolean}
  501. */
  502. valueIsChanged: function () {
  503. return !Em.isNone(this.get('config.savedValue')) && this.get('parseFunction')(this.get('config.value')) != this.get('parseFunction')(this.get('config.savedValue'));
  504. }.property('config.value', 'config.savedValue'),
  505. /**
  506. * Run changeBoundariesOnce only once
  507. * @method changeBoundaries
  508. */
  509. changeBoundaries: function() {
  510. if (this.get('config.stackConfigProperty.widget')) {
  511. Em.run.once(this, 'changeBoundariesOnce');
  512. }
  513. },
  514. /**
  515. * Recreate widget in case max or min values were changed
  516. *
  517. * @method changeBoundariesOnce
  518. */
  519. changeBoundariesOnce: function () {
  520. if ($.mocho) {
  521. //temp fix as it can broke test that doesn't have any connection with this method
  522. return;
  523. }
  524. if (this.get('config')) {
  525. try {
  526. if (this.get('slider')) {
  527. this.get('slider').destroy();
  528. }
  529. this.initIncompatibleWidgetAsTextBox();
  530. this.initSlider();
  531. this.toggleWidgetState();
  532. // arguments exists - means method is called as observer and not directly from other method
  533. // so, no need to call <code>refreshSliderObserver</code> and this prevent recursive calls
  534. // like changeBoundariesOnce -> refreshSliderObserver -> changeBoundariesOnce -> ...
  535. if (arguments.length) {
  536. this.refreshSliderObserver();
  537. }
  538. } catch (e) {
  539. console.error('error while rebuilding slider for config: ' + this.get('config.name'));
  540. }
  541. }
  542. },
  543. /**
  544. * Method used for initializing sliders in the next Ember run-loop
  545. * It's useful for overrides, because they are redrawn a little bit later than origin config
  546. *
  547. * If this method is called without arguments, it will call itself recursively using <code>changeBoundariesOnce</code>
  548. * and <code>refreshSliderObserver</code>
  549. * If not - it will just call <code>changeBoundariesOnce</code> in the next run-loop
  550. * @method changeBoundariesOnceLater
  551. */
  552. _changeBoundariesOnceLater: function() {
  553. var self = this;
  554. Em.run.later('sync', function() {
  555. self.changeBoundariesOnce();
  556. }, 10);
  557. },
  558. /**
  559. * Workaround for bootstrap-slider widget that was initiated inside hidden container.
  560. * @method refreshSliderObserver
  561. */
  562. refreshSliderObserver: function() {
  563. var self = this;
  564. var sliderTickLabel = this.$('.ui-slider-wrapper:eq(0) .slider-tick-label:first');
  565. if (sliderTickLabel.width() == 0 && this.isValueCompatibleWithWidget()) {
  566. Em.run.next(function() {
  567. self._changeBoundariesOnceLater();
  568. });
  569. }
  570. }.observes('parentView.content.isActive', 'parentView.parentView.tab.isActive'),
  571. /**
  572. * Check if value provided by user in the textbox may be used in the slider
  573. * @returns {boolean}
  574. * @method isValueCompatibleWithWidget
  575. */
  576. isValueCompatibleWithWidget: function() {
  577. if (this._super()) {
  578. if (!this.get('validateFunction')(this.get('config.value'))) {
  579. return false;
  580. }
  581. var configValue = this.get('parseFunction')(this.get('config.value'));
  582. if (this.get('config.stackConfigProperty.valueAttributes.minimum')) {
  583. var min = this.get('parseFunction')(this.getValueAttributeByGroup('minimum'));
  584. if (configValue < min) {
  585. min = this.widgetValueByConfigAttributes(min);
  586. this.updateWarningsForCompatibilityWithWidget(Em.I18n.t('config.warnMessage.outOfBoundaries.less').format(min + this.get('unitLabel')));
  587. return false;
  588. }
  589. }
  590. if (this.get('config.stackConfigProperty.valueAttributes.maximum')) {
  591. var max = this.get('parseFunction')(this.getValueAttributeByGroup('maximum'));
  592. if (configValue > max) {
  593. max = this.widgetValueByConfigAttributes(max);
  594. this.updateWarningsForCompatibilityWithWidget(Em.I18n.t('config.warnMessage.outOfBoundaries.greater').format(max + this.get('unitLabel')));
  595. return false;
  596. }
  597. }
  598. this.updateWarningsForCompatibilityWithWidget('');
  599. return true;
  600. }
  601. return false;
  602. },
  603. /**
  604. * Returns formatted value of slider label
  605. * @param tick - starting value
  606. * @param separator - will be inserted between value and unit
  607. * @returns {string}
  608. */
  609. formatTickLabel: function (tick, separator) {
  610. var label,
  611. separator = separator || '',
  612. valueLabel = tick,
  613. units = ['B', 'KB', 'MB', 'GB', 'TB'],
  614. unitLabel = this.get('unitLabel'),
  615. unitLabelIndex = units.indexOf(unitLabel);
  616. if (unitLabelIndex > -1) {
  617. while (tick > 9999 && unitLabelIndex < units.length - 1) {
  618. tick /= 1024;
  619. unitLabelIndex++;
  620. }
  621. unitLabel = units[unitLabelIndex];
  622. valueLabel = this._extraRound(tick);
  623. }
  624. label = valueLabel + separator + unitLabel;
  625. return label;
  626. }
  627. });