widget.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. App.DashboardWidgetView = Em.View.extend({
  20. title: null,
  21. templateName: null, // each has specific template
  22. /**
  23. * Setup model for widget by `model_type`. Usually `model_type` is a lowercase service name,
  24. * for example `hdfs`, `yarn`, etc. You need to set `model_type` in extended object View, for example
  25. * look App.DataNodeUpView.
  26. * @return {Object} - model that set up in App.MainDashboardView.setWidgetsDataModel()
  27. */
  28. model : function () {
  29. if (!this.get('model_type')) return {};
  30. return this.get('parentView').get(this.get('model_type') + '_model');
  31. }.property(), //data bind from parent view
  32. id: null, // id 1-10 used to identify
  33. viewID: function(){ // used by re-sort
  34. return 'widget-' + this.get('id');
  35. }.property('id'), //html id bind to view-class: widget-(1)
  36. attributeBindings: ['viewID'],
  37. isPieChart: false,
  38. isText: false,
  39. isProgressBar: false,
  40. isLinks: false,
  41. content: null, // widget content pieChart/ text/ progress bar/links/ metrics. etc
  42. hiddenInfo: null, // more info details
  43. hiddenInfoClass: "hidden-info-two-line",
  44. thresh1: null, //@type {Number}
  45. thresh2: null, //@type {Number}
  46. didInsertElement: function () {
  47. App.tooltip(this.$("[rel='ZoomInTooltip']"), {placement : 'left'});
  48. },
  49. deleteWidget: function (event) {
  50. var parent = this.get('parentView');
  51. var self = this;
  52. if (App.get('testMode')) {
  53. //update view on dashboard
  54. var objClass = parent.widgetsMapper(this.id);
  55. parent.get('visibleWidgets').removeObject(objClass);
  56. parent.get('hiddenWidgets').pushObject(Em.Object.create({displayName: this.get('title'), id: this.get('id'), checked: false}));
  57. } else {
  58. //reconstruct new persist value then post in persist
  59. parent.getUserPref(parent.get('persistKey')).complete(function(){
  60. var oldValue = parent.get('currentPrefObject');
  61. var deletedId = self.get('id');
  62. var newValue = Em.Object.create({
  63. dashboardVersion: oldValue.dashboardVersion,
  64. visible: [],
  65. hidden: oldValue.hidden,
  66. threshold: oldValue.threshold
  67. });
  68. for (var i = 0; i <= oldValue.visible.length - 1; i++) {
  69. if (oldValue.visible[i] != deletedId) {
  70. newValue.visible.push(oldValue.visible[i]);
  71. }
  72. }
  73. newValue.hidden.push([deletedId, self.get('title')]);
  74. parent.postUserPref(parent.get('persistKey'), newValue);
  75. parent.translateToReal(newValue);
  76. });
  77. }
  78. },
  79. editWidget: function (event) {
  80. var self = this;
  81. var max_tmp = parseFloat(self.get('maxValue'));
  82. var configObj = Ember.Object.create({
  83. thresh1: self.get('thresh1') + '',
  84. thresh2: self.get('thresh2') + '',
  85. hintInfo: Em.I18n.t('dashboard.widgets.hintInfo.common').format(max_tmp),
  86. isThresh1Error: false,
  87. isThresh2Error: false,
  88. errorMessage1: "",
  89. errorMessage2: "",
  90. maxValue: max_tmp,
  91. observeNewThresholdValue: function () {
  92. var thresh1 = this.get('thresh1');
  93. var thresh2 = this.get('thresh2');
  94. if (thresh1.trim() != "") {
  95. if (isNaN(thresh1) || thresh1 > max_tmp || thresh1 < 0) {
  96. this.set('isThresh1Error', true);
  97. this.set('errorMessage1', 'Invalid! Enter a number between 0 - ' + max_tmp);
  98. } else if (this.get('isThresh2Error') === false && parseFloat(thresh2)<= parseFloat(thresh1)) {
  99. this.set('isThresh1Error', true);
  100. this.set('errorMessage1', 'Threshold 1 should be smaller than threshold 2 !');
  101. } else {
  102. this.set('isThresh1Error', false);
  103. this.set('errorMessage1', '');
  104. }
  105. } else {
  106. this.set('isThresh1Error', true);
  107. this.set('errorMessage1', 'This is required');
  108. }
  109. if (thresh2.trim() != "") {
  110. if (isNaN(thresh2) || thresh2 > max_tmp || thresh2 < 0) {
  111. this.set('isThresh2Error', true);
  112. this.set('errorMessage2', 'Invalid! Enter a number between 0 - ' + max_tmp);
  113. } else {
  114. this.set('isThresh2Error', false);
  115. this.set('errorMessage2', '');
  116. }
  117. } else {
  118. this.set('isThresh2Error', true);
  119. this.set('errorMessage2', 'This is required');
  120. }
  121. // update the slider handles and color
  122. if (this.get('isThresh1Error') === false && this.get('isThresh2Error') === false) {
  123. $("#slider-range").slider('values', 0 , parseFloat(thresh1));
  124. $("#slider-range").slider('values', 1 , parseFloat(thresh2));
  125. }
  126. }.observes('thresh1', 'thresh2')
  127. });
  128. var browserVerion = this.getInternetExplorerVersion();
  129. App.ModalPopup.show({
  130. header: Em.I18n.t('dashboard.widgets.popupHeader'),
  131. classNames: [ 'sixty-percent-width-modal-edit-widget' ],
  132. bodyClass: Ember.View.extend({
  133. templateName: require('templates/main/dashboard/edit_widget_popup'),
  134. configPropertyObj: configObj
  135. }),
  136. primary: Em.I18n.t('common.apply'),
  137. onPrimary: function() {
  138. configObj.observeNewThresholdValue();
  139. if (!configObj.isThresh1Error && !configObj.isThresh2Error) {
  140. self.set('thresh1', parseFloat(configObj.get('thresh1')) );
  141. self.set('thresh2', parseFloat(configObj.get('thresh2')) );
  142. if (!App.testMode) {
  143. // save to persist
  144. var parent = self.get('parentView');
  145. parent.getUserPref(parent.get('persistKey')).complete(function () {
  146. var oldValue = parent.get('currentPrefObject');
  147. oldValue.threshold[parseInt(self.get('id'))] = [configObj.get('thresh1'), configObj.get('thresh2')];
  148. parent.postUserPref(parent.get('persistKey'), oldValue);
  149. });
  150. }
  151. this.hide();
  152. }
  153. },
  154. didInsertElement: function () {
  155. var handlers = [configObj.get('thresh1'), configObj.get('thresh2')];
  156. var colors = ['#95A800', '#FF8E00', '#B80000']; //color green, orange ,red
  157. if (browserVerion == -1 || browserVerion > 9) {
  158. configObj.set('isIE9', false);
  159. configObj.set('isGreenOrangeRed', true);
  160. $("#slider-range").slider({
  161. range: true,
  162. min: 0,
  163. max: max_tmp,
  164. values: handlers,
  165. create: function (event, ui) {
  166. updateColors(handlers);
  167. },
  168. slide: function (event, ui) {
  169. updateColors(ui.values);
  170. configObj.set('thresh1', ui.values[0] + '');
  171. configObj.set('thresh2', ui.values[1] + '');
  172. },
  173. change: function (event, ui) {
  174. updateColors(ui.values);
  175. }
  176. });
  177. function updateColors(handlers) {
  178. var colorstops = colors[0] + ", "; // start with the first color
  179. for (var i = 0; i < handlers.length; i++) {
  180. colorstops += colors[i] + " " + handlers[i]*100/max_tmp + "%,";
  181. colorstops += colors[i+1] + " " + handlers[i]*100/max_tmp + "%,";
  182. }
  183. colorstops += colors[colors.length - 1];
  184. var sliderElement = $('#slider-range');
  185. var css1 = '-webkit-linear-gradient(left,' + colorstops + ')'; // chrome & safari
  186. sliderElement.css('background-image', css1);
  187. var css2 = '-ms-linear-gradient(left,' + colorstops + ')'; // IE 10+
  188. sliderElement.css('background-image', css2);
  189. //$('#slider-range').css('filter', 'progid:DXImageTransform.Microsoft.gradient( startColorStr= ' + colors[0] + ', endColorStr= ' + colors[2] +', GradientType=1 )' ); // IE 10-
  190. var css3 = '-moz-linear-gradient(left,' + colorstops + ')'; // Firefox
  191. sliderElement.css('background-image', css3);
  192. sliderElement.find('.ui-widget-header').css({'background-color': '#FF8E00', 'background-image': 'none'}); // change the original ranger color
  193. }
  194. } else {
  195. configObj.set('isIE9', true);
  196. configObj.set('isGreenOrangeRed', true);
  197. }
  198. }
  199. });
  200. },
  201. getInternetExplorerVersion: function (){
  202. var rv = -1; //return -1 for other browsers
  203. if (navigator.appName == 'Microsoft Internet Explorer') {
  204. var ua = navigator.userAgent;
  205. var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  206. if (re.exec(ua) != null)
  207. rv = parseFloat( RegExp.$1 ); // IE version 1-10
  208. }
  209. var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
  210. if (isFirefox) {
  211. return -2;
  212. }else{
  213. return rv;
  214. }
  215. },
  216. /**
  217. * for widgets has hidden info(hover info),
  218. * calculate the hover content top number
  219. * based on how long the hiddenInfo is
  220. */
  221. hoverContentTopClass: function () {
  222. var lineNum = this.get('hiddenInfo.length');
  223. if (lineNum == 2) {
  224. return "content-hidden-two-line";
  225. } else if (lineNum == 3) {
  226. return "content-hidden-three-line";
  227. } else if (lineNum == 4) {
  228. return "content-hidden-four-line";
  229. } else if (lineNum == 5) {
  230. return "content-hidden-five-line";
  231. } else if (lineNum == 6) {
  232. return "content-hidden-six-line";
  233. }
  234. return '';
  235. }.property('hiddenInfo.length')
  236. });
  237. App.DashboardWidgetView.reopenClass({
  238. class: 'span2p4'
  239. });