FormatPickerView.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: SHARE
  5. *
  6. * (C) Copyright IBM Corp. 2015, 2019
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  9. * IBM Corp.
  10. */
  11. define([
  12. 'underscore',
  13. 'jquery',
  14. 'q',
  15. 'bi/sharecommon/utils/simpledoT',
  16. 'bi/commons/ui/View',
  17. 'bi/sharecommon/utils/translator',
  18. 'bi/commons/utils/Utils',
  19. 'bi/commons/utils/ContentFormatter',
  20. 'text!bi/schedule/templates/FormatPicker.html',
  21. 'bi/commons/ui/properties/CheckBox',
  22. 'bi/content_apps/authoring/AuthoringHelper',
  23. 'bootstrap'
  24. ], function(_, $, Q, dot, View, t, Utils, stringFormatter, template ,CheckBox, AuthoringHelper) {
  25. 'use strict';
  26. var __SLIDEOUT_PRESET_WIDTH = '350px';
  27. var formatPickerView = View.extend({
  28. init: function(options) {
  29. formatPickerView.inherited('init', this, arguments);
  30. this._initVariables();
  31. if(options.$toggler === undefined) {
  32. throw "Error: $toggler parameter must be provided.";
  33. }
  34. $.extend(this, options);
  35. // Default the outputFormats
  36. // TODO update this to use user preference default option
  37. if(!Array.isArray(this.outputFormats) || this.outputFormats.length === 0) {
  38. if(this.showHTML) {
  39. this.outputFormats = ['HTML'];
  40. } else if ((this.showPDF) && (AuthoringHelper.userCanGenerateFormat(options.glassContext, 'PDF'))){
  41. this.outputFormats = ['PDF'];
  42. } else if ((this.showExcel) && (AuthoringHelper.userCanGenerateFormat(options.glassContext, 'spreadsheetML'))) {
  43. this.outputFormats = ['spreadsheetML'];
  44. } else if ((this.showCSV) && (AuthoringHelper.userCanGenerateFormat(options.glassContext, 'CSV'))) {
  45. this.outputFormats = ['CSV'];
  46. } else if ((this.showXML) && (AuthoringHelper.userCanGenerateFormat(options.glassContext, 'XML'))) {
  47. this.outputFormats = ['XML'];
  48. } else {
  49. this.outputFormats = ['HTML'];
  50. }
  51. }
  52. // Check if formats contain unsupported format(s)
  53. if ( _.contains(this.outputFormats, 'XLWA') ) {
  54. this.showUnsupported = true;
  55. }
  56. },
  57. getOutputFormats: function() {
  58. return this.outputFormats;
  59. },
  60. render: function() {
  61. var htmlGenerator = dot.simpleTemplate(template);
  62. var attributes = {
  63. schedule_done_btn: t.translate("schedule_delivery_done_label"),
  64. uniqueId: this.uniqueId,
  65. showHTML: this.showHTML,
  66. showPDF: this.showPDF,
  67. showExcel: this.showExcel,
  68. showCSV: this.showCSV,
  69. showXML: this.showXML,
  70. showUnsupported: this.showUnsupported
  71. };
  72. this._popoverContent = htmlGenerator(attributes);
  73. this.$popoverContent = $(this._popoverContent);
  74. this._renderCheckBoxes(this.$popoverContent);
  75. // Initially update the "Format:" area in the subscribe pane
  76. this._updateFormatList(this.$popoverContent);
  77. // in update mode the permissions are passed
  78. if ( this.hasPermission && !this.hasPermission.write && this.hasPermission.read ) {
  79. this.$toggler.prop('disabled', true);
  80. }
  81. this._setEvents();
  82. return Q(this);
  83. },
  84. _renderCheckBoxes: function($popoverContent) {
  85. this.htmlCheckbox = new CheckBox({
  86. 'id': 'output_html_checkbox_' + this.uniqueId,
  87. 'el': $popoverContent.find('.html_format_option_' + this.uniqueId),
  88. 'name': 'HTML',
  89. 'label': t.translate(this.formatLabels.HTML),
  90. 'ariaLabel': t.translate(this.formatLabels.HTML),
  91. 'svgIcon': 'html_icon',
  92. 'checked': _.contains(this.outputFormats, 'HTML'),
  93. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  94. });
  95. this.pdfCheckbox = new CheckBox({
  96. 'id': 'output_pdf_checkbox_' + this.uniqueId,
  97. 'el': $popoverContent.find('.pdf_format_option_' + this.uniqueId),
  98. 'name': 'PDF',
  99. 'label': t.translate(this.formatLabels.PDF),
  100. 'ariaLabel': t.translate(this.formatLabels.PDF),
  101. 'svgIcon': 'pdf_icon',
  102. 'checked': _.contains(this.outputFormats, 'PDF'),
  103. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  104. });
  105. this.spreadsheetCheckbox = new CheckBox({
  106. 'id': 'output_spreadsheet_checkbox_' + this.uniqueId,
  107. 'el': $popoverContent.find('.spreadsheet_format_option_' + this.uniqueId),
  108. 'name': 'spreadsheetML',
  109. 'label': t.translate(this.formatLabels.spreadsheetML),
  110. 'ariaLabel': t.translate(this.formatLabels.spreadsheetML),
  111. 'svgIcon': 'excel_icon',
  112. 'checked': _.contains(this.outputFormats, 'spreadsheetML'),
  113. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  114. });
  115. this.xlsxDataCheckbox = new CheckBox({
  116. 'id': 'output_xlsxdata_checkbox_' + this.uniqueId,
  117. 'el': $popoverContent.find('.xlsxdata_format_option_' + this.uniqueId),
  118. 'name': 'xlsxData',
  119. 'label': t.translate(this.formatLabels.xlsxData),
  120. 'ariaLabel': t.translate(this.formatLabels.xlsxData),
  121. 'svgIcon': 'excel_icon',
  122. 'checked': _.contains(this.outputFormats, 'xlsxData'),
  123. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  124. });
  125. this.csvCheckbox = new CheckBox({
  126. 'id': 'output_csv_checkbox_' + this.uniqueId,
  127. 'el': $popoverContent.find('.csv_format_option_' + this.uniqueId),
  128. 'name': 'CSV',
  129. 'label': t.translate(this.formatLabels.CSV),
  130. 'ariaLabel': t.translate(this.formatLabels.CSV),
  131. 'svgIcon': 'csv_icon',
  132. 'checked': _.contains(this.outputFormats, 'CSV'),
  133. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  134. });
  135. this.xmlCheckbox = new CheckBox({
  136. 'id': 'output_xml_checkbox_' + this.uniqueId,
  137. 'el': $popoverContent.find('.xml_format_option_' + this.uniqueId),
  138. 'name': 'XML',
  139. 'label': t.translate(this.formatLabels.XML),
  140. 'ariaLabel': t.translate(this.formatLabels.XML),
  141. 'svgIcon': 'xml_icon',
  142. 'checked': _.contains(this.outputFormats, 'XML'),
  143. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  144. });
  145. this.unsupportedCheckbox = new CheckBox({
  146. 'id': 'output_unsupported_checkbox_' + this.uniqueId,
  147. 'el': $popoverContent.find('.unsupported_format_option_' + this.uniqueId),
  148. 'name': 'XLWA',
  149. 'label': t.translate(this.formatLabels.XLWA),
  150. 'ariaLabel': t.translate(this.formatLabels.XLWA),
  151. 'svgIcon': 'unsupportedformat_icon',
  152. 'checked': _.contains(this.outputFormats, 'XLWA'),
  153. 'onChange': function(name, value) {this._handleCheckBoxes(name, value, $popoverContent);}.bind(this)
  154. });
  155. if (this.showHTML) {
  156. this.htmlCheckbox.doRender();
  157. }
  158. if (this.showPDF) {
  159. this.pdfCheckbox.doRender();
  160. }
  161. if (this.showExcel) {
  162. this.spreadsheetCheckbox.doRender();
  163. this.xlsxDataCheckbox.doRender();
  164. }
  165. if (this.showCSV) {
  166. this.csvCheckbox.doRender();
  167. }
  168. if (this.showXML) {
  169. this.xmlCheckbox.doRender();
  170. }
  171. if (this.showUnsupported) {
  172. this.unsupportedCheckbox.doRender();
  173. }
  174. },
  175. _handleCheckBoxes: function(name, value, $popoverContent) {
  176. this._updateOutputFormats(name);
  177. // Force first shown format to be used if everything has been unchecked
  178. if(this.outputFormats.length === 0) {
  179. if (this.showHTML) {
  180. this.htmlCheckbox.check();
  181. } else if (this.showPDF) {
  182. this.pdfCheckbox.check();
  183. } else if (this.showExcel) {
  184. this.spreadsheetCheckbox.check();
  185. } else if (this.showCSV) {
  186. this.csvCheckbox.check();
  187. } else if (this.showXML) {
  188. this.xmlCheckbox.check();
  189. } else {
  190. // should not get here because of check in init
  191. console.error("No checkboxes shown, nothing to select.");
  192. }
  193. }
  194. this._updateFormatList($popoverContent);
  195. },
  196. /* Functions that are internally used for this object are below */
  197. _setEvents: function() {
  198. this.$toggler.on('primaryaction', function(event) {
  199. this.isVisible = !this.isVisible;
  200. if ( !_.contains(this.outputFormats, 'XLWA') ) {
  201. this.showUnsupported = false;
  202. }
  203. if(this.isVisible) {
  204. var $updatedContent = $(this._popoverContent);
  205. this._renderCheckBoxes($updatedContent);
  206. this.glassContext.appController.showSlideOut({
  207. 'parent': this.slideoutparent,
  208. 'overlay': this.overlay,
  209. 'width': __SLIDEOUT_PRESET_WIDTH,
  210. 'enableTabLooping': true,
  211. 'label': t.translate("schedule_format_picker_name"),
  212. 'onHide': this._close(),
  213. 'content': {
  214. 'module': 'bi/schedule/views/OptionsSlideoutView',
  215. 'content': $updatedContent,
  216. 'title': t.translate("schedule_output_label")
  217. }
  218. });
  219. }
  220. }.bind(this));
  221. // This handles the user clicking away from the popover
  222. this.hideHandler = function(event) {
  223. var $event = $(event.target);
  224. var $p = $event.closest(".popover");
  225. var $f = $event.closest('.output_format_toggler');
  226. if(this.isVisible) {
  227. if($p.length === 0 && $f.length === 0) {
  228. this._close();
  229. }
  230. }
  231. }.bind(this);
  232. $(document).on('clicktap', this.hideHandler);
  233. },
  234. _close: function() {
  235. this.isVisible = false;
  236. },
  237. _updateFormatList: function($popoverContent) {
  238. if(typeof $popoverContent === 'object') {
  239. this.$el.html('');
  240. for(var i = 0; i < this.outputFormats.length; i++) {
  241. var format = this.outputFormats[i];
  242. var $link = $("<div/>").addClass('subscription_link').html(t.translate(this.formatLabels[format]));
  243. $("<div/>").addClass('format_type_container').append(
  244. "<span class='subscription_icon' data-icon='" + this.formatIcons[format] + "'></span>").append(
  245. $link).appendTo(this.$el);
  246. }
  247. this._setActiveOutputSvg();
  248. }
  249. },
  250. _setActiveOutputSvg: function() {
  251. var icons = this.$el.find(".subscription_icon");
  252. var links = this.$el.find(".subscription_link");
  253. for(var i = 0; i < icons.length; i++) {
  254. var $icon = $(icons[i]);
  255. Utils.setIcon($icon, 'common-' + $icon.data('icon') + "_icon", $(links[i]).html());
  256. }
  257. for(var j = 0; j < links.length; j++) {
  258. stringFormatter.middleShortenString(links[j]);
  259. }
  260. },
  261. _updateOutputFormats: function(format) {
  262. if(_.contains(this.outputFormats, format)) {
  263. this._removeOutputFormats(format);
  264. } else if(!_.contains(this.outputFormats, format)) {
  265. this.outputFormats.push(format);
  266. }
  267. },
  268. _removeOutputFormats: function(format) {
  269. var index = this.outputFormats.indexOf(format);
  270. if(index >= 0) {
  271. this.outputFormats.splice(index, 1);
  272. }
  273. },
  274. _initVariables: function() {
  275. this.$el = {};
  276. this.$toggler = {};
  277. this.isVisible = false;
  278. this.showHTML = true;
  279. this.showPDF = true;
  280. this.showExcel = true;
  281. this.showCSV = true;
  282. this.showXML = true;
  283. this.showUnsupported = false;
  284. this.placement = 'left';
  285. this.outputFormats = [];
  286. this._popoverContent = '';
  287. this.formatLabels = {
  288. HTML: "schedule_format_web",
  289. PDF: "schedule_format_pdf",
  290. spreadsheetML: "schedule_format_xlsx",
  291. xlsxData: "schedule_format_xlsx_data",
  292. CSV: "schedule_format_csv",
  293. XML: "schedule_format_xml",
  294. XLWA: "schedule_format_unsupported"
  295. };
  296. this.formatIcons = {
  297. HTML: "html",
  298. PDF: "pdf",
  299. spreadsheetML: "excel",
  300. xlsxData: "excel",
  301. CSV: "csv",
  302. XML: "xml",
  303. XLWA: "unsupportedformat"
  304. };
  305. this.uniqueId = _.uniqueId('id_');
  306. }
  307. });
  308. return formatPickerView;
  309. });