Helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. define([ "require", "utils/Utils", "d3", "marionette", "jquery-ui" ], function(require, Utils, d3) {
  2. "use strict";
  3. _.mixin({
  4. numberFormatWithComma: function(number) {
  5. return d3.format(",")(number);
  6. },
  7. numberFormatWithBytes: function(number) {
  8. if (number > -1) {
  9. if (0 === number) return "0 Bytes";
  10. var i = 0 == number ? 0 : Math.floor(Math.log(number) / Math.log(1024));
  11. return i > 8 ? _.numberFormatWithComma(number) : Number((number / Math.pow(1024, i)).toFixed(2)) + " " + [ "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ][i];
  12. }
  13. return number;
  14. },
  15. isEmptyArray: function(val) {
  16. return !(!val || !_.isArray(val)) && _.isEmpty(val);
  17. },
  18. toArrayifObject: function(val) {
  19. return _.isObject(val) ? [ val ] : val;
  20. },
  21. startsWith: function(str, matchStr) {
  22. return str && matchStr && _.isString(str) && _.isString(matchStr) ? 0 === str.lastIndexOf(matchStr, 0) : void 0;
  23. },
  24. isUndefinedNull: function(val) {
  25. return !(!_.isUndefined(val) && !_.isNull(val));
  26. },
  27. trim: function(val) {
  28. return val && val.trim ? val.trim() : val;
  29. },
  30. isTypePrimitive: function(type) {
  31. return "int" === type || "byte" === type || "short" === type || "long" === type || "float" === type || "double" === type || "string" === type || "boolean" === type || "date" === type;
  32. }
  33. });
  34. var getPopoverEl = function(e) {
  35. return $(e.target).parent().data("bs.popover") || $(e.target).data("bs.popover") || $(e.target).parents(".popover").length;
  36. };
  37. if ($(document).on("click DOMMouseScroll mousewheel", function(e) {
  38. if (e.originalEvent) {
  39. var isPopOverEl = getPopoverEl(e);
  40. isPopOverEl ? isPopOverEl.$tip && $(".popover").not(isPopOverEl.$tip).popover("hide") : $(".popover").popover("hide"),
  41. $(".tooltip").tooltip("hide");
  42. }
  43. }), $("body").on("hidden.bs.popover", function(e) {
  44. $(e.target).data("bs.popover").inState = {
  45. click: !1,
  46. hover: !1,
  47. focus: !1
  48. };
  49. }), $("body").on("show.bs.popover", '[data-js="popover"]', function() {
  50. $(".popover").not(this).popover("hide");
  51. }), $("body").on("keypress", "input.number-input,.number-input .select2-search__field", function(e) {
  52. if (8 != e.which && 0 != e.which && (e.which < 48 || e.which > 57)) return !1;
  53. }), $("body").on("keypress", "input.number-input-negative,.number-input-negative .select2-search__field", function(e) {
  54. if (8 != e.which && 0 != e.which && (e.which < 48 || e.which > 57)) {
  55. if (45 != e.which) return !1;
  56. if (this.value.length) return !1;
  57. }
  58. }), $("body").on("keypress", "input.number-input-exponential,.number-input-exponential .select2-search__field", function(e) {
  59. if (8 != e.which && 0 != e.which && (e.which < 48 || e.which > 57) && 69 != e.which && 101 != e.which && 43 != e.which && 45 != e.which && 46 != e.which && 190 != e.which) return !1;
  60. }), $("body").on("click", ".dropdown-menu.dropdown-changetitle li a", function() {
  61. $(this).parents("li").find(".btn:first-child").html($(this).text() + ' <span class="caret"></span>');
  62. }), $("body").on("click", ".btn", function() {
  63. $(this).blur();
  64. }), $("body").on("keyup input", ".modal-body", function(e) {
  65. var target = e.target, isGlossary = "searchTerm" === e.target.dataset.id || "searchCategory" === e.target.dataset.id;
  66. if (("text" === target.type || "textarea" === target.type) && !isGlossary) {
  67. var $this = $(this), $footerButton = $this.parents(".modal").find(".modal-footer button.ok"), requiredInputField = _.filter($this.find("input"), function($e) {
  68. if ($e.getAttribute("placeholder") && $e.getAttribute("placeholder").indexOf("require") >= 0) return "" == $e.value.trim();
  69. });
  70. requiredInputField.length > 0 ? $footerButton.attr("disabled", "true") : $footerButton.removeAttr("disabled");
  71. }
  72. }), $.fn.select2 && ($.fn.select2.amd.define("TagHideDeleteButtonAdapter", [ "select2/utils", "select2/selection/multiple", "select2/selection/placeholder", "select2/selection/eventRelay", "select2/selection/search" ], function(Utils, MultipleSelection, Placeholder, EventRelay, SelectionSearch) {
  73. var adapter = Utils.Decorate(MultipleSelection, Placeholder);
  74. return adapter = Utils.Decorate(adapter, SelectionSearch), adapter = Utils.Decorate(adapter, EventRelay),
  75. adapter.prototype.render = function() {
  76. var $search = $('<li class="select2-search select2-search--inline"><input class="select2-search__field" type="search" tabindex="-1" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" /></li>');
  77. this.$searchContainer = $search, this.$search = $search.find("input");
  78. var $selection = MultipleSelection.prototype.render.call(this);
  79. return this._transferTabIndex(), $selection;
  80. }, adapter.prototype.update = function(data) {
  81. var that = this;
  82. if (this.clear(), 0 === data.length) return this.$selection.find(".select2-selection__rendered").append(this.$searchContainer),
  83. void this.$search.attr("placeholder", this.options.get("placeholder"));
  84. this.$search.attr("placeholder", "");
  85. var $rendered = this.$selection.find(".select2-selection__rendered"), $selectionContainer = [];
  86. data.length > 0 && (_.each(data, function(obj) {
  87. var $container = $('<li class="select2-selection__choice"></li>'), formatted = that.display(obj, $rendered), $remove = $('<span class="select2-selection__choice__remove" role="presentation">&times;</span>'), allowRemoveAttr = $(obj.element).data("allowremove"), allowRemove = void 0 === obj.allowRemove ? allowRemoveAttr : obj.allowRemove;
  88. void 0 !== allowRemove && allowRemove === !1 || $container.append($remove), $container.data("data", obj),
  89. $container.append(formatted), $selectionContainer.push($container);
  90. }), Utils.appendMany($rendered, $selectionContainer));
  91. var searchHadFocus = this.$search[0] == document.activeElement;
  92. this.$search.attr("placeholder", ""), this.$selection.find(".select2-selection__rendered").append(this.$searchContainer),
  93. this.resizeSearch(), searchHadFocus && this.$search.focus();
  94. }, adapter;
  95. }), $.fn.select2.amd.define("ServiceTypeFilterDropdownAdapter", [ "select2/utils", "select2/dropdown", "select2/dropdown/attachBody", "select2/dropdown/attachContainer", "select2/dropdown/search", "select2/dropdown/minimumResultsForSearch", "select2/dropdown/closeOnSelect" ], function(Utils, Dropdown, AttachBody, AttachContainer, Search, MinimumResultsForSearch, CloseOnSelect) {
  96. var dropdownWithSearch = Utils.Decorate(Utils.Decorate(Dropdown, CloseOnSelect), Search);
  97. dropdownWithSearch.prototype.render = function() {
  98. var $rendered = Dropdown.prototype.render.call(this), dropdownCssClass = this.options.get("dropdownCssClass");
  99. dropdownCssClass && $rendered.addClass(dropdownCssClass);
  100. var placeholder = this.options.get("placeholderForSearch") || "", $search = $('<span class="select2-search select2-search--dropdown"><div class="clearfix"><div class="col-md-10 no-padding" style="width: calc(100% - 30px);"><input class="select2-search__field" placeholder="' + placeholder + '" type="search" tabindex="-1" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" /></div><div class="col-md-2 no-padding" style="width: 30px;"><button type="button" style="padding: 3px 6px;margin: 0px 4px;" class="btn btn-action btn-sm filter " title="类型搜索"><i class="fa fa-filter"></i></button></div></div></span>');
  101. if (!this.options.options.getFilterBox) throw "In order to render the filter options adapter needed getFilterBox function";
  102. var $Filter = $('<ul class="type-filter-ul"></ul>');
  103. return this.$Filter = $Filter, this.$Filter.append(this.options.options.getFilterBox()),
  104. this.$Filter.hide(), this.$searchContainer = $search, $Filter.find('input[type="checkbox"]:checked').length ? $search.find("button.filter").addClass("active") : $search.find("button.filter").removeClass("active"),
  105. this.$search = $search.find("input"), $rendered.prepend($search), $rendered.append($Filter),
  106. $rendered;
  107. };
  108. var oldDropdownWithSearchBindRef = dropdownWithSearch.prototype.bind;
  109. dropdownWithSearch.prototype.bind = function(container, $container) {
  110. var self = this;
  111. oldDropdownWithSearchBindRef.call(this, container, $container);
  112. var self = this;
  113. this.$Filter.on("click", "li", function() {
  114. var itemCallback = self.options.options.onFilterItemSelect;
  115. itemCallback && itemCallback(this);
  116. }), this.$searchContainer.find("button.filter").click(function() {
  117. container.$dropdown.find(".select2-search").hide(150), container.$dropdown.find(".select2-results").hide(150),
  118. self.$Filter.html(self.options.options.getFilterBox()), self.$Filter.show();
  119. }), this.$Filter.on("click", "button.filterDone", function() {
  120. container.$dropdown.find(".select2-search").show(150), container.$dropdown.find(".select2-results").show(150),
  121. self.$Filter.hide();
  122. var filterSubmitCallback = self.options.options.onFilterSubmit;
  123. filterSubmitCallback && filterSubmitCallback({
  124. filterVal: _.map(self.$Filter.find('input[type="checkbox"]:checked'), function(item) {
  125. return $(item).data("value");
  126. })
  127. });
  128. }), container.$element.on("hideFilter", function() {
  129. container.$dropdown.find(".select2-search").show(), container.$dropdown.find(".select2-results").show(),
  130. self.$Filter.hide();
  131. });
  132. };
  133. var adapter = Utils.Decorate(dropdownWithSearch, AttachContainer);
  134. return adapter = Utils.Decorate(adapter, AttachBody);
  135. })), $.widget("custom.atlasAutoComplete", $.ui.autocomplete, {
  136. _create: function() {
  137. this._super(), this.widget().menu("option", "items", "> :not(.ui-autocomplete-category,.empty)");
  138. },
  139. _renderMenu: function(ul, items) {
  140. var that = this, currentCategory = "";
  141. items = _.sortBy(items, "order"), $.each(items, function(index, item) {
  142. item.category != currentCategory && (ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"),
  143. currentCategory = item.category), that._renderItemData(ul, item);
  144. });
  145. },
  146. _renderItemData: function(ul, item) {
  147. return this._renderItem(ul, item);
  148. }
  149. }), !("placeholder" in HTMLInputElement.prototype)) {
  150. var originalRender = Backbone.Marionette.LayoutView.prototype.render;
  151. Backbone.Marionette.LayoutView.prototype.render = function() {
  152. originalRender.apply(this, arguments), this.$("input, textarea").placeholder();
  153. };
  154. }
  155. $("body").on("click", "pre.code-block .expand-collapse-button", function(e) {
  156. var $el = $(this).parents(".code-block");
  157. $el.hasClass("shrink") ? $el.removeClass("shrink") : $el.addClass("shrink");
  158. }), $("body").on("mouseenter", ".select2-selection__choice", function() {
  159. $(this).attr("title", "");
  160. }), $("body").tooltip && $("body").tooltip({
  161. selector: '[title]:not(".select2-selection__choice,.select2-selection__rendered")',
  162. placement: function() {
  163. return this.$element.attr("data-placement") || "bottom";
  164. },
  165. container: "body"
  166. }), $(window).on("popstate", function() {
  167. $("body").find(".modal-dialog .close").click();
  168. });
  169. });