single_numeric_threshold.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  20. * @type {Em.Mixin}
  21. */
  22. App.SingleNumericThresholdMixin = Em.Mixin.create({
  23. /**
  24. * @type {Em.Object}
  25. * @class
  26. */
  27. widgetConfig: Ember.Object.extend({
  28. thresh1: '',
  29. hintInfo: '',
  30. isThresh1Error: false,
  31. errorMessage1: "",
  32. maxValue: 0,
  33. observeThresh1Value: function () {
  34. var thresh1 = this.get('thresh1');
  35. var maxValue = this.get('maxValue');
  36. if (thresh1.trim() !== "") {
  37. if (isNaN(thresh1) || thresh1 > maxValue || thresh1 < 0) {
  38. this.set('isThresh1Error', true);
  39. this.set('errorMessage1', Em.I18n.t('dashboard.widgets.error.invalid').format(maxValue));
  40. } else {
  41. this.set('isThresh1Error', false);
  42. this.set('errorMessage1', '');
  43. }
  44. } else {
  45. this.set('isThresh1Error', true);
  46. this.set('errorMessage1', Em.I18n.t('admin.users.editError.requiredField'));
  47. }
  48. this.updateSlider();
  49. }.observes('thresh1', 'maxValue'),
  50. updateSlider: function () {
  51. var thresh1 = this.get('thresh1');
  52. // update the slider handles and color
  53. if (this.get('isThresh1Error') === false) {
  54. $("#slider-range")
  55. .slider('values', 0, parseFloat(thresh1))
  56. }
  57. }
  58. }),
  59. /**
  60. * edit widget
  61. * @param {object} event
  62. */
  63. editWidget: function () {
  64. var parent = this;
  65. var maxTmp = parseFloat(this.get('maxValue'));
  66. var configObj = this.get('widgetConfig').create({
  67. thresh1: this.get('thresh1') + '',
  68. hintInfo: this.get('hintInfo') + '',
  69. maxValue: parseFloat(this.get('maxValue'))
  70. });
  71. var browserVersion = this.getInternetExplorerVersion();
  72. App.ModalPopup.show({
  73. header: Em.I18n.t('dashboard.widgets.popupHeader'),
  74. classNames: ['sixty-percent-width-modal-edit-widget'],
  75. bodyClass: Ember.View.extend({
  76. templateName: require('templates/main/dashboard/edit_widget_popup_single_threshold'),
  77. configPropertyObj: configObj
  78. }),
  79. primary: Em.I18n.t('common.apply'),
  80. onPrimary: function () {
  81. configObj.observeThresh1Value();
  82. if (!configObj.isThresh1Error) {
  83. parent.set('thresh1', parseFloat(configObj.get('thresh1')));
  84. if (!App.get('testMode')) {
  85. // save to persist
  86. var bigParent = parent.get('parentView');
  87. bigParent.getUserPref(bigParent.get('persistKey')).complete(function () {
  88. var oldValue = bigParent.get('currentPrefObject');
  89. oldValue.threshold[parseInt(parent.id, 10)] = [configObj.get('thresh1')];
  90. bigParent.postUserPref(parent.get('persistKey'), oldValue);
  91. });
  92. }
  93. this.hide();
  94. }
  95. },
  96. didInsertElement: function () {
  97. this._super();
  98. var handlers = [configObj.get('thresh1')];
  99. var colors = [App.healthStatusGreen, App.healthStatusRed]; //color green,red
  100. if (browserVersion === -1 || browserVersion > 9) {
  101. configObj.set('isIE9', false);
  102. configObj.set('isGreenRed', true);
  103. $("#slider-range").slider({
  104. range: false,
  105. min: 0,
  106. max: maxTmp,
  107. values: handlers,
  108. create: function () {
  109. updateColors(handlers);
  110. },
  111. slide: function (event, ui) {
  112. updateColors(ui.values);
  113. configObj.set('thresh1', ui.values[0] + '');
  114. },
  115. change: function (event, ui) {
  116. updateColors(ui.values);
  117. }
  118. });
  119. function updateColors(handlers) {
  120. var colorstops = colors[0] + ", "; // start with the first color
  121. for (var i = 0; i < handlers.length; i++) {
  122. colorstops += colors[i] + " " + handlers[i] * 100 / maxTmp + "%,";
  123. colorstops += colors[i + 1] + " " + handlers[i] * 100 / maxTmp + "%,";
  124. }
  125. colorstops += colors[colors.length - 1];
  126. var sliderElement = $('#slider-range');
  127. var css1 = '-webkit-linear-gradient(left,' + colorstops + ')'; // chrome & safari
  128. sliderElement.css('background-image', css1);
  129. var css2 = '-ms-linear-gradient(left,' + colorstops + ')'; // IE 10+
  130. sliderElement.css('background-image', css2);
  131. var css3 = '-moz-linear-gradient(left,' + colorstops + ')'; // Firefox
  132. sliderElement.css('background-image', css3);
  133. sliderElement.find('.ui-widget-header').css({'background-color': '#FF8E00', 'background-image': 'none'}); // change the original ranger color
  134. }
  135. } else {
  136. configObj.set('isIE9', true);
  137. configObj.set('isGreenRed', true);
  138. }
  139. }
  140. });
  141. }
  142. });