BasePane.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', 'q', 'doT', 'bi/commons/utils/BidiUtil', 'bi/commons/utils/Utils', 'bi/commons/ui/AccessibleView', 'bi/admin/common/ui/ToggleMenuBar', 'text!bi/admin/common/templates/BasePane.html', 'bi/admin/nls/StringResource'], function (_, Q, dot, BidiUtil, Utils, AccessibleView, ToggleMenuBar, htmlPane, StringResource) {
  9. //NOSONAR: needed for amd
  10. /**
  11. * Sample content view that extends the glass View class
  12. *
  13. */
  14. var BasePane = AccessibleView.extend({
  15. title: '',
  16. showHeader: true,
  17. showGobackButton: false,
  18. showSwitchViewButton: false,
  19. autoValidate: false,
  20. flexBody: true,
  21. isWideView: false,
  22. init: function init(options) {
  23. BasePane.inherited('init', this, arguments);
  24. $.extend(this, options);
  25. var basePaneInfo = {};
  26. basePaneInfo.strings = {
  27. 'goback': StringResource.get('goback'),
  28. 'switchViews': StringResource.get('switchViews')
  29. };
  30. var sHtml = dot.template(htmlPane)(basePaneInfo);
  31. this.$el.append(sHtml);
  32. this.$el.addClass('bi-admin-vflex bi-admin-fullheight');
  33. },
  34. _setTextDirection: function _setTextDirection(elems) {
  35. for (var i = 0; i < elems.length; i++) {
  36. BidiUtil.initElementForBidi(elems[i]);
  37. }
  38. },
  39. _autoValidate: function _autoValidate() {
  40. var self = this;
  41. if (!this.autoValidate) {
  42. return;
  43. }
  44. this.$el.find("[required]").on({
  45. blur: this._onBlur.bind(self),
  46. invalid: this._onInvalid,
  47. focus: this._onFocus
  48. });
  49. },
  50. _onBlur: function _onBlur(e) {
  51. if (this._checkSpace(e.target)) {
  52. this._checkScript(e.target);
  53. }
  54. },
  55. _onInvalid: function _onInvalid(e) {
  56. if (e.target.validity.valueMissing) {
  57. $(e.target.parentNode).append($("<p class='invalid'> </p>").append($("<label class='invalid'/>").text(StringResource.get("required"))));
  58. } else if (e.target.validity.typeMismatch || e.target.validity.patternMismatch) {
  59. $(e.target.parentNode).append($("<p class='invalid'> </p>").append($("<label class='invalid'/>").text(StringResource.get("invalid"))));
  60. }
  61. },
  62. _onFocus: function _onFocus(e) {
  63. $(e.target.parentNode).find(".invalid").remove();
  64. },
  65. setFocus: function setFocus() {
  66. var elem = this.$el.find('.bi-admin-base-pane');
  67. elem.focus();
  68. },
  69. onHide: function onHide() {},
  70. onToggleMenuChanged: function onToggleMenuChanged() {},
  71. renderToggleMenu: function renderToggleMenu(el, name, items, defaultValue, rightIcon) {
  72. var menuItems = _.map(items, function (item, key) {
  73. return {
  74. name: name + key,
  75. icon: 'wft_checkmark',
  76. label: StringResource.get(item),
  77. checked: item === defaultValue,
  78. action: function () {
  79. this.onToggleMenuChanged(name, item);
  80. }.bind(this)
  81. };
  82. }.bind(this));
  83. var filterSpec = {
  84. 'baseClass': "appbar",
  85. 'label': StringResource.get('type'),
  86. 'name': name,
  87. 'labelOnly': true,
  88. 'hcLabel': false,
  89. 'supportCustomCollapse': true,
  90. 'showTitle': false,
  91. 'updateLabel': true,
  92. 'icon': 'common-titan-arrow-down',
  93. 'iconRightAlign': !!rightIcon,
  94. 'bSVG': true,
  95. 'el': el,
  96. 'items': menuItems
  97. };
  98. var toggleFilter = new ToggleMenuBar(filterSpec);
  99. toggleFilter.render();
  100. },
  101. render: function render() {
  102. var deferred = Q.defer();
  103. this.$header = this.$el.find('div.bi-admin-pane-header');
  104. this.$body = this.$el.find('div.bi-admin-pane-body');
  105. if (this.isWideView) {
  106. this.$body.addClass('bi-admin-widepane');
  107. }
  108. if (this.flexBody) {
  109. this.$body.addClass('bi-admin-pane-flex');
  110. }
  111. if (this.showHeader) {
  112. this.renderHeader(this.$header);
  113. } else {
  114. this.$header.hide();
  115. this.$body.addClass('noheader');
  116. } // roll back temporarily, in future we need change renderBody() to a async method
  117. this.renderBody(this.$body).done(function () {
  118. this._localize();
  119. this._autoValidate();
  120. var $paneBody = this.$el.find(".bi-admin-pane-body");
  121. $paneBody.attr("aria-label", this.title);
  122. $paneBody.attr("role", "group");
  123. deferred.resolve(this);
  124. }.bind(this));
  125. return deferred.promise;
  126. },
  127. onHeaderButtonClicked: function onHeaderButtonClicked(id) {
  128. if (id === 'switchButton') {
  129. this._switchView();
  130. }
  131. },
  132. onHeaderChecked: function onHeaderChecked() {},
  133. _createCheckbox: function _createCheckbox(id, text, checked) {
  134. var self = this;
  135. var $item = $('<div class="bi-admin-checkbox"></div>');
  136. $item.attr('id', id);
  137. var $checkBox = $('<input type="checkbox"></input>');
  138. $checkBox.prop('checked', checked);
  139. $checkBox.attr('aria-label', text);
  140. $checkBox.on('change', function () {
  141. var $chk = $(this);
  142. self.onHeaderChecked(id, $chk.is(':checked'));
  143. });
  144. var $label = $('<span/>');
  145. $label.text(text);
  146. $item.append($checkBox);
  147. $item.append($label);
  148. return $item;
  149. },
  150. _createButton: function _createButton(id, icon, title, cls) {
  151. var $btn = $('<button />');
  152. $btn.attr('id', id);
  153. $btn.attr('title', title || "");
  154. $btn.addClass('bi-admin-button');
  155. if (cls) {
  156. $btn.addClass(cls);
  157. }
  158. Utils.setIcon($btn, icon);
  159. $btn.on('primaryaction', function () {
  160. this.onHeaderButtonClicked(id);
  161. }.bind(this));
  162. return $btn;
  163. },
  164. renderHeader: function renderHeader($header) {
  165. $header[this.showGobackButton ? 'addClass' : 'removeClass']('show-goback');
  166. if (this.showGobackButton === true) {
  167. var $backButton = $header.find('.goback');
  168. $backButton.find(".goback-icon").attr("aria-label", StringResource.get('goBack'));
  169. $backButton.on('primaryaction', function () {
  170. this.slideout.hide();
  171. }.bind(this));
  172. }
  173. var $buttonBar = this.$header.find('div.bi-admin-trailing .bi-admin-buttonbar');
  174. if (this.showSwitchViewButton) {
  175. var switchButton = this._createButton('switchButton', this.isWideView ? 'common-left_collapse' : 'common-right_expand', StringResource.get('switchViews'));
  176. $buttonBar.append(switchButton);
  177. }
  178. $header.find('div.bi-admin-leading .title').text(this.title);
  179. },
  180. _switchView: function _switchView() {
  181. var so = this._getSwitchOptions() || {};
  182. var content = _.extend({
  183. 'showSwitchViewButton': true,
  184. 'module': this._getSwitchedModuleName()
  185. }, so);
  186. this.glassContext.appController.hideSlideOut();
  187. this.glassContext.appController.showSlideOut({
  188. 'position': 'left',
  189. 'width': this._getViewWidth(),
  190. 'closeSlideouts': true,
  191. 'content': content
  192. });
  193. },
  194. //should be override by the pane if it support view switch
  195. _getSwitchedModuleName: function _getSwitchedModuleName() {},
  196. _getSwitchOptions: function _getSwitchOptions() {},
  197. _getViewWidth: function _getViewWidth() {
  198. var retVal = 450;
  199. if (!this.isWideView) {
  200. retVal = $(window).width() < 1370 ? 576 : 800;
  201. }
  202. return retVal;
  203. },
  204. renderBody: function renderBody() {},
  205. _localize: function _localize($el) {
  206. if (!this._stringResource) {
  207. return;
  208. }
  209. var self = this;
  210. $el = $el || this.$el;
  211. $el.find('[local_id]').each(function () {
  212. var $this = $(this);
  213. var localId = $this.attr('local_id');
  214. $this.text(self._stringResource.get(localId));
  215. });
  216. $el.find('[local_title]').each(function () {
  217. var $this = $(this);
  218. var localTitle = $this.attr('local_title');
  219. $this.attr('title', self._stringResource.get(localTitle));
  220. });
  221. },
  222. /**
  223. * Check space input
  224. */
  225. _checkSpace: function _checkSpace(el) {
  226. var retVal = true;
  227. var reg = /^\s*$/;
  228. $(el.parentNode).find(".invalid").remove();
  229. if ($(el).val() && reg.test($(el).val())) {
  230. $(el.parentNode).append($("<p class='invalid'> </p>").append($("<label class='invalid'/>").text(StringResource.get("invalid"))));
  231. retVal = false;
  232. }
  233. return retVal;
  234. },
  235. _checkScript: function _checkScript(el) {
  236. var retVal = true;
  237. $(el.parentNode).find(".invalid").remove();
  238. if ($(el).val().indexOf('<script') !== -1) {
  239. $(el.parentNode).append($("<p class='invalid'> </p>").append($("<label class='invalid'/>").text(StringResource.get("invalid"))));
  240. retVal = false;
  241. }
  242. return retVal;
  243. },
  244. validate: function validate() {
  245. var self = this;
  246. var valid = true;
  247. this.$el.find(".invalid").remove();
  248. this.$el.find("[required]").each(function () {
  249. if ((self._checkSpace(this) && self._checkScript(this)) === false) {
  250. valid = false;
  251. return false;
  252. }
  253. });
  254. return valid;
  255. },
  256. close: function close() {
  257. this.slideout.hide();
  258. },
  259. isSysAdmin: function isSysAdmin() {
  260. return this.glassContext.getCoreSvc('.UserProfile').isSysAdmin();
  261. },
  262. isTenantAdmin: function isTenantAdmin() {
  263. return this.glassContext.getCoreSvc('.UserProfile').isTenantAdmin();
  264. }
  265. });
  266. return BasePane;
  267. });