1+ /*
2+ * Project Name : Visual Python
3+ * Description : GUI-based Python code generator
4+ * File Name : FitPredict.js
5+ * Author : Black Logic
6+ * Note : Model fit / predict
7+ * License : GNU GPLv3 with Visual Python special exception
8+ * Date : 2022. 04. 20
9+ * Change Date :
10+ */
11+
12+ //============================================================================
13+ // [CLASS] FitPredict
14+ //============================================================================
15+ define ( [
16+ 'text!vp_base/html/m_ml/fitPredict.html!strip' ,
17+ 'vp_base/js/com/com_util' ,
18+ 'vp_base/js/com/com_interface' ,
19+ 'vp_base/js/com/com_String' ,
20+ 'vp_base/js/com/com_generatorV2' ,
21+ 'vp_base/data/m_ml/mlLibrary' ,
22+ 'vp_base/js/com/component/PopupComponent' ,
23+ 'vp_base/js/com/component/VarSelector2' ,
24+ 'vp_base/js/com/component/ModelEditor'
25+ ] , function ( msHtml , com_util , com_interface , com_String , com_generator , ML_LIBRARIES , PopupComponent , VarSelector2 , ModelEditor ) {
26+
27+ /**
28+ * FitPredict
29+ */
30+ class FitPredict extends PopupComponent {
31+ _init ( ) {
32+ super . _init ( ) ;
33+ this . config . sizeLevel = 2 ;
34+ this . config . dataview = false ;
35+
36+ this . state = {
37+ // model selection
38+ model : '' ,
39+ method : '' ,
40+ ...this . state
41+ }
42+
43+ this . modelConfig = ML_LIBRARIES ;
44+ }
45+
46+ _bindEvent ( ) {
47+ super . _bindEvent ( ) ;
48+ /** Implement binding events */
49+ var that = this ;
50+
51+ // change model
52+ $ ( this . wrapSelector ( '#model' ) ) . on ( 'change' , function ( ) {
53+ that . modelEditor . reload ( ) ;
54+ } ) ;
55+ }
56+
57+ templateForBody ( ) {
58+ let page = $ ( msHtml ) ;
59+
60+ let that = this ;
61+
62+ //================================================================
63+ // Model selection
64+ //================================================================
65+ // set model list
66+ let modelOptionTag = new com_String ( ) ;
67+ vpKernel . getModelList ( ) . then ( function ( resultObj ) {
68+ let { result } = resultObj ;
69+ var modelList = JSON . parse ( result ) ;
70+ modelList && modelList . forEach ( model => {
71+ let selectFlag = '' ;
72+ if ( model . varName == that . state . model ) {
73+ selectFlag = 'selected' ;
74+ }
75+ modelOptionTag . appendFormatLine ( '<option value="{0}" data-type="{1}" {2}>{3} ({4})</option>' ,
76+ model . varName , model . varType , selectFlag , model . varName , model . varType ) ;
77+ } ) ;
78+ $ ( page ) . find ( '#model' ) . html ( modelOptionTag . toString ( ) ) ;
79+ $ ( that . wrapSelector ( '#model' ) ) . html ( modelOptionTag . toString ( ) ) ;
80+
81+ if ( ! that . state . model || that . state . model == '' ) {
82+ that . state . model = $ ( that . wrapSelector ( '#model' ) ) . val ( ) ;
83+ }
84+
85+ that . modelEditor . show ( 'action' ) ;
86+ } ) ;
87+
88+ //================================================================
89+ // Load state
90+ //================================================================
91+ Object . keys ( this . state ) . forEach ( key => {
92+ let tag = $ ( page ) . find ( '#' + key ) ;
93+ let tagName = $ ( tag ) . prop ( 'tagName' ) ; // returns with UpperCase
94+ let value = that . state [ key ] ;
95+ if ( value == undefined ) {
96+ return ;
97+ }
98+ switch ( tagName ) {
99+ case 'INPUT' :
100+ let inputType = $ ( tag ) . prop ( 'type' ) ;
101+ if ( inputType == 'text' || inputType == 'number' || inputType == 'hidden' ) {
102+ $ ( tag ) . val ( value ) ;
103+ break ;
104+ }
105+ if ( inputType == 'checkbox' ) {
106+ $ ( tag ) . prop ( 'checked' , value ) ;
107+ break ;
108+ }
109+ break ;
110+ case 'TEXTAREA' :
111+ case 'SELECT' :
112+ default :
113+ $ ( tag ) . val ( value ) ;
114+ break ;
115+ }
116+ } ) ;
117+
118+ return page ;
119+ }
120+
121+ templateForOption ( modelType ) {
122+ let config = this . modelConfig [ modelType ] ;
123+ let state = this . state ;
124+
125+ let optBox = new com_String ( ) ;
126+ // render tag
127+ config . options . forEach ( opt => {
128+ optBox . appendFormatLine ( '<label for="{0}" title="{1}">{2}</label>'
129+ , opt . name , opt . name , com_util . optionToLabel ( opt . name ) ) ;
130+ let content = com_generator . renderContent ( this , opt . component [ 0 ] , opt , state ) ;
131+ optBox . appendLine ( content [ 0 ] . outerHTML ) ;
132+ } ) ;
133+ // render user option
134+ optBox . appendFormatLine ( '<label for="{0}">{1}</label>' , 'userOption' , 'User option' ) ;
135+ optBox . appendFormatLine ( '<input type="text" class="vp-input vp-state" id="{0}" placeholder="{1}" value="{2}"/>' ,
136+ 'userOption' , 'key=value, ...' , this . state . userOption ) ;
137+ return optBox . toString ( ) ;
138+ }
139+
140+ render ( ) {
141+ super . render ( ) ;
142+
143+ // Model Editor
144+ this . modelEditor = new ModelEditor ( this , "model" , "instanceEditor" ) ;
145+ }
146+
147+ generateCode ( ) {
148+ let { model } = this . state ;
149+ let code = new com_String ( ) ;
150+ code . append ( this . modelEditor . getCode ( { '${model}' : model } ) ) ;
151+
152+ return code . toString ( ) ;
153+ }
154+
155+ }
156+
157+ return FitPredict ;
158+ } ) ;
0 commit comments