2018-11-26 19:22:53 +00:00
var buttonsarray = [ ]
2020-04-17 21:07:39 +00:00
var macroCodeType = "gcode"
2018-11-26 19:22:53 +00:00
function populateMacroButtons ( ) {
$ ( "#macros" ) . empty ( ) ;
for ( i = 0 ; i < buttonsarray . length ; i ++ ) {
2019-04-12 15:22:42 +00:00
// Handle old created buttons that didnt have a tooltip
if ( ! buttonsarray [ i ] . tooltip ) {
buttonsarray [ i ] . tooltip = ""
} ;
2020-04-15 07:02:35 +00:00
if ( buttonsarray [ i ] . macrokeyboardshortcut && buttonsarray [ i ] . macrokeyboardshortcut . length ) {
2020-04-14 17:57:56 +00:00
var keyboardAssignment = buttonsarray [ i ] . macrokeyboardshortcut
} else {
var keyboardAssignment = "none"
}
2020-04-17 21:07:39 +00:00
if ( buttonsarray [ i ] . codetype && buttonsarray [ i ] . codetype . length ) {
var codetype = buttonsarray [ i ] . codetype
} else {
buttonsarray [ i ] . codetype = "gcode"
var codetype = "gcode"
}
if ( codetype == "gcode" ) {
var button = `
< button class = "macrobtn m-1 command-button drop-shadow outline ` + buttonsarray[i].class + `" title = "` + buttonsarray[i].tooltip + `" onclick = "sendGcode('` + buttonsarray[i].gcode.replace(/(\r\n|\n|\r)/gm, " \ \ n ") + `');" >
< span class = "` + buttonsarray[i].icon + ` icon" > < / s p a n >
< span class = "caption mt-2" >
` + buttonsarray[i].title + `
< small > < i class = "far fa-fw fa-keyboard" > < / i > : [ ` + k e y b o a r d A s s i g n m e n t + ` ] < / s m a l l >
< / s p a n >
< span title = "Edit Macro" onclick = "edit(` + i + `, event);" id = "edit` + i + `" class = "fas fa-cogs macroedit" > < / s p a n >
< span title = "Code Type: ` + codetype + `" class = "macrotype" > ` + codetype + ` < / s p a n >
< / b u t t o n >
2020-04-14 17:57:56 +00:00
2020-04-17 21:07:39 +00:00
`
} else if ( codetype == "javascript" ) {
// Future JS Macros here
var button = `
< button class = "macrobtn m-1 command-button drop-shadow outline ` + buttonsarray[i].class + `" title = "` + buttonsarray[i].tooltip + `" onclick = "runJsMacro('` + i + `');" >
< span class = "` + buttonsarray[i].icon + ` icon" > < / s p a n >
< span class = "caption mt-2" >
` + buttonsarray[i].title + `
< small > < i class = "far fa-fw fa-keyboard" > < / i > : [ ` + k e y b o a r d A s s i g n m e n t + ` ] < / s m a l l >
< / s p a n >
< span title = "Edit Macro" onclick = "edit(` + i + `, event);" id = "edit` + i + `" class = "fas fa-cogs macroedit" > < / s p a n >
< span title = "Code Type: ` + codetype + `" class = "macrotype" > ` + codetype + ` < / s p a n >
< / b u t t o n >
`
}
2018-11-26 19:22:53 +00:00
$ ( "#macros" ) . append ( button ) ;
}
// append add button
var button = `
2020-04-15 16:24:34 +00:00
< button class = "m-1 command-button drop-shadow outline rounded" onclick = "edit(` + (buttonsarray.length + 1) + `, event)" >
2020-04-14 17:57:56 +00:00
< span class = "fas fa-plus icon" > < / s p a n >
< span class = "caption mt-2" >
Add
< small > Macro < / s m a l l >
< / s p a n >
< / b u t t o n >
`
2018-11-26 19:22:53 +00:00
$ ( "#macros" ) . append ( button ) ;
localStorage . setItem ( 'macroButtons' , JSON . stringify ( buttonsarray ) ) ;
}
function edit ( i , evt ) {
evt . preventDefault ( ) ;
evt . stopPropagation ( ) ;
2020-04-20 20:58:03 +00:00
// console.log("Editing " + i)
2018-11-26 19:22:53 +00:00
if ( buttonsarray [ i ] ) {
var icon = buttonsarray [ i ] . icon ;
var title = buttonsarray [ i ] . title ;
2020-04-17 21:07:39 +00:00
var codetype = buttonsarray [ i ] . codetype
2018-11-26 19:22:53 +00:00
var gcode = buttonsarray [ i ] . gcode ;
2020-04-17 21:07:39 +00:00
var javascript = buttonsarray [ i ] . javascript
2018-11-26 19:22:53 +00:00
var cls = buttonsarray [ i ] . class ;
2019-04-12 15:22:42 +00:00
var tooltip = buttonsarray [ i ] . tooltip ;
2020-04-15 14:09:57 +00:00
if ( buttonsarray [ i ] . macrokeyboardshortcut && buttonsarray [ i ] . macrokeyboardshortcut . length > 0 ) {
2020-04-14 17:57:56 +00:00
var macrokeyboardshortcut = buttonsarray [ i ] . macrokeyboardshortcut ;
} else {
var macrokeyboardshortcut = "" ;
}
2018-11-26 19:22:53 +00:00
} else {
var icon = "far fa-question-circle" ;
var title = "" ;
2020-04-17 21:07:39 +00:00
var codetype = "gcode"
2018-11-26 19:22:53 +00:00
var gcode = "" ;
2020-04-17 21:07:39 +00:00
var javascript = "" ;
2018-11-26 19:22:53 +00:00
var cls = "" ;
2019-04-12 15:22:42 +00:00
var tooltip = "" ;
2020-04-14 17:57:56 +00:00
var macrokeyboardshortcut = "" ;
2018-11-26 19:22:53 +00:00
}
2020-04-14 17:57:56 +00:00
var macroTemplate = ` <form id="macroEditForm">
2018-11-26 19:22:53 +00:00
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< label class = "cell-sm-3" > Icon < / l a b e l >
< div class = "cell-sm-9" >
< form class = "inline-form" >
< div class = "inline-form" >
< button class = "button outline dark " type = "button" id = "GetIconPicker" data - iconpicker - input = "#macroicon" data - iconpicker - preview = "#IconPreview" > Select Icon < / b u t t o n >
< div class = "h2 m-2" >
< i id = "IconPreview" class = "` + icon + `" > < / i >
< / d i v >
< / d i v >
< input id = "macroicon" type = "hidden" value = "` + icon + `" data - editable = "true" / >
< / f o r m >
2018-11-26 19:22:53 +00:00
< / d i v >
< / d i v >
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< label class = "cell-sm-3" > Label < / l a b e l >
< div class = "cell-sm-9" >
2019-04-22 14:52:10 +00:00
< input id = "macrotitle" type = "text" value = "` + title + `" data - editable = "true" >
2018-11-26 19:22:53 +00:00
< / d i v >
< / d i v >
2019-04-12 15:22:42 +00:00
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< label class = "cell-sm-3" > Tooltip < / l a b e l >
< div class = "cell-sm-9" >
2019-04-22 14:52:10 +00:00
< input id = "macrotooltip" type = "text" value = "` + tooltip + `" data - editable = "true" >
2019-04-12 15:22:42 +00:00
< / d i v >
< / d i v >
2018-11-26 19:22:53 +00:00
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< div class = "cell-sm-3" >
< ul data - tabs - position = "vertical" data - role = "tabs" >
< li id = "editorGcodeModeTab" onclick = "editorGcodeMode();" > < a href = "#" > GCODE < / a > < / l i >
< li id = "editorJavascriptModeTab" onclick = "editorJavascriptMode();" > < a href = "#" > Javascript < / a > < / l i >
< / u l >
< / d i v >
< div class = "cell-sm-9" >
< div id = "macroGcodeEditField" >
2020-06-22 21:04:23 +00:00
< textarea wrap = "off" id = "macrogcode" type = "text" value = "` + gcode + `" style = "overflow-y: auto; height: 200px; max-height: 200px; resize: none;" rows = "4" data - editable = "true" > < / t e x t a r e a >
2020-04-17 21:07:39 +00:00
< span class = "text-small" > Enter GCODE to execute < / s p a n >
< / d i v >
< div id = "macroJavascriptEditField" style = "display:none;" >
2020-06-22 21:04:23 +00:00
< textarea wrap = "off" id = "macrojs" type = "text" value = "" style = "overflow-y: auto; height: 200px; max-height: 200px; resize: none;" rows = "4" data - editable = "true" > < / t e x t a r e a >
2020-04-17 21:07:39 +00:00
< span class = "text-small" > Enter Javascript to execute < / s p a n > < b r >
< span class = "text-small" > tip : Prototype your code using < / s p a n >
< span class = "text-small" > the Devtools Console ( Ctrl + Shift + i > Console ) < / s p a n >
< / d i v >
2018-11-26 19:22:53 +00:00
< / d i v >
< / d i v >
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< label class = "cell-sm-3" > Color < / l a b e l >
< div class = "cell-sm-9" >
2019-04-22 14:52:10 +00:00
< select data - role = "select" id = "macrocls" data - editable = "true" >
2018-11-26 19:22:53 +00:00
< option value = "" selected > Default < / o p t i o n >
< option value = "primary" > Blue < / o p t i o n >
< option value = "info" > Light Blue < / o p t i o n >
< option value = "secondary" > Blue - Gray < / o p t i o n >
< option value = "success" > Green < / o p t i o n >
< option value = "alert" > Red < / o p t i o n >
< option value = "warning" > Orange < / o p t i o n >
< option value = "yellow" > Yellow < / o p t i o n >
< option value = "dark" > Dark < / o p t i o n >
< option value = "light" > Light < / o p t i o n >
< / s e l e c t >
< / d i v >
< / d i v >
2020-04-14 17:57:56 +00:00
< div class = "row mb-2" >
2020-04-17 21:07:39 +00:00
< label class = "cell-sm-3" > Keyboard Shortcut < / l a b e l >
< div class = "cell-sm-9" >
2020-06-22 21:04:23 +00:00
< input id = "macrokeyboardshortcut" class = "macrokeyboardshortcutinput" type = "text" value = "` + macrokeyboardshortcut + `" data - role = "input" data - clear - button = "true" data - editable = "true" onclick = "$('.macrokeyboardshortcutinput').removeClass('newMacroKeyAssignment'); $('#macrokeyboardshortcut').addClass('newMacroKeyAssignment')" >
< span class = "text-small fg-red" id = "alreadyAssignedWarn" style = "display: none;" > < / s p a n >
2020-04-17 21:07:39 +00:00
< span class = "text-small" > Click above to assign a new Keyboard Shortcut / combination to a function . Ctrl , Alt and Shift can be added to create combinations . < / s p a n >
2020-04-14 17:57:56 +00:00
< / d i v >
< / d i v >
2018-11-26 19:22:53 +00:00
< input type = "hidden" id = "macroseq" value = "` + i + `" / >
< / f o r m > `
Metro . dialog . create ( {
title : "Edit Macro" ,
2020-06-22 21:04:23 +00:00
width : 600 ,
2018-11-26 19:22:53 +00:00
content : macroTemplate ,
actions : [ {
caption : "Cancel" ,
cls : "js-dialog-close" ,
onclick : function ( ) {
//
}
} ,
{
caption : "Delete Macro" ,
cls : "js-dialog-close alert" ,
onclick : function ( ) {
buttonsarray . splice ( i , 1 ) ;
populateMacroButtons ( ) ;
}
} ,
{
caption : "Apply" ,
cls : "js-dialog-close success" ,
onclick : function ( ) {
var seq = $ ( '#macroseq' ) . val ( ) ;
if ( buttonsarray [ seq ] ) {
buttonsarray [ seq ] . icon = $ ( '#macroicon' ) . val ( ) ;
buttonsarray [ seq ] . title = $ ( '#macrotitle' ) . val ( ) ;
2020-04-17 21:07:39 +00:00
buttonsarray [ seq ] . codetype = macroCodeType ; // TODO might not be jquery val in final version
2018-11-26 19:22:53 +00:00
buttonsarray [ seq ] . gcode = $ ( '#macrogcode' ) . val ( ) ;
2020-04-17 21:07:39 +00:00
buttonsarray [ seq ] . javascript = $ ( '#macrojs' ) . val ( ) ;
2018-11-26 19:22:53 +00:00
buttonsarray [ seq ] . class = $ ( '#macrocls' ) . val ( ) ;
2019-04-12 15:22:42 +00:00
buttonsarray [ seq ] . tooltip = $ ( '#macrotooltip' ) . val ( ) ;
2020-04-14 17:57:56 +00:00
buttonsarray [ seq ] . macrokeyboardshortcut = $ ( '#macrokeyboardshortcut' ) . val ( ) ;
2018-11-26 19:22:53 +00:00
populateMacroButtons ( ) ;
2020-04-14 17:59:33 +00:00
bindKeys ( )
2018-11-26 19:22:53 +00:00
} else {
buttonsarray . push ( {
title : $ ( '#macrotitle' ) . val ( ) ,
icon : $ ( '#macroicon' ) . val ( ) ,
2020-04-17 21:07:39 +00:00
codetype : macroCodeType , // TODO might not be jquery val in final version
2018-11-26 19:22:53 +00:00
gcode : $ ( '#macrogcode' ) . val ( ) ,
2020-04-17 21:07:39 +00:00
javascript : $ ( '#macrojs' ) . val ( ) ,
2019-04-12 15:22:42 +00:00
class : $ ( '#macrocls' ) . val ( ) ,
2020-04-14 17:57:56 +00:00
tooltip : $ ( '#macrotooltip' ) . val ( ) ,
macrokeyboardshortcut : $ ( '#macrokeyboardshortcut' ) . val ( ) ,
2018-11-26 19:22:53 +00:00
} )
populateMacroButtons ( ) ;
2020-04-14 17:59:33 +00:00
bindKeys ( )
2018-11-26 19:22:53 +00:00
}
}
}
]
} ) ;
2020-04-14 17:57:56 +00:00
$ ( '#macrokeyboardshortcut' ) . bind ( 'keydown' , null , function ( e ) {
2020-04-17 21:07:39 +00:00
console . log ( e )
2020-04-14 17:57:56 +00:00
e . preventDefault ( ) ;
console . log ( e )
var newVal = "" ;
if ( e . altKey ) {
newVal += 'alt+'
}
if ( e . ctrlKey ) {
newVal += 'ctrl+'
}
if ( e . shiftKey ) {
newVal += 'shift+'
}
if ( e . key . toLowerCase ( ) != 'alt' && e . key . toLowerCase ( ) != 'control' && e . key . toLowerCase ( ) != 'shift' ) {
// Handle MetroUI naming non-standards of some keys
if ( e . keyCode == 32 ) {
newVal += 'space' ;
} else if ( e . key . toLowerCase ( ) == 'escape' ) {
newVal += 'esc' ;
} else if ( e . key . toLowerCase ( ) == 'arrowleft' ) {
newVal += 'left' ;
} else if ( e . key . toLowerCase ( ) == 'arrowright' ) {
newVal += 'right' ;
} else if ( e . key . toLowerCase ( ) == 'arrowup' ) {
newVal += 'up' ;
} else if ( e . key . toLowerCase ( ) == 'arrowdown' ) {
newVal += 'down' ;
} else {
newVal += e . key . toLowerCase ( ) ;
}
2020-06-22 21:04:23 +00:00
// $('.newMacroKeyAssignment').val(newVal)
var alreadyAssigned = false ;
var assignedMacro = '' ;
for ( i = 0 ; i < buttonsarray . length ; i ++ ) {
if ( newVal == buttonsarray [ i ] . macrokeyboardshortcut ) {
alreadyAssigned = true ;
assignedMacro = buttonsarray [ i ] . title
}
}
if ( alreadyAssigned ) {
$ ( '#alreadyAssignedWarn' ) . show ( ) ;
$ ( '#alreadyAssignedWarn' ) . html ( newVal + " is already assigned to \"" + assignedMacro + "\"<br>" ) ;
$ ( '#macrokeyboardshortcut' ) . addClass ( "alert" )
} else {
$ ( '#alreadyAssignedWarn' ) . hide ( ) ;
$ ( '#macrokeyboardshortcut' ) . removeClass ( "alert" )
}
2020-04-14 17:57:56 +00:00
}
2020-06-22 21:04:23 +00:00
$ ( '.newMacroKeyAssignment' ) . val ( newVal )
$ ( '#jsedit' ) . val ( javascript ) ;
2020-04-14 17:57:56 +00:00
} ) ;
2018-11-26 19:22:53 +00:00
2020-04-17 21:07:39 +00:00
// var options = {
// placement: 'bottom',
// collision: 'none',
// animation: true,
// hideOnSelect: true,
// };
// // fa iconpicker https://github.com/farbelous/fontawesome-iconpicker
// $('#macroicon').iconpicker(options);
// setTimeout(function() {
IconPicker . Init ( {
// Required: You have to set the path of IconPicker JSON file to "jsonUrl" option. e.g. '/content/plugins/IconPicker/dist/iconpicker-1.5.0.json'
jsonUrl : '/lib/furcanIconPicker/iconpicker-1.5.0.json' ,
searchPlaceholder : 'Search Macro Icon' ,
showAllButton : 'Show All' ,
cancelButton : 'Cancel' ,
noResultsFound : 'No results found.' , // v1.5.0 and the next versions
borderRadius : '0px' , // v1.5.0 and the next versions
} ) ;
// Select your Button element (ID or Class)
IconPicker . Run ( '#GetIconPicker' ) ;
// }, 300)
2018-11-26 19:22:53 +00:00
$ ( "#macrocls" ) . val ( cls ) . trigger ( "change" ) ;
2019-04-01 07:33:15 +00:00
$ ( '#macrogcode' ) . val ( gcode ) ;
2020-04-17 21:07:39 +00:00
$ ( '#macrojs' ) . val ( javascript ) ;
2019-04-01 07:33:15 +00:00
2020-04-17 21:07:39 +00:00
if ( codetype == "gcode" ) {
$ ( "#editorJavascriptModeTab" ) . removeClass ( "active" ) ;
$ ( "#editorGcodeModeTab" ) . addClass ( "active" ) ;
editorGcodeMode ( ) ;
} else if ( codetype == "javascript" ) {
$ ( "#editorGcodeModeTab" ) . removeClass ( "active" ) ;
$ ( "#editorJavascriptModeTab" ) . addClass ( "active" ) ;
editorJavascriptMode ( ) ;
}
2018-11-26 19:22:53 +00:00
}
function run ( i , evt ) {
evt . preventDefault ( ) ;
evt . stopPropagation ( ) ;
console . log ( "Run " + i )
}
// run it to begin
if ( localStorage . getItem ( 'macroButtons' ) ) {
buttonsarray = JSON . parse ( localStorage . getItem ( 'macroButtons' ) ) ;
}
2020-04-15 07:02:35 +00:00
$ ( document ) . ready ( function ( ) {
populateMacroButtons ( )
bindKeys ( )
} ) ;
2020-04-14 17:57:56 +00:00
function searchMacro ( prop , nameKey , myArray ) {
console . log ( nameKey , prop , myArray )
for ( var i = 0 ; i < myArray . length ; i ++ ) {
if ( myArray [ i ] [ prop ] === nameKey ) {
return myArray [ i ] ;
}
}
2020-04-17 21:07:39 +00:00
}
function editorGcodeMode ( ) {
macroCodeType = "gcode" ;
$ ( "#macroGcodeEditField" ) . show ( )
$ ( "#macroJavascriptEditField" ) . hide ( )
}
function editorJavascriptMode ( ) {
macroCodeType = "javascript" ;
$ ( "#macroJavascriptEditField" ) . show ( )
$ ( "#macroGcodeEditField" ) . hide ( )
}
function runJsMacro ( i ) {
console . log ( "Running: " , buttonsarray [ i ] . javascript )
executeJS ( buttonsarray [ i ] . javascript )
// Execute code
}
function executeJS ( js ) {
Function ( `
"use strict" ;
` + js + `
` )();
2020-04-14 17:57:56 +00:00
}