MobileVizView.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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
  9. * restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define([
  12. 'underscore',
  13. 'bi/glass/app/ContentView',
  14. 'bi/content_apps/utils/GlassContextHelper',
  15. 'jquery',
  16. 'q',
  17. 'dashboard-analytics/preview/LiveWidgetPreview'
  18. ], function (_, View, GlassContextHelper, $, Q, lWidget) {
  19. var MESSAGE_VERSION = '1.0.0';
  20. var mobileView = View.extend({
  21. lw: null,
  22. widgetSpec: {},
  23. init: function (options) {
  24. this.widgetSpec = {};
  25. mobileView.inherited('init', this, arguments);
  26. this.data = {};
  27. $.extend(this, options);
  28. },
  29. postMessage : function({ name, data }){
  30. window.ReactNativeWebView.postMessage(this.createMessage({ name, data }));
  31. },
  32. createMessage: function({ name, data }){
  33. const message = JSON.stringify({
  34. name,
  35. data,
  36. version: MESSAGE_VERSION
  37. });
  38. return message;
  39. },
  40. onResize: function(){
  41. this.lw.resize();
  42. setTimeout(()=>{
  43. if(this.isVisHorizontal){
  44. this.lw.widget._currVis.viprWidget.setLegendPosition('right');
  45. }else{
  46. this.lw.widget._currVis.viprWidget.setLegendPosition('top');
  47. }
  48. this.lw.widget._currVis.viprWidget.render();
  49. // will working hard to remove the timeout
  50. },300);
  51. },
  52. generateThumbnail: function(widget){
  53. widget.generateThumbnail({
  54. size:{
  55. width:350,
  56. height:350
  57. }
  58. }).then(data=>{
  59. this.postMessage({
  60. name:'thumbnailSvg',
  61. data:data.thumbnail
  62. });
  63. });
  64. },
  65. renderVis: function(widgetSpec){
  66. if(widgetSpec){
  67. this.widgetSpec = widgetSpec;
  68. this.widgetSpec.properties = [
  69. {
  70. id: 'widget.legend.font',
  71. value: '14px'
  72. }
  73. ];
  74. if(this.isVisHorizontal){
  75. this.widgetSpec.properties.push({
  76. id: 'widget.legend.position',
  77. value: 'right'
  78. });
  79. }
  80. $('.ca-mobile-live-widget').detach();
  81. $('head').append(`<style>
  82. *{
  83. box-sizing:border-box;
  84. }
  85. body,html{
  86. width:100%;
  87. height:100%
  88. margin:0;
  89. padding:0;
  90. }
  91. .ca-mobile-live-widget{
  92. width:100vw;
  93. height:100vh;
  94. z-index:999;
  95. display: flex;
  96. flex-direction: column;
  97. }
  98. .ca-mobile-live-widget__title{
  99. padding-top:16px;
  100. padding-bottom:16px;
  101. padding-left:16px;
  102. font-size: 18px;
  103. line-height: 14px;
  104. font-weight: bold;
  105. color:#343334;
  106. }
  107. .ca-mobile-live-widget__content{
  108. flex: auto 1;
  109. }
  110. </style>`);
  111. $('body').append(`
  112. <div class="ca-mobile-live-widget" >
  113. <div class="ca-mobile-live-widget__title">
  114. ${this.title}
  115. </div>
  116. <div class="ca-mobile-live-widget__content">
  117. </div>
  118. </div>`);
  119. this.lw = new lWidget({
  120. glassContext: this.glassContext,
  121. enableClick: true,
  122. enablePanAndZoom: true,
  123. enableHover: true,
  124. node: $('.ca-mobile-live-widget')[0],
  125. spec: widgetSpec
  126. });
  127. this.lw.render()
  128. .then(()=>{
  129. if(this.lw.widget.isRendered){
  130. this.generateThumbnail(this.lw.widget);
  131. window.addEventListener('resize',this.onResize.bind(this));
  132. }
  133. })
  134. .catch(err=>{
  135. console.warn('failed to render visualizaion:', err);
  136. });
  137. }
  138. },
  139. destroy: function(){
  140. this.lw.destroy();
  141. window.removeEventListener('resize');
  142. },
  143. onMessage: function(event){
  144. const message = JSON.parse(event.data);
  145. this.readMessage(message);
  146. },
  147. readMessage: function(message){
  148. // for backward compatible cuz the change from ca-mobile-client not merged yet.
  149. if(message.version === 'test'){
  150. if(message.name === 'title'){
  151. this.title = message.data;
  152. }else if(message.name === 'orientation'){
  153. if( message.data === 'landscape'){
  154. this.isVisHorizontal = true;
  155. }else if( message.data === 'portrait'){
  156. this.isVisHorizontal = false;
  157. }
  158. }
  159. }else{
  160. this.source = message;
  161. this.renderVis(message);
  162. }
  163. },
  164. open: function(/* context, options */){
  165. this.glassContext = __glassAppController.glassContext; // eslint-disable-line no-undef
  166. this.render();
  167. return Promise.resolve();
  168. },
  169. render: function () {
  170. try {
  171. window.addEventListener('message', this.onMessage.bind(this));
  172. this.postMessage({
  173. name:'glassLoaded'
  174. });
  175. } catch (error) {
  176. console.log(error);
  177. }
  178. return Promise.resolve(this);
  179. }
  180. });
  181. return mobileView;
  182. });