1+ "use strict" ;
2+
3+ function _typeof ( obj ) { "@babel/helpers - typeof" ; if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
4+
5+ Object . defineProperty ( exports , "__esModule" , {
6+ value : true
7+ } ) ;
8+ exports . default = void 0 ;
9+
10+ var _react = _interopRequireDefault ( require ( "react" ) ) ;
11+
12+ require ( "./retool.css" ) ;
13+
14+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
15+
16+ function ownKeys ( object , enumerableOnly ) { var keys = Object . keys ( object ) ; if ( Object . getOwnPropertySymbols ) { var symbols = Object . getOwnPropertySymbols ( object ) ; if ( enumerableOnly ) symbols = symbols . filter ( function ( sym ) { return Object . getOwnPropertyDescriptor ( object , sym ) . enumerable ; } ) ; keys . push . apply ( keys , symbols ) ; } return keys ; }
17+
18+ function _objectSpread ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] != null ? arguments [ i ] : { } ; if ( i % 2 ) { ownKeys ( Object ( source ) , true ) . forEach ( function ( key ) { _defineProperty ( target , key , source [ key ] ) ; } ) ; } else if ( Object . getOwnPropertyDescriptors ) { Object . defineProperties ( target , Object . getOwnPropertyDescriptors ( source ) ) ; } else { ownKeys ( Object ( source ) ) . forEach ( function ( key ) { Object . defineProperty ( target , key , Object . getOwnPropertyDescriptor ( source , key ) ) ; } ) ; } } return target ; }
19+
20+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
21+
22+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
23+
24+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
25+
26+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function" ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , writable : true , configurable : true } } ) ; if ( superClass ) _setPrototypeOf ( subClass , superClass ) ; }
27+
28+ function _setPrototypeOf ( o , p ) { _setPrototypeOf = Object . setPrototypeOf || function _setPrototypeOf ( o , p ) { o . __proto__ = p ; return o ; } ; return _setPrototypeOf ( o , p ) ; }
29+
30+ function _createSuper ( Derived ) { var hasNativeReflectConstruct = _isNativeReflectConstruct ( ) ; return function _createSuperInternal ( ) { var Super = _getPrototypeOf ( Derived ) , result ; if ( hasNativeReflectConstruct ) { var NewTarget = _getPrototypeOf ( this ) . constructor ; result = Reflect . construct ( Super , arguments , NewTarget ) ; } else { result = Super . apply ( this , arguments ) ; } return _possibleConstructorReturn ( this , result ) ; } ; }
31+
32+ function _possibleConstructorReturn ( self , call ) { if ( call && ( _typeof ( call ) === "object" || typeof call === "function" ) ) { return call ; } return _assertThisInitialized ( self ) ; }
33+
34+ function _assertThisInitialized ( self ) { if ( self === void 0 ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return self ; }
35+
36+ function _isNativeReflectConstruct ( ) { if ( typeof Reflect === "undefined" || ! Reflect . construct ) return false ; if ( Reflect . construct . sham ) return false ; if ( typeof Proxy === "function" ) return true ; try { Boolean . prototype . valueOf . call ( Reflect . construct ( Boolean , [ ] , function ( ) { } ) ) ; return true ; } catch ( e ) { return false ; } }
37+
38+ function _getPrototypeOf ( o ) { _getPrototypeOf = Object . setPrototypeOf ? Object . getPrototypeOf : function _getPrototypeOf ( o ) { return o . __proto__ || Object . getPrototypeOf ( o ) ; } ; return _getPrototypeOf ( o ) ; }
39+
40+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
41+
42+ var Retool = /*#__PURE__*/ function ( _React$Component ) {
43+ _inherits ( Retool , _React$Component ) ;
44+
45+ var _super = _createSuper ( Retool ) ;
46+
47+ function Retool ( props ) {
48+ var _this ;
49+
50+ _classCallCheck ( this , Retool ) ;
51+
52+ _this = _super . call ( this , props ) ;
53+
54+ _defineProperty ( _assertThisInitialized ( _this ) , "startListening" , function ( ) {
55+ window . addEventListener ( 'message' , function ( e ) {
56+ return _this . handle ( e ) ;
57+ } ) ;
58+ } ) ;
59+
60+ _defineProperty ( _assertThisInitialized ( _this ) , "startWatchers" , function ( ) {
61+ var watcherKeys = Object . keys ( _this . state . elementWatchers ) ;
62+
63+ for ( var i = 0 ; i < watcherKeys . length ; i ++ ) {
64+ var key = watcherKeys [ i ] ;
65+ var watcher = _this . state . elementWatchers [ key ] ;
66+ var selector = watcher . selector ;
67+ var node = document . querySelector ( selector ) ;
68+ var value = node === null || node === void 0 ? void 0 : node . textContent ;
69+
70+ if ( value !== watcher . prevValue ) {
71+ watcher . prevValue = value ;
72+ watcher . iframe . contentWindow . postMessage ( {
73+ type : 'PARENT_WINDOW_RESULT' ,
74+ result : value ,
75+ id : watcher . queryId ,
76+ pageName : watcher . pageName
77+ } , '*' ) ;
78+ }
79+ }
80+
81+ setTimeout ( _this . startWatchers , 100 ) ;
82+ } ) ;
83+
84+ _defineProperty ( _assertThisInitialized ( _this ) , "createOrReplaceWatcher" , function ( selector , pageName , queryId ) {
85+ var watcherId = pageName + '-' + queryId ;
86+
87+ var watchers = _objectSpread ( { } , _this . state . elementWatchers ) ;
88+
89+ watchers [ watcherId ] = {
90+ iframe : _this . iframe ,
91+ selector : selector ,
92+ pageName : pageName ,
93+ queryId : queryId ,
94+ prevValue : null
95+ } ;
96+
97+ _this . setState ( {
98+ elementWatchers : watchers
99+ } ) ;
100+ } ) ;
101+
102+ _defineProperty ( _assertThisInitialized ( _this ) , "handle" , function ( event ) {
103+ if ( ! _this . iframe . contentWindow ) return ;
104+ var node ;
105+
106+ if ( event . data . type === 'PARENT_WINDOW_QUERY' ) {
107+ var _node ;
108+
109+ node = document . querySelector ( event . data . selector ) ;
110+
111+ _this . createOrReplaceWatcher ( event . data . selector , event . data . pageName , event . data . id ) ;
112+
113+ _this . iframe . contentWindow . postMessage ( {
114+ type : 'PARENT_WINDOW_RESULT' ,
115+ result : ( _node = node ) === null || _node === void 0 ? void 0 : _node . textContent ,
116+ id : event . data . id ,
117+ pageName : event . data . pageName
118+ } , '*' ) ;
119+ }
120+
121+ if ( event . data . type === 'PARENT_WINDOW_PREVIEW_QUERY' ) {
122+ var _node2 ;
123+
124+ node = document . querySelector ( event . data . selector ) ;
125+
126+ _this . iframe . contentWindow . postMessage ( {
127+ type : 'PARENT_WINDOW_PREVIEW_RESULT' ,
128+ result : ( _node2 = node ) === null || _node2 === void 0 ? void 0 : _node2 . textContent ,
129+ id : event . data . id
130+ } , '*' ) ;
131+ }
132+ } ) ;
133+
134+ _this . state = {
135+ url : props . url ,
136+ elementWatchers : { }
137+ } ;
138+ return _this ;
139+ }
140+
141+ _createClass ( Retool , [ {
142+ key : "componentDidMount" ,
143+ value : function componentDidMount ( ) {
144+ this . startListening ( ) ;
145+ this . startWatchers ( ) ;
146+ }
147+ } , {
148+ key : "render" ,
149+ value : function render ( ) {
150+ var _this2 = this ;
151+
152+ return /*#__PURE__*/ _react . default . createElement ( "iframe" , {
153+ frameBorder : "none" ,
154+ src : this . state . url ,
155+ ref : function ref ( e ) {
156+ _this2 . iframe = e ;
157+ }
158+ } ) ;
159+ }
160+ } ] ) ;
161+
162+ return Retool ;
163+ } ( _react . default . Component ) ;
164+
165+ var _default = Retool ;
166+ exports . default = _default ;
0 commit comments