define([ 'require' , 'jquery' , 'nbextensions/visualpython/src/common/vpCommon' , 'nbextensions/visualpython/src/common/constant' , 'nbextensions/visualpython/src/common/StringBuilder' , 'nbextensions/visualpython/src/common/vpFuncJS' , 'nbextensions/visualpython/src/common/vpSetting' // TEST: CodeMirror , 'codemirror/lib/codemirror' , 'codemirror/mode/python/python' , 'notebook/js/codemirror-ipython' , 'codemirror/addon/display/placeholder' ], function (requirejs, $, vpCommon, vpConst, sb, vpFuncJS, vpSetting, CodeMirror, cmpython, cmip) { // ìµì ìì± const funcOptProp = { stepCount : 1 , funcName : "User-defined Code" , funcID : "com_udf" } /** * html load ì½ë°±. ê³ ì id ìì±íì¬ ë¶ê³¼íë©° js ê°ì²´ í´ëì¤ ìì±íì¬ ì»¨í ì´ëë¡ ì ë¬ * @param {function} callback í¸ì¶ì(컨í ì´ë) ì ì½ë°±í¨ì */ var optionLoadCallback = function(callback, meta) { // document.getElementsByTagName("head")[0].appendChild(link); // 컨í ì´ëìì ì ë¬ë callback í¨ìê° ì¡´ì¬íë©´ ì¤í. if (typeof(callback) === 'function') { var uuid = vpCommon.getUUID(); // ìµë 10í ì¤ë³µëì§ ìëë¡ ì²´í¬ for (var idx = 0; idx < 10; idx++) { // ì´ë¯¸ ì¬ì©ì¤ì¸ uuid ì¸ ê²½ì° ë¤ì ìì± if ($(vpConst.VP_CONTAINER_ID).find("." + uuid).length > 0) { uuid = vpCommon.getUUID(); } } $(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM))).find(vpCommon.formatString(".{0}", vpConst.API_OPTION_PAGE)).addClass(uuid); // ìµì ê°ì²´ ìì± var optionPackage = new OptionPackage(uuid); optionPackage.metadata = meta; // ìµì ìì± í ë¹. optionPackage.setOptionProp(funcOptProp); // html ì¤ì . optionPackage.initHtml(); callback(optionPackage); // ê³µíµ ê°ì²´ë¥¼ callback ì¸ìë¡ ì ë¬ // after load cell metadata, set codemirror value // optionPackage.vp_userCode.setValue($(vpCommon.wrapSelector('#vp_userCode')).val()); optionPackage.bindCodeMirror(); } } /** * html ë¡ë. * @param {function} callback í¸ì¶ì(컨í ì´ë) ì ì½ë°±í¨ì */ var initOption = function(callback, meta) { vpCommon.loadHtml(vpCommon.wrapSelector(vpCommon.formatString("#{0}", vpConst.OPTION_GREEN_ROOM)), "file_io/udf.html", optionLoadCallback, callback, meta); } /** * 본 ìµì ì²ë¦¬ ìí í´ëì¤ * @param {String} uuid ê³ ì id */ var OptionPackage = function(uuid) { this.uuid = uuid; // Load html ììì uuid. this.package = { input: [ { name: 'vp_userCode' } ] } } /** * vpFuncJS ìì ìì */ OptionPackage.prototype = Object.create(vpFuncJS.VpFuncJS.prototype); /** * ì í¨ì± ê²ì¬ * @returns ì í¨ì± ê²ì¬ ê²°ê³¼. ì í©ì true */ OptionPackage.prototype.optionValidation = function() { return true; // ë¶ëª¨ í´ëì¤ ì í¨ì± ê²ì¬ í¸ì¶. // vpFuncJS.VpFuncJS.prototype.optionValidation.apply(this); } /** * html ë´ë¶ binding ì²ë¦¬ */ OptionPackage.prototype.initHtml = function() { var that = this; this.loadCss(Jupyter.notebook.base_url + vpConst.BASE_PATH + vpConst.STYLE_PATH + "file_io/udf.css"); // bind values after loading html this.package.input && this.package.input.forEach(obj => { if (obj.value != undefined) { var tag = $(this.wrapSelector('#' + obj.name)); tag.val(obj.value); } }); // save udf $(this.wrapSelector('#vp_udfSave')).click(function() { // if title is not empty var key = $(that.wrapSelector('#vp_udfTitle')).val(); if (key == undefined || key === "") { vpCommon.renderAlertModal('Please enter the title'); return; } // save codemirror value to origin textarea that.vp_userCode.save(); var code = that.vp_userCode.getValue(); // save udf var saveUdf = { [key]: code }; vpSetting.saveUserDefinedCode(saveUdf); // FIXME: vp-multilang for success message vpCommon.renderSuccessMessage('Successfully saved!'); // load again that.loadUdfList(); }); // load udf list this.loadUdfList(); // load when refresh clicks $(this.wrapSelector('#vp_udfRefresh')).click(function(event) { event.stopPropagation(); that.loadUdfList(); // show success message vpCommon.renderSuccessMessage('Refreshed!'); }); // new button clicked $(this.wrapSelector('#vp_udfCreate')).click(function() { that.vp_userCode.save(); var code = that.vp_userCode.getValue(); if (code && code.length > 0) { // ask clearing codes that.openMultiBtnModal_new("Save Code", "Would you like to save previous code and clear it?" , ["Just Clear", "Cancel", "Save and Clear"] , [()=> { // clear code $(that.wrapSelector('#vp_udfTitle')).val(''); $(that.wrapSelector('#vp_userCode')).val(''); that.vp_userCode.setValue(''); }, ()=> { }, ()=> { // save and clear code // save var key = $(that.wrapSelector('#vp_udfTitle')).val(); if (key == undefined || key === "") { key = '_temporary'; } // save codemirror value to origin textarea that.vp_userCode.save(); var code = that.vp_userCode.getValue(); // save udf var saveUdf = { [key]: code }; vpSetting.saveUserDefinedCode(saveUdf); // clear code $(that.wrapSelector('#vp_udfTitle')).val(''); $(that.wrapSelector('#vp_userCode')).val(''); that.vp_userCode.setValue(''); // load again that.loadUdfList(); }]); } else { // clear code $(that.wrapSelector('#vp_udfTitle')).val(''); $(that.wrapSelector('#vp_userCode')).val(''); that.vp_userCode.setValue(''); } }); // delete button clicked $(this.wrapSelector('#vp_udfDelete')).click(function() { // remove key from list var key = $(that.wrapSelector('#vp_udfList')).find('.vp-udf-check:checked').val(); if (key && vpSetting.getUserDefinedCode(key)) { // remove key vpSetting.removeUserDefinedCode(key); // FIXME: vp-multilang for success message vpCommon.renderSuccessMessage('Successfully removed!'); } else { vpCommon.renderAlertModal('No key available...'); } // load again that.loadUdfList(); }); } OptionPackage.prototype.loadUdfList = function() { var that = this; // clear table except head $(this.wrapSelector('#vp_udfList tr:not(:first)')).remove(); // load udf list to table 'vp_udfList' vpSetting.loadUserDefinedCodeList(function(udfList) { udfList.forEach(obj => { if (obj.code != null && obj.code != undefined) { var trow = $(`
${obj.code}