MediaWidget.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./UrlWidget', '../../../app/nls/StringResources', './media/MediaResolver', 'jquery', 'underscore'], function (UrlWidget, resources, MediaResolver, $, _) {
  8. var MediaWidget = UrlWidget.extend({
  9. init: function init(options) {
  10. MediaWidget.inherited('init', this, arguments);
  11. this.api = {
  12. getId: this.getId.bind(this),
  13. setMediaUrl: this.setUrl.bind(this)
  14. };
  15. this.model = this.model || {};
  16. this.model.mediaLink = options && options.initialConfigJSON && options.initialConfigJSON.mediaLink;
  17. this.propertyCallbacks = this.dashboardApi.getFeature('PropertyCallbacks');
  18. },
  19. onContainerReady: function onContainerReady() {
  20. MediaWidget.inherited('onContainerReady', this, arguments);
  21. if (this.model) {
  22. this.model.on('change:mediaLink', this._onMediaLinkChange, this);
  23. this.model.on('change:title', this._onPropertyChange, this);
  24. this.addWhiteListAttrs('mediaLink', 'mediaType', 'title');
  25. }
  26. this.eventRouter.trigger('media:ready', this.api);
  27. },
  28. render: function render() {
  29. if (this.propertyCallbacks.validateMediaInput(null, this.model.mediaLink).isValid) {
  30. this.model.mediaLink = this.encodeURL(this.model.mediaLink);
  31. this.media = MediaResolver.loadFromModel(this.model);
  32. this.$el.append(this.getHtmlRender());
  33. if (this.media) {
  34. this.media.markupLoaded(this.$el, this.isAuthoringMode ? 'authoring' : 'consume');
  35. }
  36. } else {
  37. this.$el.append(UrlWidget.getDefaultConsumeMarkup(resources.get('mediaMissingUrl')));
  38. }
  39. },
  40. setUrl: function setUrl(url) {
  41. var errorData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  42. var ret = this.propertyCallbacks.validateMediaInput(null, url);
  43. errorData.message = ret.message;
  44. if (ret.isValid) {
  45. var transactionId = _.uniqueId('_addMedia_');
  46. this.set({
  47. mediaLink: url,
  48. mediaType: ret.media.type
  49. }, {
  50. sender: this,
  51. payloadData: {
  52. undoRedoTransactionId: transactionId
  53. }
  54. });
  55. this.updateModelContent();
  56. return true;
  57. }
  58. return false;
  59. },
  60. onAuthoringMode: function onAuthoringMode() {
  61. MediaWidget.inherited('onAuthoringMode', this, arguments);
  62. //Only prompt media to perform DOM operations when root DOM
  63. //reference is valid
  64. if (this.media && $.contains(document.body, this.el)) {
  65. this.media.showAuthoringMode();
  66. }
  67. },
  68. onConsumeMode: function onConsumeMode() {
  69. MediaWidget.inherited('onConsumeMode', this, arguments);
  70. //Only prompt media to perform DOM operations when root DOM
  71. //reference is valid
  72. if (this.media && $.contains(document.body, this.el)) {
  73. this.media.showConsumeMode();
  74. }
  75. },
  76. onEnterContainer: function onEnterContainer() {
  77. var mediaInput = this.$el.find('.inlineEditInput');
  78. mediaInput.focus();
  79. },
  80. isInlineEditMode: function isInlineEditMode() {
  81. return !this.model.mediaLink;
  82. },
  83. getInlineEditCaption: function getInlineEditCaption() {
  84. return resources.get('mediaPasteLink');
  85. },
  86. getMissingUrlText: function getMissingUrlText() {
  87. return resources.get('mediaMissingUrl');
  88. },
  89. getAriaLabelText: function getAriaLabelText() {
  90. return resources.get('mediaAriaLabel');
  91. },
  92. getLabel: function getLabel() {
  93. return this._getTitle() || resources.get('mediaWidgetTitle');
  94. },
  95. getContentClass: function getContentClass() {
  96. return 'mediaWidgetContent';
  97. },
  98. _getTitle: function _getTitle() {
  99. return this.model && this.model.get ? this.model.get('title') : null;
  100. },
  101. _onMediaLinkChange: function _onMediaLinkChange(options) {
  102. delete this.model.mediaType;
  103. return this._onPropertyChange(options);
  104. },
  105. _onPropertyChange: function _onPropertyChange(options) {
  106. var updatedHtml = null;
  107. if (this.model.mediaLink) {
  108. this.model.mediaLink = this.encodeURL(this.model.mediaLink);
  109. if (this.propertyCallbacks.validateMediaInput(null, this.model.mediaLink).isValid) {
  110. this.media = MediaResolver.loadFromModel(this.model);
  111. updatedHtml = this.getHtmlRender();
  112. var staticContent = this.$el.find('.staticContent');
  113. staticContent.empty();
  114. staticContent.replaceWith(updatedHtml);
  115. this.media.markupLoaded(this.$el, this.isAuthoringMode ? 'authoring' : 'consume');
  116. }
  117. } else {
  118. this.renderInlineEditUi();
  119. updatedHtml = UrlWidget.getDefaultConsumeMarkup(resources.get('mediaMissingUrl'));
  120. }
  121. this.updateMarkup();
  122. if (options.data && options.data.undoRedoTransactionId) {
  123. this.updateModelContent(updatedHtml, options.data.undoRedoTransactionId);
  124. } else {
  125. this.updateModelContent(updatedHtml);
  126. }
  127. },
  128. getHtmlRender: function getHtmlRender() {
  129. return this.media ? this.media.getMarkup(this._getMediaParams()) : '';
  130. },
  131. _getMediaParams: function _getMediaParams() {
  132. var params = {};
  133. var title = this._getTitle();
  134. if (title) {
  135. params.title = title;
  136. }
  137. return params;
  138. }
  139. });
  140. MediaWidget.getDefaultSpec = function () {
  141. var spec = {
  142. model: {
  143. type: 'media',
  144. avatarHtml: UrlWidget.getDefaultConsumeMarkup(),
  145. title: ''
  146. },
  147. layoutProperties: {
  148. style: {
  149. width: '480px',
  150. height: '360px'
  151. }
  152. }
  153. };
  154. return Promise.resolve(spec);
  155. };
  156. return MediaWidget;
  157. });
  158. //# sourceMappingURL=MediaWidget.js.map