Overrides.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. define([ "require", "utils/Utils", "lossless-json", "marionette", "backgrid", "asBreadcrumbs", "jquery-placeholder" ], function(require, Utils, LosslessJSON) {
  2. "use strict";
  3. Backbone.$.ajaxSetup({
  4. cache: !1
  5. });
  6. var oldBackboneSync = Backbone.sync;
  7. Backbone.sync = function(method, model, options) {
  8. var that = this;
  9. if (options.queryParam) {
  10. var generateQueryParam = $.param(options.queryParam);
  11. options.url.indexOf("?") !== -1 ? options.url = options.url + "&" + generateQueryParam : options.url = options.url + "?" + generateQueryParam;
  12. }
  13. return oldBackboneSync.apply(this, [ method, model, _.extend(options, {
  14. error: function(response) {
  15. Utils.defaultErrorHandler(that, response, options), that.trigger("error", that, response),
  16. options.cust_error && options.cust_error(that, response);
  17. },
  18. converters: _.extend($.ajaxSettings.converters, {
  19. "text json": function(data) {
  20. try {
  21. return LosslessJSON.parse(data, function(k, v) {
  22. try {
  23. return v.isLosslessNumber ? v.valueOf() : v;
  24. } catch (err) {
  25. return v.value;
  26. }
  27. });
  28. } catch (err) {
  29. if (!("syntaxerror" === err.name.toLowerCase() && data.length > 0 && data.indexOf("<html") > -1)) return $.parseJSON(data);
  30. var redirectUrl = window.location.origin + window.location.pathname;
  31. window.location = redirectUrl.substring(0, redirectUrl.lastIndexOf("/"));
  32. }
  33. }
  34. })
  35. }) ]);
  36. }, String.prototype.trunc = String.prototype.trunc || function(n) {
  37. return this.length > n ? this.substr(0, n - 1) + "..." : this;
  38. }, String.prototype.capitalize = function() {
  39. return this.charAt(0).toUpperCase() + this.slice(1);
  40. }, Backgrid.Column.prototype.defaults.sortType = "toggle";
  41. var cellInit = Backgrid.Cell.prototype.initialize;
  42. Backgrid.Cell.prototype.initialize = function() {
  43. cellInit.apply(this, arguments);
  44. var className = this.column.get("className"), rowClassName = this.column.get("rowClassName");
  45. rowClassName && this.$el.addClass(rowClassName), className && this.$el.addClass(className);
  46. }, Backgrid.HeaderRow = Backgrid.HeaderRow.extend({
  47. render: function() {
  48. var that = this;
  49. return Backgrid.HeaderRow.__super__.render.apply(this, arguments), _.each(this.columns.models, function(modelValue) {
  50. var elAttr = modelValue.get("elAttr"), elAttrObj = null;
  51. elAttr && (_.isFunction(elAttr) ? elAttrObj = elAttr(modelValue) : _.isObject(elAttr) && (elAttrObj = _.isArray(elAttr) ? elAttr : [ elAttr ]),
  52. _.each(elAttrObj, function(val) {
  53. that.$el.find("." + modelValue.get("name")).data(val);
  54. })), modelValue.get("width") && that.$el.find("." + modelValue.get("name")).css("min-width", modelValue.get("width") + "px"),
  55. modelValue.get("fixWidth") && that.$el.find("." + modelValue.get("name")).css("width", modelValue.get("fixWidth") + "px"),
  56. modelValue.get("toolTip") && that.$el.find("." + modelValue.get("name")).attr("title", modelValue.get("toolTip")),
  57. modelValue.get("headerClassName") && that.$el.find("." + modelValue.get("name").replace(".", "\\.")).addClass(modelValue.get("headerClassName"));
  58. }), this;
  59. }
  60. });
  61. var BackgridHeaderInitializeMethod = (Backgrid.HtmlCell = Backgrid.Cell.extend({
  62. className: "html-cell",
  63. render: function() {
  64. this.$el.empty();
  65. var rawValue = this.model.get(this.column.get("name")), formattedValue = this.formatter.fromRaw(rawValue, this.model);
  66. return this.$el.append(formattedValue), this.delegateEvents(), this;
  67. }
  68. }), function(options) {
  69. this.columns = options.columns, this.columns instanceof Backbone.Collection || (this.columns = new Backgrid.Columns(this.columns)),
  70. this.createHeaderRow(), this.listenTo(this.columns, "sort", _.bind(function() {
  71. this.createHeaderRow(), this.render();
  72. }, this));
  73. }), BackgridHeaderCreateHeaderRowMethod = function() {
  74. this.row = new Backgrid.HeaderRow({
  75. columns: this.columns,
  76. collection: this.collection
  77. });
  78. }, BackgridHeaderRenderMethod = function() {
  79. return this.$el.empty(), this.$el.append(this.row.render().$el), this.delegateEvents(),
  80. this.trigger("backgrid:header:rendered", this), this;
  81. };
  82. Backgrid.ExpandableCell = Backgrid.Cell.extend({
  83. accordion: !0,
  84. toggle: '<i style="cursor: pointer;" class="glyphicon toggle pull-left"></i>',
  85. toggleClass: "toggle",
  86. toggleExpandedClass: "fa fa-angle-down",
  87. toggleCollapsedClass: "fa fa-angle-right",
  88. trClass: "expandable",
  89. tdClass: "expandable-content",
  90. events: {
  91. click: "setToggle"
  92. },
  93. initialize: function(options) {
  94. options.accordion && (this.accordion = options.accordion), this.column = options.column,
  95. this.column instanceof Backgrid.Column || (this.column = new Backgrid.Column(this.column));
  96. var column = this.column, model = this.model, $el = this.$el;
  97. Backgrid.callByNeed(column.renderable(), column, model) && $el.addClass("renderable");
  98. },
  99. render: function() {
  100. this.$el.empty();
  101. var isExpand = !0;
  102. return this.column.get("isExpandVisible") && (isExpand = this.column.get("isExpandVisible")(this.$el, this.model)),
  103. this.$toggleEl = $(this.toggle).addClass(this.toggleClass).addClass(this.toggleCollapsedClass),
  104. this.$toggleEl = isExpand ? this.$toggleEl : this.$toggleEl.addClass("noToggle"),
  105. this.$el.append(this.$toggleEl), this.delegateEvents(), this;
  106. },
  107. setToggle: function() {
  108. var detailsRow = this.$el.data("details"), toggle = this.$toggleEl;
  109. if (!detailsRow && this.$toggleEl.hasClass("noToggle")) return !1;
  110. if (detailsRow) $(detailsRow).remove(), this.$el.data("details", null), toggle.removeClass(this.toggleExpandedClass).addClass(this.toggleCollapsedClass); else {
  111. if (this.accordion) {
  112. var table = this.$el.closest("table");
  113. $("." + this.toggleClass, table).filter("." + this.toggleExpandedClass).click();
  114. }
  115. var renderableColumns = this.$el.closest("table").find("th.renderable").length, isRenderable = !1, cellClass = this.tdClass;
  116. Backgrid.callByNeed(this.column.renderable(), this.column, this.model) && (isRenderable = !0,
  117. cellClass += " renderable"), detailsRow = $('<tr class="' + this.trClass + '"></td><td class="' + cellClass + '" colspan="' + (renderableColumns - 1) + '"></td></tr>'),
  118. this.$el.closest("tr").after(detailsRow), this.column.get("expand")(detailsRow.find("td." + this.tdClass), this.model),
  119. this.$el.data("details", detailsRow), toggle.removeClass(this.toggleCollapsedClass).addClass(this.toggleExpandedClass);
  120. }
  121. return this;
  122. }
  123. }), Backgrid.Header.prototype.initialize = BackgridHeaderInitializeMethod, Backgrid.Header.prototype.createHeaderRow = BackgridHeaderCreateHeaderRowMethod,
  124. Backgrid.Header.prototype.render = BackgridHeaderRenderMethod;
  125. var UriCell = Backgrid.UriCell = Backgrid.Cell.extend({
  126. className: "uri-cell",
  127. title: null,
  128. target: "_blank",
  129. initialize: function(options) {
  130. UriCell.__super__.initialize.apply(this, arguments), this.title = options.title || this.title,
  131. this.target = options.target || this.target;
  132. },
  133. render: function() {
  134. this.$el.empty();
  135. var rawValue = this.model.get(this.column.get("name")), href = _.isFunction(this.column.get("href")) ? this.column.get("href")(this.model) : this.column.get("href"), klass = this.column.get("klass"), formattedValue = this.formatter.fromRaw(rawValue, this.model);
  136. if (this.$el.append($("<a>", {
  137. tabIndex: -1,
  138. href: href,
  139. title: this.title || formattedValue,
  140. class: klass
  141. }).text(formattedValue)), this.column.has("iconKlass")) {
  142. var iconKlass = this.column.get("iconKlass"), iconTitle = this.column.get("iconTitle");
  143. this.$el.find("a").append('<i class="' + iconKlass + '" title="' + iconTitle + '"></i>');
  144. }
  145. return this.delegateEvents(), this;
  146. }
  147. });
  148. Backgrid.HeaderHTMLDecodeCell = Backgrid.HeaderCell.extend({
  149. initialize: function(options) {
  150. Backgrid.HeaderCell.prototype.initialize.apply(this, arguments), this.name = _.unescape(this.column.get("label")),
  151. this.$el.addClass(this.name);
  152. },
  153. render: function() {
  154. return this.$el.empty(), this.$el.text(this.name), this.delegateEvents(), this;
  155. }
  156. });
  157. });