define([
'jquery'
, 'nbextensions/visualpython/src/common/vpCommon'
, 'nbextensions/visualpython/src/common/constant'
, 'nbextensions/visualpython/src/common/metaDataHandler'
, 'nbextensions/visualpython/src/common/StringBuilder'
, 'nbextensions/visualpython/src/common/component/vpAccordionBox'
, 'nbextensions/visualpython/src/common/component/vpLineNumberTextArea'
, 'nbextensions/visualpython/src/common/component/vpTableLayoutVerticalSimple'
, 'nbextensions/visualpython/src/common/component/vpTableLayoutHorizontalSimple'
, 'nbextensions/visualpython/src/common/component/vpMultiButtonModal'
, 'nbextensions/visualpython/src/common/component/vpMultiButtonModal_new'
], function($, vpCommon, vpConst, md, sb, vpAccordionBox, vpLineNumberTextArea, vpTableLayoutVerticalSimple, vpTableLayoutHorizontalSimple, vpMultiButtonModal
, vpMultiButtonModal_new) {
"use strict";
/**
* @class VpFuncJS
* @constructor
* @param {funcOptProp} props 기본 ìì±
* @param {String} uuid ê³ ì id
*/
var VpFuncJS = function(props, uuid) {
this.setOptionProp(props);
this.uuid = uuid;
this.generatedCode = "";
};
/**
@param {funcOptProp} props 기본 ìì±
*/
VpFuncJS.prototype.setOptionProp = function(props) {
this.funcName = props.funcName;
this.funcID = props.funcID;
}
/**
* Task Index ì
í
* @param {number} idx task sequential index
*/
VpFuncJS.prototype.setTaskIndex = function(idx) {
this.taskIdx = idx;
}
/**
* Task Index íì¸
* @returns {number} task sequential index
*/
VpFuncJS.prototype.getTaskIndex = function() {
return this.taskIdx;
}
/**
* ì í¨ì± ê²ì¬
* @param {*} args
* @returns {boolean} ì í¨ì± ì²´í¬
*/
VpFuncJS.prototype.optionValidation = function(args) {
console.log("[vpFuncJS.optionValidation] Not developed yet. Need override on child.");
return false;
}
/**
* Python ì½ë ì¤í í ë°í ê° ì ë¬í´ ì½ë°±í¨ì í¸ì¶
* @param {String} command ì¤íí ì½ë
* @param {function} callback ì¤í ìë£ í í¸ì¶ë callback
* @param {boolean} isSilent 커ëì ì¤íìí ì í¸ ì ë¬ ì¬ë¶ 기본 false
* @param {boolean} isStoreHistory 커ëì íì¤í 리 ì±ì°ëë¡ ì í¸ ê¸°ë³¸ !isSilent
* @param {boolean} isStopOnError ì¤ííì ìì¸ ë°ìì ì¤ì§ ì¬ë¶ 기본 true
*/
VpFuncJS.prototype.kernelExecute = function(command, callback, isSilent = false, isStoreHistory = !isSilent, isStopOnError = true) {
Jupyter.notebook.kernel.execute(
command,
{
iopub: {
output: function(msg) {
var result = String(msg.content["text"]);
if (!result || result == 'undefined') {
if (msg.content.data) {
result = String(msg.content.data["text/plain"]);
}
}
callback(result);
}
}
},
{ silent: isSilent, store_history: isStoreHistory, stop_on_error: isStopOnError }
);
}
/**
* ì
ì ìì¤ ì¶ê°íê³ ì¤í.
* @param {String} command ì¤íí ì½ë
* @param {boolean} exec ì¤íì¬ë¶
* @param {String} type ì
íì
*/
VpFuncJS.prototype.cellExecute = function(command, exec, type = "code") {
// TODO: Validate ê±°ì¹ ê²
this.generatedCode = command;
var targetCell = Jupyter.notebook.insert_cell_below(type);
// ì½ëíì
ì¸ ê²½ì° ìê·¸ëì³ ì¶ê°.
if (type == "code") {
// command = vpCommon.formatString("{0}\n{1}", vpConst.PREFIX_CODE_SIGNATURE, command);
command = vpCommon.formatString("{0}", command);
}
targetCell.set_text(command);
Jupyter.notebook.select_next();
// this.metaSave(); ê° í¨ììì í¸ì¶íëë¡ ë³ê²½.
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
/**
* ì¶ê° + ì´ì§ì© 주ì
* 2020 10 22 íê¸('ì½ëê° ì¤íëììµëë¤') -> ìì´ë¡ ë³ê²½('Your code has been executed)
*/
vpCommon.renderSuccessMessage("Your code has been executed");
}
/** ì¶ê° + ê¹ë¯¼ì£¼ 주ì
* 2020 11 24 주í¼í° ì
ì¤í(run)/ì¶ê°(add) í ì íë ì
ë¡ ì´ë
*/
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
}
/**
* ì íì ë²ì uuid ìì¼ë¡ ê°ì¸ê¸°
* @param {String} selector ì íí ëì ì íì. ë³µì ë§¤ê° ì uuid ìëë¡ ììëë¡ ì íë¨
* @returns ê°ì¸ì§ ì íì
*/
VpFuncJS.prototype.wrapSelector = function(selector) {
var args = Array.prototype.slice.call(arguments);
args.unshift("." + this.uuid);
return vpCommon.wrapSelector.apply(this, args);
}
/**
* append css on option
* @param {String} url style sheet url
*/
VpFuncJS.prototype.loadCss = function(url) {
try {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = requirejs.toUrl(url);
document.getElementsByClassName(this.uuid)[0].appendChild(link);
} catch (err) {
console.log("[vp] Error occurred during load style sheet. Skip this time.");
console.warn(err.message);
}
}
/**
* 미리 ìì±ë ì½ë ì¤í
*/
VpFuncJS.prototype.executeGenerated = function() {
if (this.generatedCode !== "")
this.cellExecute(this.generatedCode, true);
}
/** ì¶ê° + ì´ì§ì© 주ì
* íì¼ ë¤ë¹ê²ì´ì
ì ì´ ì½ë를 ì¬ì©
* @param {String} command ì¤íí ì½ë
* @param {function} callback ì¤í ìë£ í í¸ì¶ë callback
* @param {boolean} isSilent 커ëì ì¤íìí ì í¸ ì ë¬ ì¬ë¶ 기본 false
* @param {boolean} isStoreHistory 커ëì íì¤í 리 ì±ì°ëë¡ ì í¸ ê¸°ë³¸ !isSilent
* @param {boolean} isStopOnError ì¤ííì ìì¸ ë°ìì ì¤ì§ ì¬ë¶ 기본 true
*/
VpFuncJS.prototype.kernelExecuteV2 = function(command, callback, isSilent = false, isStoreHistory = !isSilent, isStopOnError = true) {
Jupyter.notebook.kernel.execute(
command,
{
iopub: {
output: function(msg) {
var result = msg.content.data['text/plain']; // <- ì´ ë¶ë¶ì ê°ì í kernelExecute ë²ì 2 ì½ë
callback(result);
}
}
},
{ silent: isSilent, store_history: isStoreHistory, stop_on_error: isStopOnError }
);
}
/**
* ë©íë°ì´í° í¸ë¤ë¬ ì´ê¸°í
*/
VpFuncJS.prototype.initMetaHandler = function() {
if (this.mdHandler === undefined)
this.mdHandler = new md.MdHandler(this.funcID);
return this.mdHandler;
}
/**
* ë©íë°ì´í° ìì±
*/
VpFuncJS.prototype.metaGenerate = function() {
if (this.package === undefined) return;
var inputIdList = this.package.input.map(x => x.name);
// inputIdList = inputIdList.concat(this.package.output.map(x => x.name));
// inputIdList = inputIdList.concat(this.package.variable.map(x => x.name));
// FIXME: minju : not existing object mapping error fixed
if (this.package.output) inputIdList = inputIdList.concat(this.package.output.map(x => x.name));
if (this.package.variable) inputIdList = inputIdList.concat(this.package.variable.map(x => x.name));
// generate metadata
this.initMetaHandler();
this.metadata = this.mdHandler.generateMetadata(this, inputIdList);
}
/**
* ë©íë°ì´í° ì¸ì´ë¸
*/
VpFuncJS.prototype.metaSave = function() {
// generate metadata
this.metaGenerate();
// save metadata
if (this.package === undefined) return;
// 20210104 minju: ì
ì Metadata ì ì¥íì§ ì기
// this.mdHandler.saveMetadata();
}
/**
* ë©íë°ì´í° ë¡ë
* @param {funcJS} option
* @param {JSON} meta
*/
VpFuncJS.prototype.loadMeta = function(funcJS, meta) {
this.initMetaHandler();
this.mdHandler.metadata = meta;
this.mdHandler.loadDirectMdAsTag(funcJS, meta);
// ë¡ë í ìì
ì´ ë°ì¸ë© ëì´ìì¼ë©´ ì²ë¦¬
if (this.loadMetaExpend !== undefined && typeof this.loadMetaExpend == "function") {
this.loadMetaExpend(funcJS, meta);
}
}
/**
* Get Value of Metadata by option id
* @param {string} id
*/
VpFuncJS.prototype.getMetadata = function(id) {
if (this.metadata == undefined)
return "";
if (this.metadata.options) {
var len = this.metadata.options.length;
for (var i = 0; i < len; i++) {
var obj = this.metadata.options[i];
if (obj.id == id)
return obj.value;
}
}
return "";
}
/**
* íì´ì§ ë´ì© ì½ì
.
* @param {String} content íì´ì§ ë´ì©
* @param {number} pageIndex íì´ì§ ì¸ë±ì¤
*/
VpFuncJS.prototype.setPage = function(content, pageIndex = 0) {
$(vpCommon.wrapSelector(vpCommon.formatString(".{0}.{1}:eq({2})", this.uuid, vpConst.API_OPTION_PAGE, pageIndex))).append(content);
}
/**
* prefix, postfix ì
ë ¥ 컨í¸ë¡¤ ìì±
* @param {String} caption ìì½ëì¸ ë°ì¤ 캡ì
* @param {String} areaID textarea id
* @param {String} content textarea content
* @returns {String} tag string
*/
VpFuncJS.prototype.createManualCode = function(caption, areaID, content) {
var accBoxManualCode = new vpAccordionBox.vpAccordionBox(caption);
var lineNumberTextArea = new vpLineNumberTextArea.vpLineNumberTextArea(areaID, content);
accBoxManualCode.addClass(vpConst.ACCORDION_GRAY_COLOR);
accBoxManualCode.appendContent(lineNumberTextArea.toTagString());
return accBoxManualCode.toTagString();
}
/**
* prefix ì
ë ¥ 컨í¸ë¡¤ ìì±
* @param {String} content textarea content
* @returns {String} tag string
*/
VpFuncJS.prototype.createPrefixCode = function(content = "") {
return this.createManualCode(vpConst.API_OPTION_PREFIX_CAPTION, vpConst.API_OPTION_PREFIX_CODE_ID, content);
}
/**
* prefix 컨í¸ë¡¤ ê° ì¤ì
* @param {String} content textarea content
*/
VpFuncJS.prototype.setPrefixCode = function(content) {
$(this.wrapSelector(vpCommon.formatString("#{0}", vpConst.API_OPTION_PREFIX_CODE_ID))).val(content);
}
/**
* prefix 컨í¸ë¡¤ ê° ì¡°í
* @returns {String} textarea content
*/
VpFuncJS.prototype.getPrefixCode = function() {
return $(this.wrapSelector(vpCommon.formatString("#{0}", vpConst.API_OPTION_PREFIX_CODE_ID))).val();
}
/**
* postfix ì
ë ¥ 컨í¸ë¡¤ ìì±
* @param {String} content textarea content
* @returns {String} tag string
*/
VpFuncJS.prototype.createPostfixCode = function(content = "") {
return this.createManualCode(vpConst.API_OPTION_POSTFIX_CAPTION, vpConst.API_OPTION_POSTFIX_CODE_ID, content);
}
/**
* postfix 컨í¸ë¡¤ ê° ì¤ì
* @param {String} content textarea content
*/
VpFuncJS.prototype.setPostfixCode = function(content) {
$(this.wrapSelector(vpCommon.formatString("#{0}", vpConst.API_OPTION_POSTFIX_CODE_ID))).val(content);
}
/**
* postfix 컨í¸ë¡¤ ê° ì¡°í
* @returns {String} textarea content
*/
VpFuncJS.prototype.getPostfixCode = function() {
return $(this.wrapSelector(vpCommon.formatString("#{0}", vpConst.API_OPTION_POSTFIX_CODE_ID))).val();
}
/**
* ìµì
컨í
ì´ë ìì±
* @param {String} caption ìì½ëì¸ ë°ì¤ 캡ì
* @returns {vpAccordionBox} ìì½ëì¸ ë°ì¤
*/
VpFuncJS.prototype.createOptionContainer = function(caption) {
var accBox = new vpAccordionBox.vpAccordionBox(caption);
return accBox;
}
/**
* ì¸ë¡í ê°ë¨ í
ì´ë¸ ë ì´ìì ìì±
* @param {String} thWidth í
ì´ë¸ í¤ë(ì¢ì¸¡ ì
) ëì´
*/
VpFuncJS.prototype.createVERSimpleLayout = function(thWidth) {
var tblLayout = new vpTableLayoutVerticalSimple.vpTableLayoutVerticalSimple();
tblLayout.setTHWidth(thWidth);
return tblLayout;
}
/**
* ê°ë¡í ê°ë¨ í
ì´ë¸ ë ì´ìì ìì±
* @param {Array} thWidth í
ì´ë¸ ì
ë³ ëì´
*/
VpFuncJS.prototype.createHORIZSimpleLayout = function(thWidth) {
var tblLayout = new vpTableLayoutHorizontalSimple.vpTableLayoutHorizontalSimple();
// tblLayout.setTHWidth(thWidth);
return tblLayout;
}
/**
* ìµì
ë³ ì´ë²¤í¸ ë°ì¸ë©. (컨í
ì´ë callback ìì í¸ì¶ í¨)
*/
VpFuncJS.prototype.bindOptionEvent = function() {
// var that = this;
// $(document).on(vpCommon.formatString("click.{0}", that.uuid), function(evt) {
// console.log("Test log from vp func. " + that.uuid);
// });
// $(document).on(vpCommon.formatString("dblclick.{0}", that.uuid), function(evt) {
// console.log("Test log from vp func dblclick. " + that.uuid);
// });
}
/**
* ìµì
ë³ ì´ë²¤í¸ ì¸ë°ì¸ë©. (컨í
ì´ëìì ë¡ëìµì
í기ë ë í¸ì¶ í¨).
*/
VpFuncJS.prototype.unbindOptionEvent = function() {
$(document).unbind(vpCommon.formatString(".{0}", this.uuid));
}
/**
* ëª¨ë¬ ì¤í
* @param {String} message ëª¨ë¬ ë©ìì§
* @param {Array} buttons ë²í¼ 캡ì
* @param {function} callback ì í ì½ë°± í¨ì
*/
VpFuncJS.prototype.openMultiBtnModal = function(message = "", buttons = new Array(), callback) {
var mbmModal = new vpMultiButtonModal.vpMultiButtonModal();
mbmModal.setMessage(message);
mbmModal.setButtons(buttons);
mbmModal.openModal(callback);
}
/**
* ëª¨ë¬ ì¤í
* @param {String} message ëª¨ë¬ ë©ìì§
* @param {Array