widget.js 12 KB

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