metric.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * use: {{view App.MetricFilteringWidget controllerBinding="App.router.mainChartsController"}}
  21. * set controller.metric field with metric value
  22. * widget assign itself to controller like metricWidget (controller.get('metricWidget'))
  23. * @type {*}
  24. */
  25. App.MetricFilteringWidget = Em.View.extend({
  26. metrics:[],
  27. itemView:Em.View.extend({
  28. tagName:'li',
  29. classNameBindings:['disabled'],
  30. disabled:function () {
  31. return this.get('isActive') ? "disabled" : false;
  32. }.property('isActive'),
  33. isActive:function () {
  34. return this.get('metric.value') == this.get('widget.controller.metric');
  35. }.property('widget.controller.metric'),
  36. template:Em.Handlebars.compile('<a {{action activate view.metric.value target="view.widget" }}>{{view.metric.label}}</a>')
  37. }),
  38. metricsConfig:[
  39. { label:Em.I18n.t('metric.default'), value:null},
  40. { label:Em.I18n.t('metric.cpu'), value:'cpu'},
  41. { label:Em.I18n.t('metric.memory'), value:'memory'},
  42. { label:Em.I18n.t('metric.network'), value:'network'},
  43. { label:Em.I18n.t('metric.io'), value:'io'}
  44. ],
  45. allMetrics:function () {
  46. var values = [];
  47. $.each(this.get('metricsConfig'), function () {
  48. if (this.value) {
  49. values.push(this.value);
  50. }
  51. });
  52. return values;
  53. }.property(),
  54. init:function () {
  55. this._super();
  56. var thisW = this;
  57. var controller = this.get('controller');
  58. controller.set('metricWidget', thisW);
  59. this.get('itemView').reopen({
  60. widget:thisW
  61. });
  62. // preparing metric objects
  63. this.get('metricsConfig').forEach(function (config) {
  64. config['widget'] = thisW;
  65. thisW.get('metrics').push(Em.Object.create(config))
  66. });
  67. },
  68. /**
  69. * write active metric to binded controller
  70. * @param event
  71. */
  72. activate:function (event) {
  73. var selected = event.context;
  74. var controller = this.get('controller');
  75. controller.set('metric', selected);
  76. },
  77. templateName:require('templates/common/metric')
  78. })