Skip to content

Commit

Permalink
Tidy js script
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyInformation committed Oct 3, 2020
1 parent 8c2cfe8 commit 44ca011
Showing 1 changed file with 85 additions and 67 deletions.
152 changes: 85 additions & 67 deletions moment/nrmoment.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,84 +211,102 @@ <h3>Output String Formatting</h3>
;(function () { // Isolate the front-end code
'use strict'

//#region --------- "global" variables for the panel --------- //

/** Module name must match this nodes html file @constant {string} moduleName */
var moduleName = 'moment'
/** Node's label @constant {string} paletteCategory */
var nodeLabel = 'Date/Time Formatter'
/** Node's palette category @constant {string} paletteCategory */
var paletteCategory = 'formats'
/** Node's background color @constant {string} paletteColor */
var paletteColor = '#E6E0F8'

//#endregion ------------------------------------------------- //

//#region --------- "global" functions for the panel --------- //

function tzValidate(val) {
// TODO: See /contribapi/momentzones in nrmoment.js
// auto-correct the entry
//$('#node-input-inTz').val( val.replace(/^GMT|UTC/i, 'ETC/GMT') )
return true
}

RED.nodes.registerType('moment',{
category: 'formats', // the palette category
color: '#E6E0F8',
defaults: {
name: {value:''},
topic: {value:''},
input: {value:''},
inputType: {value:'msg'},
inTz: {value:'', validate:tzValidate},
adjAmount: {value:0, validate:RED.validators.number()},
adjType: {value:'days'},
adjDir: {value:'add'},
format: {value:''},
locale: {value:''},
output: {value:''},
outputType: {value:'msg'},
outTz: {value:'', validate:tzValidate}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
// set the icon (held in icons dir below where you save the node)
icon: 'timer.png', // saved in icons/myicon.png
label: function() { // sets the default label contents
return this.name||this.topic||'Date/Time Formatter';
},
labelStyle: function() { // sets the class to apply to the label
return this.name?'node_label_italic':'';
},
oneditprepare: function() {
// Make sure we can reference parent this in async fns
var _this = this
//#endregion ------------------------------------------------- //

RED.nodes.registerType(moduleName, {
category: paletteCategory, // the palette category
color: paletteColor,
defaults: {
name: {value:''},
topic: {value:''},
input: {value:''},
inputType: {value:'msg'},
inTz: {value:'', validate:tzValidate},
adjAmount: {value:0, validate:RED.validators.number()},
adjType: {value:'days'},
adjDir: {value:'add'},
format: {value:''},
locale: {value:''},
output: {value:''},
outputType: {value:'msg'},
outTz: {value:'', validate:tzValidate}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
// set the icon (held in icons dir below where you save the node)
icon: 'timer.png', // saved in icons/myicon.png
paletteLabel: nodeLabel,
label: function() { // sets the default label contents
return this.name||this.topic||nodeLabel;
},
labelStyle: function() { // sets the class to apply to the label
return this.name?'node_label_italic':'';
},
oneditprepare: function() {
// Make sure we can reference parent this in async fns
var _this = this

if (!this.inputType) {
this.inputType = 'msg';
}
$('#node-input-input').typedInput({
default: 'msg',
types: ['msg','flow','global','date','str'],
typeField: $('#node-input-inputType')
});
if (!this.outputType) {
this.outputType = 'msg';
}
$('#node-input-output').typedInput({
default: 'msg',
types: ['msg','flow','global'],
typeField: $('#node-input-outputType')
});
if (!this.inputType) {
this.inputType = 'msg';
}
$('#node-input-input').typedInput({
default: 'msg',
types: ['msg','flow','global','date','str'],
typeField: $('#node-input-inputType')
});
if (!this.outputType) {
this.outputType = 'msg';
}
$('#node-input-output').typedInput({
default: 'msg',
types: ['msg','flow','global'],
typeField: $('#node-input-outputType')
});

// Get host locale and timezone
$.getJSON('contribapi/moment',function(data) {
// Save to parent object
_this.localeData = data
// Get host locale and timezone
$.getJSON('contribapi/moment',function(data) {
// Save to parent object
_this.localeData = data

if ( $('#node-input-inTz').val() === '' ) $('#node-input-inTz').val(data.tz);
if ( $('#node-input-outTz').val() === '' ) $('#node-input-outTz').val(data.tz);
if ( $('#node-input-locale').val() === '' ) $('#node-input-locale').val(data.locale);
});
if ( $('#node-input-inTz').val() === '' ) $('#node-input-inTz').val(data.tz);
if ( $('#node-input-outTz').val() === '' ) $('#node-input-outTz').val(data.tz);
if ( $('#node-input-locale').val() === '' ) $('#node-input-locale').val(data.locale);
});

// Autocorrect timezone entries
$('#node-input-inTz').add('#node-input-outTz').blur( function(){
if( this.value === '' ) {
// Use saved locale data from parent object as default
this.value = _this.localeData.tz
} else {
// autocorrect common tz errors
this.value = this.value.trim().replace(/^(GMT|UTC)/i, 'ETC/$1')
}
})
// Autocorrect timezone entries
$('#node-input-inTz').add('#node-input-outTz').blur( function(){
if( this.value === '' ) {
// Use saved locale data from parent object as default
this.value = _this.localeData.tz
} else {
// autocorrect common tz errors
this.value = this.value.trim().replace(/^(GMT|UTC)/i, 'ETC/$1')
}
})

} // --- end of oneditprepare --- //
}); // ---- end of RED.nodes.registerType ---- //
}, // --- end of oneditprepare --- //
}) // ---- end of RED.nodes.registerType ---- //
})()
</script>

0 comments on commit 44ca011

Please sign in to comment.