widget.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /**
  21. * @type {string}
  22. * @default null
  23. */
  24. title: null,
  25. templateName: null, // each has specific template
  26. /**
  27. * Setup model for widget by `model_type`. Usually `model_type` is a lowercase service name,
  28. * for example `hdfs`, `yarn`, etc. You need to set `model_type` in extended object View, for example
  29. * look App.DataNodeUpView.
  30. * @type {object} - model that set up in App.MainDashboardView.setWidgetsDataModel()
  31. */
  32. model : function () {
  33. if (!this.get('model_type')) return {};
  34. return this.get('parentView').get(this.get('model_type') + '_model');
  35. }.property(),
  36. /**
  37. * id 1-10 used to identify
  38. * @type {number}
  39. * @default null
  40. */
  41. id: null,
  42. /**
  43. * html id bind to view-class: widget-(1)
  44. * used by re-sort
  45. * @type {string}
  46. */
  47. viewID: Em.computed.format('widget-{0}', 'id'),
  48. attributeBindings: ['viewID'],
  49. /**
  50. * widget content pieChart/ text/ progress bar/links/ metrics. etc
  51. * @type {Array}
  52. * @default null
  53. */
  54. content: null,
  55. /**
  56. * more info details
  57. * @type {Array}
  58. */
  59. hiddenInfo: [],
  60. /**
  61. * @type {string}
  62. */
  63. hiddenInfoClass: "hidden-info-two-line",
  64. /**
  65. * @type {number}
  66. * @default null
  67. */
  68. thresh1: null,
  69. /**
  70. * @type {number}
  71. * @default null
  72. */
  73. thresh2: null,
  74. /**
  75. * @type {Boolean}
  76. * @default false
  77. */
  78. isDataLoadedBinding: 'App.router.clusterController.isServiceContentFullyLoaded',
  79. /**
  80. * @type {Em.Object}
  81. * @class
  82. */
  83. widgetConfig: Ember.Object.extend({
  84. thresh1: '',
  85. thresh2: '',
  86. hintInfo: Em.computed.i18nFormat('dashboard.widgets.hintInfo.common', 'maxValue'),
  87. isThresh1Error: false,
  88. isThresh2Error: false,
  89. errorMessage1: "",
  90. errorMessage2: "",
  91. maxValue: 0,
  92. observeThresh1Value: function () {
  93. var thresh1 = this.get('thresh1');
  94. var thresh2 = this.get('thresh2');
  95. var maxValue = this.get('maxValue');
  96. if (thresh1.trim() !== "") {
  97. if (isNaN(thresh1) || thresh1 > maxValue || thresh1 < 0) {
  98. this.set('isThresh1Error', true);
  99. this.set('errorMessage1', Em.I18n.t('dashboard.widgets.error.invalid').format(maxValue));
  100. } else if (this.get('isThresh2Error') === false && parseFloat(thresh2) <= parseFloat(thresh1)) {
  101. this.set('isThresh1Error', true);
  102. this.set('errorMessage1', Em.I18n.t('dashboard.widgets.error.smaller'));
  103. } else {
  104. this.set('isThresh1Error', false);
  105. this.set('errorMessage1', '');
  106. }
  107. } else {
  108. this.set('isThresh1Error', true);
  109. this.set('errorMessage1', Em.I18n.t('admin.users.editError.requiredField'));
  110. }
  111. this.updateSlider();
  112. }.observes('thresh1', 'maxValue'),
  113. observeThresh2Value: function () {
  114. var thresh2 = this.get('thresh2');
  115. var maxValue = this.get('maxValue');
  116. if (thresh2.trim() !== "") {
  117. if (isNaN(thresh2) || thresh2 > maxValue || thresh2 < 0) {
  118. this.set('isThresh2Error', true);
  119. this.set('errorMessage2', Em.I18n.t('dashboard.widgets.error.invalid').format(maxValue));
  120. } else {
  121. this.set('isThresh2Error', false);
  122. this.set('errorMessage2', '');
  123. }
  124. } else {
  125. this.set('isThresh2Error', true);
  126. this.set('errorMessage2', Em.I18n.t('admin.users.editError.requiredField'));
  127. }
  128. this.updateSlider();
  129. }.observes('thresh2', 'maxValue'),
  130. updateSlider: function () {
  131. var thresh1 = this.get('thresh1');
  132. var thresh2 = this.get('thresh2');
  133. // update the slider handles and color
  134. if (this.get('isThresh1Error') === false && this.get('isThresh2Error') === false) {
  135. $("#slider-range")
  136. .slider('values', 0, parseFloat(thresh1))
  137. .slider('values', 1, parseFloat(thresh2));
  138. }
  139. }
  140. }),
  141. didInsertElement: function () {
  142. App.tooltip(this.$("[rel='ZoomInTooltip']"), {
  143. placement: 'left',
  144. template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner graph-tooltip"></div></div>'
  145. });
  146. },
  147. willDestroyElement : function() {
  148. $("[rel='ZoomInTooltip']").tooltip('destroy');
  149. },
  150. /**
  151. * delete widget
  152. * @param {object} event
  153. */
  154. deleteWidget: function () {
  155. var parent = this.get('parentView');
  156. var self = this;
  157. if (App.get('testMode')) {
  158. //update view on dashboard
  159. var objClass = parent.widgetsMapper(this.get('id'));
  160. parent.get('visibleWidgets').removeObject(objClass);
  161. parent.get('hiddenWidgets').pushObject(Em.Object.create({displayName: this.get('title'), id: this.get('id'), checked: false}));
  162. } else {
  163. //reconstruct new persist value then post in persist
  164. parent.getUserPref(parent.get('persistKey')).complete(function () {
  165. self.deleteWidgetComplete.apply(self);
  166. });
  167. }
  168. },
  169. /**
  170. * delete widget complete callback
  171. */
  172. deleteWidgetComplete: function () {
  173. var parent = this.get('parentView');
  174. var oldValue = parent.get('currentPrefObject');
  175. var deletedId = this.get('id');
  176. var newValue = Em.Object.create({
  177. dashboardVersion: oldValue.dashboardVersion,
  178. visible: oldValue.visible.slice(0).without(deletedId),
  179. hidden: oldValue.hidden,
  180. threshold: oldValue.threshold
  181. });
  182. newValue.hidden.push([deletedId, this.get('title')]);
  183. parent.postUserPref(parent.get('persistKey'), newValue);
  184. parent.translateToReal(newValue);
  185. },
  186. /**
  187. * edit widget
  188. * @param {object} event
  189. */
  190. editWidget: function (event) {
  191. var configObj = this.get('widgetConfig').create({
  192. thresh1: this.get('thresh1') + '',
  193. thresh2: this.get('thresh2') + '',
  194. maxValue: parseFloat(this.get('maxValue'))
  195. });
  196. this.showEditDialog(configObj)
  197. },
  198. /**
  199. * show edit dialog
  200. * @param {Em.Object} configObj
  201. * @returns {App.ModalPopup}
  202. */
  203. showEditDialog: function (configObj) {
  204. var self = this;
  205. var maxValue = this.get('maxValue');
  206. return App.ModalPopup.show({
  207. header: Em.I18n.t('dashboard.widgets.popupHeader'),
  208. classNames: ['sixty-percent-width-modal-edit-widget'],
  209. bodyClass: Ember.View.extend({
  210. templateName: require('templates/main/dashboard/edit_widget_popup'),
  211. configPropertyObj: configObj
  212. }),
  213. primary: Em.I18n.t('common.apply'),
  214. onPrimary: function () {
  215. configObj.observeThresh1Value();
  216. configObj.observeThresh2Value();
  217. if (!configObj.isThresh1Error && !configObj.isThresh2Error) {
  218. self.set('thresh1', parseFloat(configObj.get('thresh1')));
  219. self.set('thresh2', parseFloat(configObj.get('thresh2')));
  220. if (!App.get('testMode')) {
  221. // save to persist
  222. var parent = self.get('parentView');
  223. parent.getUserPref(parent.get('persistKey')).complete(function () {
  224. var oldValue = parent.get('currentPrefObject');
  225. oldValue.threshold[parseInt(self.get('id'), 10)] = [configObj.get('thresh1'), configObj.get('thresh2')];
  226. parent.postUserPref(parent.get('persistKey'), oldValue);
  227. });
  228. }
  229. this.hide();
  230. }
  231. },
  232. didInsertElement: function () {
  233. this._super();
  234. var browserVersion = self.getInternetExplorerVersion();
  235. var handlers = [configObj.get('thresh1'), configObj.get('thresh2')];
  236. var colors = [App.healthStatusGreen, App.healthStatusOrange, App.healthStatusRed]; //color green, orange ,red
  237. if (browserVersion === -1 || browserVersion > 9) {
  238. configObj.set('isIE9', false);
  239. configObj.set('isGreenOrangeRed', true);
  240. $("#slider-range").slider({
  241. range: true,
  242. min: 0,
  243. max: maxValue,
  244. values: handlers,
  245. create: function () {
  246. updateColors(handlers);
  247. },
  248. slide: function (event, ui) {
  249. updateColors(ui.values);
  250. configObj.set('thresh1', ui.values[0] + '');
  251. configObj.set('thresh2', ui.values[1] + '');
  252. },
  253. change: function (event, ui) {
  254. updateColors(ui.values);
  255. }
  256. });
  257. function updateColors(handlers) {
  258. var colorstops = colors[0] + ", "; // start with the first color
  259. for (var i = 0; i < handlers.length; i++) {
  260. colorstops += colors[i] + " " + handlers[i] * 100 / maxValue + "%,";
  261. colorstops += colors[i + 1] + " " + handlers[i] * 100 / maxValue + "%,";
  262. }
  263. colorstops += colors[colors.length - 1];
  264. var sliderElement = $('#slider-range');
  265. var css1 = '-webkit-linear-gradient(left,' + colorstops + ')'; // chrome & safari
  266. sliderElement.css('background-image', css1);
  267. var css2 = '-ms-linear-gradient(left,' + colorstops + ')'; // IE 10+
  268. sliderElement.css('background-image', css2);
  269. //$('#slider-range').css('filter', 'progid:DXImageTransform.Microsoft.gradient( startColorStr= ' + colors[0] + ', endColorStr= ' + colors[2] +', GradientType=1 )' ); // IE 10-
  270. var css3 = '-moz-linear-gradient(left,' + colorstops + ')'; // Firefox
  271. sliderElement.css('background-image', css3);
  272. sliderElement.find('.ui-widget-header').css({'background-color': '#FF8E00', 'background-image': 'none'}); // change the original ranger color
  273. }
  274. } else {
  275. configObj.set('isIE9', true);
  276. configObj.set('isGreenOrangeRed', true);
  277. }
  278. }
  279. });
  280. },
  281. /**
  282. * @returns {number}
  283. */
  284. getInternetExplorerVersion: function () {
  285. var rv = -1; //return -1 for other browsers
  286. if (navigator.appName === 'Microsoft Internet Explorer') {
  287. var ua = navigator.userAgent;
  288. var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  289. if (re.exec(ua) != null) {
  290. rv = parseFloat(RegExp.$1); // IE version 1-10
  291. }
  292. }
  293. var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
  294. if (isFirefox) {
  295. return -2;
  296. }
  297. return rv;
  298. },
  299. /**
  300. * for widgets has hidden info(hover info),
  301. * calculate the hover content top number
  302. * based on how long the hiddenInfo is
  303. * @returns {string}
  304. */
  305. hoverContentTopClass: function () {
  306. var lineNum = this.get('hiddenInfo.length');
  307. if (lineNum === 2) {
  308. return "content-hidden-two-line";
  309. }
  310. if (lineNum === 3) {
  311. return "content-hidden-three-line";
  312. }
  313. if (lineNum === 4) {
  314. return "content-hidden-four-line";
  315. }
  316. if (lineNum === 5) {
  317. return "content-hidden-five-line";
  318. }
  319. if (lineNum === 6) {
  320. return "content-hidden-six-line";
  321. }
  322. return '';
  323. }.property('hiddenInfo.length')
  324. });
  325. App.DashboardWidgetView.reopenClass({
  326. class: 'span2p4'
  327. });