widget.js 11 KB

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