1- var webviewPluginApp = angular . module ( 'webviewPlugin' , [ ] ) ;
2-
3- webviewPluginApp . controller ( "webviewPluginCtrl" , [ "$scope" , "$log" , "$timeout" , function ( $scope , $log , $timeout ) {
4- var dataChanged = false ;
5- $scope . datastoreInitialized = false ;
6- $scope . urlValid = false ;
7- $scope . urlInValid = false ;
8- $scope . viewType = {
9- NATIVE_IN_APP : 'Native In App' ,
10- IN_APP_POPUP : 'In app popup' ,
11- EXTERNAL_BROWSER : 'External browser'
12- } ;
13- /*
14- * Go pull any previously saved data
15- * */
16- buildfire . datastore . get ( function ( err , result ) {
17- if ( ! err ) {
18- $scope . datastoreInitialized = true ;
19- } else {
20- console . error ( "Error: " , err ) ;
21- return ;
22- }
23- if ( result && result . data && ! angular . equals ( { } , result . data ) && result . id ) {
24- $scope . data = result . data ;
25- $scope . id = result . id ;
26- if ( typeof result . data . content . openInApp != 'undefined' && typeof result . data . content . openInApp != 'object' ) {
27- if ( result . data . content . openInApp )
28- $scope . data . content . view = $scope . viewType . NATIVE_IN_APP ;
29- else
30- $scope . data . content . view = $scope . viewType . IN_APP_POPUP ;
31- }
32- } else {
33- $scope . data = {
34- content : {
35- url : "" ,
36- view : $scope . viewType . NATIVE_IN_APP
37- }
38- } ;
39- }
40-
41- /*
42- * watch for changes in data
43- * */
44- $scope . $watch ( 'data' , function ( newObj , oldObj ) {
45- if ( angular . equals ( newObj , oldObj ) || newObj == undefined ) {
46- dataChanged = false ;
47- } else {
48- dataChanged = true ;
49- }
50- } , true ) ;
51-
52- if ( ! $scope . $$phase && ! $scope . $root . $$phase ) {
53- $scope . $apply ( ) ;
54- }
55- } ) ;
56-
57- $scope . saveData = function ( ) {
58- if ( ! $scope . datastoreInitialized ) {
59- console . error ( "Error with datastore didn't get called" ) ;
60- return ;
61- }
62- if ( ! dataChanged ) {
63- console . warn ( "data didn't changed" ) ;
64- return ;
65- }
66- var data = $scope . data ;
67- // if the form has some invalid data do not save, in our case the user eneter invalid URL
68- if ( $scope . frmMain . $invalid ) {
69- $log . warn ( 'invalid data, details will not be saved' ) ;
70- $scope . urlValid = false ;
71- $scope . urlInValid = true ;
72- $timeout ( function ( ) {
73- $scope . urlInValid = false ;
74- } , 3000 ) ;
75- } else {
76- $scope . urlValid = true ;
77- $scope . urlInValid = false ;
78- $timeout ( function ( ) {
79- $scope . urlValid = false ;
80- } , 3000 ) ;
81- dataChanged = false ;
82- if ( ! / ^ h t t p s ? \: \/ \/ / . test ( data . content . url ) ) {
83- data . content . url = "http://" + data . content . url ;
1+ ( function ( angular , buildfire ) {
2+ var webviewPluginApp = angular . module ( 'webviewPlugin' , [ ] ) ;
3+
4+ webviewPluginApp . controller ( 'webviewPluginCtrl' , [ '$scope' , '$log' , '$timeout' , controller ] ) ;
5+
6+ function controller ( $scope , $log , $timeout ) {
7+ var dataChanged = false ;
8+ $scope . datastoreInitialized = false ;
9+ $scope . urlValid = false ;
10+ $scope . urlInValid = false ;
11+ $scope . viewType = {
12+ NATIVE_IN_APP : 'Native In App' ,
13+ IN_APP_POPUP : 'In app popup' ,
14+ EXTERNAL_BROWSER : 'External browser'
15+ } ;
16+
17+ buildfire . datastore . get ( function ( err , result ) {
18+ if ( err ) return console . error ( 'Error: ' , err ) ;
19+
20+ $scope . datastoreInitialized = true ;
21+
22+ if ( isValidResult ( result ) ) {
23+ $scope . data = result . data ;
24+ $scope . id = result . id ;
25+
26+ var type = typeof result . data . content . openInApp ;
27+
28+ if ( type != 'undefined' && type != 'object' ) {
29+ if ( result . data . content . openInApp ) {
30+ $scope . data . content . view = $scope . viewType . NATIVE_IN_APP ;
31+ } else {
32+ $scope . data . content . view = $scope . viewType . IN_APP_POPUP ;
33+ }
34+ }
35+ } else {
36+ $scope . data = {
37+ content : {
38+ url : '' ,
39+ view : $scope . viewType . NATIVE_IN_APP
40+ }
41+ } ;
42+ }
43+
44+ $scope . $watch ( 'data' , watchFn , true ) ;
45+ function watchFn ( newObj , oldObj ) {
46+ if ( angular . equals ( newObj , oldObj ) || newObj == undefined ) {
47+ dataChanged = false ;
48+ } else {
49+ dataChanged = true ;
50+ }
8451 }
8552
86- if ( data . content . openInApp != undefined )
87- data . content . openInApp = null ;
88- buildfire . datastore . save ( data , function ( err , result ) {
89- if ( err || ! result ) {
90- $log . error ( 'Error saving the widget details: ' , err ) ;
91- }
92- else {
93- $log . info ( 'Widget details saved' ) ;
94- }
95- } ) ;
96- }
97- } ;
53+ if ( ! $scope . $$phase && ! $scope . $root . $$phase ) {
54+ $scope . $apply ( ) ;
55+ }
56+
57+ function isValidResult ( res ) {
58+ return res && res . data && ! angular . equals ( { } , res . data ) && res . id ;
59+ }
60+ } ) ;
9861
99- $scope . validateUrl = function ( ) {
100- $scope . saveData ( ) ;
101- } ;
62+ buildfire . messaging . onReceivedMessage = function ( message ) {
63+ buildfire . messaging . sendMessageToWidget ( message ) ;
64+ } ;
65+
66+ $scope . saveData = function ( ) {
67+ if ( ! $scope . datastoreInitialized ) {
68+ return console . error ( "Error with datastore didn't get called" ) ;
69+ }
70+
71+ if ( ! dataChanged ) {
72+ return console . warn ( "data didn't change" ) ;
73+ }
74+
75+ if ( $scope . frmMain . $invalid ) return setDataInvalid ( ) ;
76+
77+ var data = $scope . data ;
78+ dataChanged = false ;
79+
80+ setDataValid ( ) ;
81+
82+ if ( ! / ^ h t t p s ? \: \/ \/ / . test ( data . content . url ) ) {
83+ data . content . url = 'http://' + data . content . url ;
84+ }
85+
86+ if ( data . content . openInApp != undefined ) {
87+ data . content . openInApp = null ;
88+ }
89+
90+ buildfire . datastore . save ( data , function ( err , result ) {
91+ if ( err || ! result ) {
92+ return $log . error ( 'Error saving the widget details: ' , err ) ;
93+ }
94+
95+ $log . info ( 'Widget details saved' ) ;
96+ } ) ;
97+
98+ function setDataInvalid ( ) {
99+ $log . warn ( 'invalid data, details will not be saved' ) ;
100+ $scope . urlValid = false ;
101+ $scope . urlInValid = true ;
102+ $timeout ( function ( ) {
103+ $scope . urlInValid = false ;
104+ } , 3000 ) ;
105+ }
106+
107+ function setDataValid ( ) {
108+ $scope . urlValid = true ;
109+ $scope . urlInValid = false ;
110+ $timeout ( function ( ) {
111+ $scope . urlValid = false ;
112+ } , 3000 ) ;
113+ }
114+ } ;
115+
116+ $scope . validateUrl = function ( ) {
117+ $scope . saveData ( ) ;
118+ } ;
119+
120+ $scope . changeViewType = function ( ) {
121+ dataChanged = true ;
122+
123+ if ( $scope . frmMain . $invalid ) return ;
102124
103- $scope . changeViewType = function ( ) {
104- dataChanged = true ;
105- if ( ! $scope . frmMain . $invalid ) {
106125 var data = $scope . data ;
107- console . log ( "***********save" , data . content . openInApp ) ;
126+
108127 if ( data . content . openInApp != undefined ) {
109128 data . content . openInApp = null ;
110129 }
111- buildfire . datastore . save ( data , function ( err , result ) {
130+ buildfire . datastore . save ( data , function ( err , result ) {
112131 if ( err || ! result ) {
113132 $log . error ( 'Error saving the widget details: ' , err ) ;
114- }
115- else {
133+ } else {
116134 $log . info ( 'Widget details saved' ) ;
117135 }
118136 } ) ;
119- }
120- } ;
121-
122- $scope . openMethodChanged = function ( ) {
123- dataChanged = true ;
124- buildfire . datastore . save ( $scope . data , function ( err , result ) {
125- if ( err || ! result ) {
126- $log . error ( 'Error saving the widget details: ' , err ) ;
127- }
128- else {
129- $log . info ( 'Widget details saved' ) ;
130- }
131- } ) ;
132- } ;
137+ } ;
138+
139+ $scope . openMethodChanged = function ( ) {
140+ dataChanged = true ;
141+ buildfire . datastore . save ( $scope . data , function ( err , result ) {
142+ if ( err || ! result ) {
143+ $log . error ( 'Error saving the widget details: ' , err ) ;
144+ } else {
145+ $log . info ( 'Widget details saved' ) ;
146+ }
147+ } ) ;
148+ } ;
133149
134- $scope . isUrlValid = function ( url ) {
135- return / [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & a m p ; / / = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( \/ [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? ! ? \/ ? \w \/ ? & a m p ; / / = ] * ) ? / . test ( url ) ;
136- } ;
137- } ] ) ;
150+ $scope . isUrlValid = function ( url ) {
151+ return / [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & a m p ; / / = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( \/ [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? ! ? \/ ? \w \/ ? & a m p ; / / = ] * ) ? / . test ( url ) ;
152+ } ;
153+ }
154+ } ) ( window . angular , window . buildfire ) ;
0 commit comments