1+ define ( [
2+ 'require'
3+ , 'jquery'
4+ , 'nbextensions/visualpython/src/common/constant'
5+ , 'nbextensions/visualpython/src/common/StringBuilder'
6+ , 'nbextensions/visualpython/src/common/vpCommon'
7+ ] , function ( requirejs , $ , vpConst , sb , vpCommon ) {
8+
9+ // TEST: testing popuppage as an interface of every popup modules
10+
11+ const VP_PP = 'vp-pp' ;
12+ const VP_PP_CONTAINER = 'vp-pp-container' ;
13+ const VP_PP_TITLE = 'vp-pp-title' ;
14+ const VP_PP_CLOSE = 'vp-pp-close' ;
15+ const VP_PP_BODY = 'vp-pp-body' ;
16+ const VP_PP_BUTTON_BOX = 'vp-pp-btn-box' ;
17+ const VP_PP_BUTTON_CANCEL = 'vp-pp-btn-cancel' ;
18+ const VP_PP_BUTTON_APPLY = 'vp-pp-btn-apply' ;
19+
20+ /**
21+ * @class PopupPage
22+ * @param {object } pageThis
23+ * @param {string } targetId
24+ * @constructor
25+ */
26+ var PopupPage = function ( pageThis , targetId ) {
27+ this . pageThis = pageThis ;
28+ this . targetId = targetId ;
29+ this . uuid = vpCommon . getUUID ( ) ;
30+
31+ this . config = {
32+ title : '' ,
33+ width : '95%' ,
34+ height : '95%' ,
35+ pageDom : $ ( '<div>Empty</div>' )
36+ } ;
37+ }
38+
39+ PopupPage . prototype . wrapSelector = function ( selector = '' ) {
40+ return vpCommon . formatString ( '.{0} {1}' , this . uuid , selector ) ;
41+ }
42+
43+ PopupPage . prototype . init = function ( ) {
44+ vpCommon . loadCss ( Jupyter . notebook . base_url + vpConst . BASE_PATH + vpConst . STYLE_PATH + "common/popupPage.css" ) ;
45+ this . renderPopup ( ) ;
46+ this . bindEvent ( ) ;
47+ }
48+
49+ PopupPage . prototype . renderPopup = function ( ) {
50+ var { title, width, height, pageDom } = this . config ;
51+
52+ var page = new sb . StringBuilder ( ) ;
53+ page . appendFormatLine ( '<div class="{0} {1}">' , VP_PP , this . uuid ) ;
54+ page . appendFormatLine ( '<div class="{0}" style="width: {1}; height: {2};">' , VP_PP_CONTAINER , width , height ) ;
55+
56+ // title
57+ page . appendFormat ( '<div class="{0}">{1}</div>'
58+ , VP_PP_TITLE
59+ , title ) ;
60+
61+ // close button
62+ page . appendFormatLine ( '<div class="{0}"><i class="{1}"></i></div>'
63+ , VP_PP_CLOSE , 'fa fa-close' ) ;
64+
65+ // body start
66+ page . appendFormatLine ( '<div class="{0}">' , VP_PP_BODY ) ;
67+ page . appendLine ( '</div>' ) ; // body end
68+
69+ // button box
70+ page . appendFormatLine ( '<div class="{0}">' , VP_PP_BUTTON_BOX ) ;
71+ page . appendFormatLine ( '<button type="button" class="{0}">{1}</button>'
72+ , VP_PP_BUTTON_CANCEL , 'Cancel' ) ;
73+ page . appendFormatLine ( '<button type="button" class="{0}">{1}</button>'
74+ , VP_PP_BUTTON_APPLY , 'Apply' ) ;
75+ page . appendLine ( '</div>' ) ;
76+
77+ page . appendLine ( '</div>' ) ; // container end
78+ page . appendLine ( '</div>' ) ; // VP_PP end
79+
80+ $ ( '#vp-wrapper' ) . append ( page . toString ( ) ) ;
81+
82+ $ ( pageDom ) . appendTo ( $ ( this . wrapSelector ( '.' + VP_PP_BODY ) ) ) ;
83+ $ ( this . wrapSelector ( ) ) . hide ( ) ;
84+
85+ return page . toString ( ) ;
86+ }
87+
88+ PopupPage . prototype . open = function ( config ) {
89+ this . config = {
90+ ...this . config ,
91+ ...config
92+ } ;
93+
94+ this . init ( ) ;
95+ $ ( this . wrapSelector ( ) ) . show ( ) ;
96+ }
97+
98+ PopupPage . prototype . close = function ( ) {
99+ this . unbindEvent ( ) ;
100+ $ ( this . wrapSelector ( ) ) . remove ( ) ;
101+ }
102+
103+ PopupPage . prototype . bindEvent = function ( ) {
104+ var that = this ;
105+
106+ // close popup
107+ $ ( document ) . on ( 'click' , this . wrapSelector ( '.' + VP_PP_CLOSE ) , function ( event ) {
108+ that . close ( ) ;
109+ } ) ;
110+
111+ // click cancel
112+ $ ( document ) . on ( 'click' , this . wrapSelector ( '.' + VP_PP_BUTTON_CANCEL ) , function ( ) {
113+ that . close ( ) ;
114+ } ) ;
115+
116+ // click apply
117+ $ ( document ) . on ( 'click' , this . wrapSelector ( '.' + VP_PP_BUTTON_APPLY ) , function ( ) {
118+ if ( that . pageThis ) {
119+ var code = that . pageThis . generateCode ( false , false ) ;
120+ $ ( vpCommon . wrapSelector ( '#' + that . targetId ) ) . val ( code ) ;
121+ $ ( vpCommon . wrapSelector ( '#' + that . targetId ) ) . trigger ( {
122+ type : 'popup_apply' ,
123+ title : that . config . title ,
124+ code : code
125+ } ) ;
126+ }
127+ that . close ( ) ;
128+ } ) ;
129+ }
130+
131+ PopupPage . prototype . unbindEvent = function ( ) {
132+ $ ( document ) . unbind ( vpCommon . formatString ( ".{0} .{1}" , this . uuid , VP_PP_BODY ) ) ;
133+ }
134+
135+
136+ return PopupPage ;
137+ } ) ;
0 commit comments