ProfileColumnLayoutView.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. define([ "require", "backbone", "hbs!tmpl/profile/ProfileColumnLayoutView_tmpl", "views/graph/ProfileBarChart", "collection/VProfileList", "utils/Utils", "utils/Messages", "utils/Globals", "moment", "models/VEntity", "d3" ], function(require, Backbone, ProfileColumnLayoutViewTmpl, ProfileBarChart, VProfileList, Utils, Messages, Globals, moment, VEntity, d3) {
  2. "use strict";
  3. var ProfileColumnLayoutView = Backbone.Marionette.LayoutView.extend({
  4. _viewName: "ProfileColumnLayoutView",
  5. template: ProfileColumnLayoutViewTmpl,
  6. regions: {},
  7. templateHelpers: function() {
  8. return {
  9. profileData: this.profileData.attributes ? this.profileData.attributes : this.profileData,
  10. entityDetail: this.entityDetail,
  11. typeObject: this.typeObject
  12. };
  13. },
  14. ui: {
  15. backToYear: '[data-id="backToYear"]'
  16. },
  17. events: function() {
  18. var events = {};
  19. return events["click " + this.ui.backToYear] = "backToYear", events;
  20. },
  21. initialize: function(options) {
  22. _.extend(this, _.pick(options, "profileData", "guid", "entityDetail")), this.typeObject = Utils.getProfileTabType(this.profileData.attributes),
  23. this.entityModel = new VEntity(), this.formatValue = d3.format(".0s");
  24. },
  25. fetchEntity: function(argument) {
  26. var that = this;
  27. this.entityModel.getEntity(this.entityDetail.table.guid, {
  28. success: function(data) {
  29. var entity = data.entity, profileData = entity && entity.attributes && entity.attributes.profileData ? entity.attributes.profileData.attributes : null;
  30. profileData && profileData.rowCount && (that.$(".rowValue").show(), that.$(".rowValue .graphval").html("<b>" + that.formatValue(profileData.rowCount).replace("G", "B") + "</b>")),
  31. entity.attributes && (entity.guid && that.$(".table_name .graphval").html('<b><a href="#!/detailPage/' + entity.guid + '?profile=true">' + Utils.getName(entity) + "</a></b>"),
  32. that.$(".table_created .graphval").html("<b>" + moment(entity.attributes.createTime).format("LL") + "</b>"));
  33. }
  34. });
  35. },
  36. bindEvents: function() {},
  37. onRender: function() {
  38. this.fetchEntity(), this.typeObject && "date" === this.typeObject.type && this.$("svg").addClass("dateType");
  39. },
  40. onShow: function() {
  41. this.renderGraph();
  42. },
  43. renderGraph: function(argument) {
  44. if (this.typeObject) {
  45. this.profileData.attributes;
  46. this.data = {
  47. key: this.typeObject.label,
  48. values: this.typeObject.actualObj || []
  49. }, this.updateGraph(this.data);
  50. }
  51. },
  52. backToYear: function() {
  53. this.ui.backToYear.hide(), this.$(".profileGraphDetail").show(), this.monthsData = null,
  54. this.updateGraph(this.data);
  55. },
  56. createMonthData: function(data) {
  57. var monthsKey = {
  58. 1: "Jan",
  59. 2: "Feb",
  60. 3: "Mar",
  61. 4: "Apr",
  62. 5: "May",
  63. 6: "Jun",
  64. 7: "Jul",
  65. 8: "Aug",
  66. 9: "Sep",
  67. 10: "Oct",
  68. 11: "Nov",
  69. 12: "Dec"
  70. }, i = 1;
  71. for (this.monthsData = {
  72. key: this.typeObject.label,
  73. values: []
  74. }; i <= 12; ) this.monthsData.values.push({
  75. value: monthsKey[i],
  76. count: data[i] || 0
  77. }), i++;
  78. this.ui.backToYear.show(), this.$(".profileGraphDetail").hide(), this.updateGraph(this.monthsData);
  79. },
  80. updateGraph: function(data) {
  81. var that = this;
  82. this.$("svg").empty(), ProfileBarChart.render({
  83. el: this.$("svg")[0],
  84. data: data,
  85. xAxisLabel: this.typeObject.xAxisLabel,
  86. yAxisLabel: this.typeObject.yAxisLabel,
  87. formatValue: this.formatValue,
  88. rotateXticks: "date" !== this.typeObject.type,
  89. onBarClick: function(e) {
  90. "date" === that.typeObject.type && (that.monthsData || that.createMonthData(e.monthlyCounts));
  91. }
  92. });
  93. }
  94. });
  95. return ProfileColumnLayoutView;
  96. });