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}