2015-02-05 16:05:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* configurator.js
|
|
|
|
|
*
|
|
|
|
|
* Marlin Configuration Utility
|
|
|
|
|
* - Web form for entering configuration options
|
|
|
|
|
* - A reprap calculator to calculate movement values
|
|
|
|
|
* - Uses HTML5 to generate downloadables in Javascript
|
|
|
|
|
* - Reads and parses standard configuration files from local folders
|
|
|
|
|
*
|
|
|
|
|
* Supporting functions
|
|
|
|
|
* - Parser to read Marlin Configuration.h and Configuration_adv.h files
|
|
|
|
|
* - Utilities to replace values in configuration files
|
|
|
|
|
*/
|
2015-02-05 17:22:51 +01:00
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2015-02-05 16:05:29 +01:00
|
|
|
|
$(function(){
|
|
|
|
|
|
2015-02-06 23:58:47 +01:00
|
|
|
|
var marlin_config = 'config';
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
|
|
|
|
// Extend String
|
|
|
|
|
String.prototype.lpad = function(len, chr) {
|
|
|
|
|
if (chr === undefined) { chr = ' '; }
|
|
|
|
|
var s = this+'', need = len - s.length;
|
|
|
|
|
if (need > 0) { s = new Array(need+1).join(chr) + s; }
|
|
|
|
|
return s;
|
|
|
|
|
};
|
2015-02-07 09:46:14 +01:00
|
|
|
|
String.prototype.prePad = function(len, chr) { return len ? this.lpad(len, chr) : this; };
|
|
|
|
|
String.prototype.zeroPad = function(len) { return this.prePad(len, '0'); };
|
|
|
|
|
String.prototype.regEsc = function() { return this.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"); }
|
2015-02-07 15:20:04 +01:00
|
|
|
|
String.prototype.lineCount = function() { var len = this.split(/\r?\n|\r/).length; return len > 0 ? len - 1 : len; };
|
2015-02-06 04:30:11 +01:00
|
|
|
|
|
2015-02-05 16:05:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* selectField.addOptions takes an array or keyed object
|
|
|
|
|
*/
|
|
|
|
|
$.fn.extend({
|
|
|
|
|
addOptions: function(arrObj) {
|
|
|
|
|
return this.each(function() {
|
|
|
|
|
var sel = $(this);
|
|
|
|
|
var isArr = Object.prototype.toString.call(arrObj) == "[object Array]";
|
|
|
|
|
$.each(arrObj, function(k, v) {
|
|
|
|
|
sel.append( $('<option>',{value:isArr?v:k}).text(v) );
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The app is a singleton
|
|
|
|
|
var configuratorApp = (function(){
|
|
|
|
|
|
|
|
|
|
// private variables and functions go here
|
|
|
|
|
var self,
|
|
|
|
|
pi2 = Math.PI * 2,
|
2015-02-07 02:57:31 +01:00
|
|
|
|
has_boards = false, has_config = false, has_config_adv = false,
|
2015-02-05 20:14:31 +01:00
|
|
|
|
boards_file = 'boards.h',
|
|
|
|
|
config_file = 'Configuration.h',
|
|
|
|
|
config_adv_file = 'Configuration_adv.h',
|
2015-02-07 18:03:00 +01:00
|
|
|
|
$tooltip = $('#tooltip'),
|
2015-02-05 16:05:29 +01:00
|
|
|
|
$config = $('#config_text'),
|
|
|
|
|
$config_adv = $('#config_adv_text'),
|
|
|
|
|
boards_list = {},
|
2015-02-07 02:57:31 +01:00
|
|
|
|
therms_list = {},
|
|
|
|
|
total_config_lines,
|
2015-02-07 16:39:10 +01:00
|
|
|
|
total_config_adv_lines,
|
2015-02-07 19:45:56 +01:00
|
|
|
|
hover_timer,
|
|
|
|
|
pulse_offset = 0;
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
|
|
|
|
// Return this anonymous object as configuratorApp
|
|
|
|
|
return {
|
|
|
|
|
my_public_var: 4,
|
2015-02-06 04:30:11 +01:00
|
|
|
|
logging: 1,
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
|
self = this; // a 'this' for use when 'this' is something else
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
// Set up the form
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.initConfigForm();
|
2015-02-07 02:57:31 +01:00
|
|
|
|
|
2015-02-06 05:28:39 +01:00
|
|
|
|
// Make tabs for the fieldsets
|
|
|
|
|
var $fset = $('#config_form fieldset');
|
|
|
|
|
var $tabs = $('<ul>',{class:'tabs'}), ind = 1;
|
|
|
|
|
$('#config_form fieldset').each(function(){
|
|
|
|
|
var tabID = 'TAB'+ind;
|
|
|
|
|
$(this).addClass(tabID);
|
|
|
|
|
var $leg = $(this).find('legend');
|
|
|
|
|
var $link = $('<a>',{href:'#'+ind,id:tabID}).text($leg.text());
|
|
|
|
|
$tabs.append($('<li>').append($link));
|
|
|
|
|
$link.click(function(e){
|
|
|
|
|
e.preventDefault;
|
|
|
|
|
var ind = this.id;
|
|
|
|
|
$tabs.find('.active').removeClass('active');
|
|
|
|
|
$(this).addClass('active');
|
|
|
|
|
$fset.hide();
|
|
|
|
|
$fset.filter('.'+this.id).show();
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
ind++;
|
|
|
|
|
});
|
|
|
|
|
$tabs.appendTo('#tabs');
|
|
|
|
|
$('<br>',{class:'clear'}).appendTo('#tabs');
|
|
|
|
|
$tabs.find('a:first').trigger('click');
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
// Make a droppable file uploader, if possible
|
2015-02-05 20:14:31 +01:00
|
|
|
|
var $uploader = $('#file-upload');
|
|
|
|
|
var fileUploader = new BinaryFileUploader({
|
|
|
|
|
element: $uploader[0],
|
2015-02-06 04:30:11 +01:00
|
|
|
|
onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
|
2015-02-05 20:14:31 +01:00
|
|
|
|
});
|
2015-02-07 08:46:16 +01:00
|
|
|
|
if (!fileUploader.hasFileUploaderSupport())
|
|
|
|
|
this.setMessage("Your browser doesn't support the file reading API.", 'error');
|
|
|
|
|
|
|
|
|
|
// Read boards.h, Configuration.h, Configuration_adv.h
|
|
|
|
|
var ajax_count = 0, success_count = 0;
|
|
|
|
|
var loaded_items = {};
|
|
|
|
|
var config_files = [boards_file, config_file, config_adv_file];
|
|
|
|
|
$.each(config_files, function(i,fname){
|
|
|
|
|
$.ajax({
|
|
|
|
|
url: marlin_config+'/'+fname,
|
|
|
|
|
type: 'GET',
|
|
|
|
|
async: true,
|
|
|
|
|
cache: false,
|
|
|
|
|
success: function(txt) {
|
|
|
|
|
loaded_items[fname] = function(){ self.fileLoaded(fname, txt); };
|
|
|
|
|
success_count++;
|
|
|
|
|
},
|
|
|
|
|
complete: function() {
|
|
|
|
|
ajax_count++;
|
|
|
|
|
if (ajax_count >= 3) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
$.each(config_files, function(i,fname){ if (loaded_items[fname] !== undefined) loaded_items[fname](); });
|
2015-02-07 08:46:16 +01:00
|
|
|
|
self.refreshConfigForm();
|
|
|
|
|
if (success_count < ajax_count)
|
|
|
|
|
self.setMessage('Unable to load configurations. Use the upload field instead.', 'error');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-02-05 16:05:29 +01:00
|
|
|
|
});
|
2015-02-07 08:46:16 +01:00
|
|
|
|
},
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
setMessage: function(msg,type) {
|
|
|
|
|
if (msg) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (type === undefined) type = 'message';
|
2015-02-07 08:46:16 +01:00
|
|
|
|
var $err = $('<p class="'+type+'">'+msg+'</p>'), err = $err[0];
|
|
|
|
|
$('#message').prepend($err);
|
|
|
|
|
var baseColor = $err.css('color').replace(/rgba?\(([^),]+,[^),]+,[^),]+).*/, 'rgba($1,');
|
|
|
|
|
var d = new Date();
|
2015-02-07 19:45:56 +01:00
|
|
|
|
err.pulse_offset = (pulse_offset += 200);
|
|
|
|
|
err.startTime = d.getTime() + pulse_offset;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
err.pulser = setInterval(function(){
|
|
|
|
|
d = new Date();
|
2015-02-07 19:45:56 +01:00
|
|
|
|
var pulse_time = d.getTime() + err.pulse_offset;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
$err.css({color:baseColor+(0.5+Math.sin(pulse_time/200)*0.4)+')'});
|
2015-02-07 19:45:56 +01:00
|
|
|
|
if (pulse_time - err.startTime > 5000) {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
clearInterval(err.pulser);
|
|
|
|
|
$err.remove();
|
|
|
|
|
}
|
|
|
|
|
}, 50);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$('#message p.error, #message p.warning').each(function() {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (this.pulser !== undefined && this.pulser)
|
2015-02-07 08:46:16 +01:00
|
|
|
|
clearInterval(this.pulser);
|
|
|
|
|
$(this).remove();
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Init the boards array from a boards.h file
|
|
|
|
|
*/
|
2015-02-05 20:14:31 +01:00
|
|
|
|
initBoardsFromText: function(txt) {
|
|
|
|
|
boards_list = {};
|
2015-02-06 04:30:11 +01:00
|
|
|
|
var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[\\w_]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
|
2015-02-05 20:14:31 +01:00
|
|
|
|
while((r = findDef.exec(txt)) !== null) {
|
|
|
|
|
boards_list[r[1]] = r[2].prePad(3, ' ') + " — " + r[4].replace(/\).*/, ')');
|
|
|
|
|
}
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.log("Loaded boards", 3); this.log(boards_list, 3);
|
|
|
|
|
has_boards = true;
|
2015-02-05 20:14:31 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Init the thermistors array from the Configuration.h file
|
|
|
|
|
*/
|
2015-02-05 20:14:31 +01:00
|
|
|
|
initThermistorsFromText: function(txt) {
|
|
|
|
|
// Get all the thermistors and save them into an object
|
|
|
|
|
var r, s, findDef = new RegExp('(//.*\n)+\\s+(#define[ \\t]+TEMP_SENSOR_0)', 'g');
|
|
|
|
|
r = findDef.exec(txt);
|
|
|
|
|
findDef = new RegExp('^//[ \\t]*([-\\d]+)[ \\t]+is[ \\t]+(.*)[ \\t]*$', 'gm');
|
|
|
|
|
while((s = findDef.exec(r[0])) !== null) {
|
|
|
|
|
therms_list[s[1]] = s[1].prePad(4, ' ') + " — " + s[2];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Handle a file being dropped on the file field
|
|
|
|
|
*/
|
|
|
|
|
handleFileLoad: function(txt, $uploader) {
|
|
|
|
|
txt += '';
|
2015-02-05 20:14:31 +01:00
|
|
|
|
var filename = $uploader.val().replace(/.*[\/\\](.*)$/, '$1');
|
|
|
|
|
switch(filename) {
|
|
|
|
|
case boards_file:
|
2015-02-07 08:46:16 +01:00
|
|
|
|
case config_file:
|
|
|
|
|
case config_adv_file:
|
|
|
|
|
this.fileLoaded(filename, txt);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this.log("Can't parse "+filename, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 15:20:04 +01:00
|
|
|
|
/**
|
|
|
|
|
* Process a file after it's been successfully loaded
|
|
|
|
|
*/
|
2015-02-07 08:46:16 +01:00
|
|
|
|
fileLoaded: function(filename, txt) {
|
|
|
|
|
this.log("fileLoaded:"+filename,4);
|
|
|
|
|
switch(filename) {
|
|
|
|
|
case boards_file:
|
|
|
|
|
this.initBoardsFromText(txt);
|
2015-02-05 20:14:31 +01:00
|
|
|
|
$('#MOTHERBOARD').html('').addOptions(boards_list);
|
2015-02-07 02:57:31 +01:00
|
|
|
|
if (has_config) this.initField('MOTHERBOARD');
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setMessage(boards_file+' loaded successfully.');
|
2015-02-07 02:57:31 +01:00
|
|
|
|
break;
|
|
|
|
|
case config_file:
|
|
|
|
|
if (has_boards) {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
$config.text(txt);
|
2015-02-07 09:46:14 +01:00
|
|
|
|
total_config_lines = txt.lineCount();
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.initThermistorsFromText(txt);
|
2015-02-07 02:57:31 +01:00
|
|
|
|
this.purgeDefineInfo(false);
|
|
|
|
|
this.refreshConfigForm();
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setMessage(config_file+' loaded successfully.');
|
|
|
|
|
has_config = true;
|
2015-02-07 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setMessage("Upload a " + boards_file + " file first!", 'error');
|
2015-02-07 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case config_adv_file:
|
|
|
|
|
if (has_config) {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
$config_adv.text(txt);
|
2015-02-07 09:46:14 +01:00
|
|
|
|
total_config_adv_lines = txt.lineCount();
|
2015-02-07 02:57:31 +01:00
|
|
|
|
this.purgeDefineInfo(true);
|
|
|
|
|
this.refreshConfigForm();
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setMessage(config_adv_file+' loaded successfully.');
|
|
|
|
|
has_config_adv = true;
|
2015-02-07 02:57:31 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setMessage("Upload a " + config_file + " file first!", 'error');
|
2015-02-07 02:57:31 +01:00
|
|
|
|
}
|
2015-02-05 20:14:31 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Add enhancements to the form
|
|
|
|
|
*/
|
|
|
|
|
initConfigForm: function() {
|
2015-02-05 16:05:29 +01:00
|
|
|
|
// Modify form fields and make the form responsive.
|
|
|
|
|
// As values change on the form, we could update the
|
|
|
|
|
// contents of text areas containing the configs, for
|
|
|
|
|
// example.
|
|
|
|
|
|
|
|
|
|
// while(!$config_adv.text() == null) {}
|
|
|
|
|
// while(!$config.text() == null) {}
|
|
|
|
|
|
|
|
|
|
// Go through all form items with names
|
|
|
|
|
$('#config_form').find('[name]').each(function() {
|
|
|
|
|
// Set its id to its name
|
|
|
|
|
var name = $(this).attr('name');
|
|
|
|
|
$(this).attr({id: name});
|
|
|
|
|
// Attach its label sibling
|
2015-02-07 15:20:04 +01:00
|
|
|
|
var $label = $(this).prev('label');
|
|
|
|
|
if ($label.length) $label.attr('for',name);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Get all 'switchable' class items and add a checkbox
|
|
|
|
|
$('#config_form .switchable').each(function(){
|
|
|
|
|
$(this).after(
|
|
|
|
|
$('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
|
|
|
|
|
.attr('id',this.id + '-switch')
|
|
|
|
|
.change(self.handleSwitch)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
// Add options to the popup menus
|
2015-02-05 20:14:31 +01:00
|
|
|
|
$('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
|
|
|
|
|
$('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
|
|
|
|
|
$('#EXTRUDERS').addOptions([1,2,3,4]);
|
|
|
|
|
$('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
// Replace the Serial popup menu with a stepper control
|
2015-02-07 02:57:31 +01:00
|
|
|
|
$('#serial_stepper').jstepper({
|
|
|
|
|
min: 0,
|
2015-02-07 08:46:16 +01:00
|
|
|
|
max: 3,
|
2015-02-07 02:57:31 +01:00
|
|
|
|
val: $('#SERIAL_PORT').val(),
|
|
|
|
|
arrowWidth: '18px',
|
|
|
|
|
arrowHeight: '15px',
|
|
|
|
|
color: '#FFF',
|
|
|
|
|
acolor: '#F70',
|
|
|
|
|
hcolor: '#FF0',
|
|
|
|
|
id: 'select-me',
|
|
|
|
|
textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
|
|
|
|
|
onChange: function(v) { $('#SERIAL_PORT').val(v).trigger('change'); }
|
|
|
|
|
});
|
2015-02-05 20:14:31 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 15:20:04 +01:00
|
|
|
|
/**
|
|
|
|
|
* Update all fields on the form after loading a configuration
|
|
|
|
|
*/
|
2015-02-05 20:14:31 +01:00
|
|
|
|
refreshConfigForm: function() {
|
|
|
|
|
|
2015-02-05 16:05:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* For now I'm manually creating these references
|
|
|
|
|
* but I should be able to parse Configuration.h
|
|
|
|
|
* and iterate the #defines.
|
|
|
|
|
*
|
|
|
|
|
* For any #ifdef blocks I can create field groups
|
|
|
|
|
* which can be dimmed together when the option
|
|
|
|
|
* is disabled.
|
|
|
|
|
*
|
|
|
|
|
* Then we only need to specify exceptions to
|
|
|
|
|
* standard behavior, (which is to add a text field)
|
|
|
|
|
*/
|
|
|
|
|
this.initField('SERIAL_PORT');
|
|
|
|
|
|
|
|
|
|
this.initField('BAUDRATE');
|
|
|
|
|
|
|
|
|
|
this.initField('BTENABLED');
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
$('#MOTHERBOARD').html('').addOptions(boards_list);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
this.initField('MOTHERBOARD');
|
|
|
|
|
|
|
|
|
|
this.initField('CUSTOM_MENDEL_NAME');
|
|
|
|
|
|
|
|
|
|
this.initField('MACHINE_UUID');
|
|
|
|
|
|
|
|
|
|
this.initField('EXTRUDERS');
|
|
|
|
|
|
|
|
|
|
this.initField('POWER_SUPPLY');
|
|
|
|
|
|
|
|
|
|
this.initField('PS_DEFAULT_OFF');
|
|
|
|
|
|
2015-02-05 20:14:31 +01:00
|
|
|
|
$('#TEMP_SENSOR_0, #TEMP_SENSOR_1, #TEMP_SENSOR_2, #TEMP_SENSOR_BED').html('').addOptions(therms_list);
|
2015-02-05 17:19:09 +01:00
|
|
|
|
this.initField('TEMP_SENSOR_0');
|
|
|
|
|
this.initField('TEMP_SENSOR_1');
|
|
|
|
|
this.initField('TEMP_SENSOR_2');
|
|
|
|
|
this.initField('TEMP_SENSOR_BED');
|
|
|
|
|
|
2015-02-06 04:30:11 +01:00
|
|
|
|
this.initField('TEMP_SENSOR_1_AS_REDUNDANT');
|
|
|
|
|
this.initField('MAX_REDUNDANT_TEMP_SENSOR_DIFF');
|
|
|
|
|
|
|
|
|
|
this.initField('TEMP_RESIDENCY_TIME');
|
2015-02-07 08:46:16 +01:00
|
|
|
|
},
|
2015-02-05 20:44:43 +01:00
|
|
|
|
|
2015-02-06 04:30:11 +01:00
|
|
|
|
/**
|
2015-02-07 08:46:16 +01:00
|
|
|
|
* Make a field responsive and initialize its defineInfo
|
2015-02-06 04:30:11 +01:00
|
|
|
|
*/
|
2015-02-07 02:57:31 +01:00
|
|
|
|
initField: function(name, adv) {
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.log("initField:"+name,4);
|
2015-02-06 04:30:11 +01:00
|
|
|
|
var $elm = $('#'+name), elm = $elm[0];
|
2015-02-07 08:46:16 +01:00
|
|
|
|
if (elm.defineInfo == null) {
|
2015-02-07 15:20:04 +01:00
|
|
|
|
var inf = elm.defineInfo = this.getDefineInfo(name, adv);
|
2015-02-06 04:30:11 +01:00
|
|
|
|
$elm.on($elm.attr('type') == 'text' ? 'input' : 'change', this.handleChange);
|
2015-02-07 16:39:10 +01:00
|
|
|
|
|
|
|
|
|
if (inf.comment) {
|
|
|
|
|
var $tipme = $elm.prev('label');
|
2015-02-07 18:03:00 +01:00
|
|
|
|
if ($tipme.length) {
|
2015-02-07 16:39:10 +01:00
|
|
|
|
$tipme.hover(
|
|
|
|
|
function() {
|
2015-02-07 18:03:00 +01:00
|
|
|
|
var pos = $tipme.position();
|
|
|
|
|
$tooltip.text(inf.comment)
|
2015-02-07 16:39:10 +01:00
|
|
|
|
.append('<span>')
|
2015-02-07 18:03:00 +01:00
|
|
|
|
.css({bottom:($tooltip.parent().outerHeight()-pos.top)+'px',left:(pos.left+70)+'px'})
|
2015-02-07 16:39:10 +01:00
|
|
|
|
.show();
|
|
|
|
|
if (hover_timer) {
|
|
|
|
|
clearTimeout(hover_timer);
|
|
|
|
|
hover_timer = null;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function() {
|
|
|
|
|
hover_timer = setTimeout(function(){
|
|
|
|
|
hover_timer = null;
|
2015-02-07 18:03:00 +01:00
|
|
|
|
$tooltip.fadeOut(400);
|
2015-02-07 16:39:10 +01:00
|
|
|
|
}, 400);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-02-07 15:20:04 +01:00
|
|
|
|
}
|
2015-02-06 04:30:11 +01:00
|
|
|
|
}
|
2015-02-05 16:05:29 +01:00
|
|
|
|
this.setFieldFromDefine(name);
|
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Handle any value field being changed
|
|
|
|
|
*/
|
|
|
|
|
handleChange: function() { self.updateDefineFromField(this.id); },
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Handle a switch checkbox being changed
|
|
|
|
|
*/
|
|
|
|
|
handleSwitch: function() {
|
|
|
|
|
var $elm = $(this), $prev = $elm.prev();
|
2015-02-05 16:05:29 +01:00
|
|
|
|
var on = $elm.prop('checked') || false;
|
|
|
|
|
$prev.attr('disabled', !on);
|
2015-02-06 04:30:11 +01:00
|
|
|
|
self.setDefineEnabled($prev[0].id, on);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get the current value of a #define (from the config text)
|
|
|
|
|
*/
|
|
|
|
|
defineValue: function(name) {
|
|
|
|
|
this.log('defineValue:'+name,4);
|
|
|
|
|
var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
if (inf == null) return 'n/a';
|
2015-02-07 02:57:31 +01:00
|
|
|
|
var result = inf.regex.exec($(inf.field).text());
|
|
|
|
|
|
|
|
|
|
this.log(result,2);
|
|
|
|
|
|
|
|
|
|
return inf.type == 'switch' ? result[inf.val_i] != '//' : result[inf.val_i];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current enabled state of a #define (from the config text)
|
|
|
|
|
*/
|
|
|
|
|
defineIsEnabled: function(name) {
|
|
|
|
|
this.log('defineIsEnabled:'+name,4);
|
|
|
|
|
var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
if (inf == null) return false;
|
2015-02-07 02:57:31 +01:00
|
|
|
|
var result = inf.regex.exec($(inf.field).text());
|
|
|
|
|
|
|
|
|
|
this.log(result,2);
|
|
|
|
|
|
|
|
|
|
var on = result !== null ? result[1].trim() != '//' : true;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.log(name + ' = ' + on, 2);
|
2015-02-07 02:57:31 +01:00
|
|
|
|
|
|
|
|
|
return on;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a #define enabled or disabled by altering the config text
|
|
|
|
|
*/
|
2015-02-06 04:30:11 +01:00
|
|
|
|
setDefineEnabled: function(name, val) {
|
2015-02-07 02:57:31 +01:00
|
|
|
|
this.log('setDefineEnabled:'+name,4);
|
2015-02-07 08:46:16 +01:00
|
|
|
|
var $elm = $('#'+name), inf = $elm[0].defineInfo;
|
|
|
|
|
if (inf == null) return;
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
2015-02-06 04:39:42 +01:00
|
|
|
|
var slash = val ? '' : '//';
|
2015-02-06 04:30:11 +01:00
|
|
|
|
var newline = inf.line
|
2015-02-07 08:46:16 +01:00
|
|
|
|
.replace(/^([ \t]*)(\/\/)([ \t]*)/, '$1$3') // remove slashes
|
|
|
|
|
.replace(inf.pre+inf.define, inf.pre+slash+inf.define); // add them back
|
2015-02-07 03:09:14 +01:00
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setDefineLine(name, newline);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
/**
|
|
|
|
|
* Update a #define (from the form) by altering the config text
|
|
|
|
|
*/
|
|
|
|
|
updateDefineFromField: function(name) {
|
|
|
|
|
this.log('updateDefineFromField:'+name,4);
|
2015-02-07 08:46:16 +01:00
|
|
|
|
var $elm = $('#'+name), inf = $elm[0].defineInfo;
|
|
|
|
|
if (inf == null) return;
|
2015-02-06 04:30:11 +01:00
|
|
|
|
|
|
|
|
|
var isCheck = $elm.attr('type') == 'checkbox',
|
|
|
|
|
val = isCheck ? $elm.prop('checked') : $elm.val();
|
|
|
|
|
|
|
|
|
|
var newline;
|
|
|
|
|
switch(inf.type) {
|
|
|
|
|
case 'switch':
|
|
|
|
|
var slash = val ? '' : '//';
|
2015-02-07 11:04:44 +01:00
|
|
|
|
newline = inf.line.replace(inf.repl, '$1'+slash+'$3');
|
2015-02-06 04:30:11 +01:00
|
|
|
|
break;
|
|
|
|
|
case 'quoted':
|
|
|
|
|
case 'plain':
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (isCheck)
|
|
|
|
|
this.setMessage(name + ' should not be a checkbox!', 'error');
|
|
|
|
|
else
|
|
|
|
|
newline = inf.line.replace(inf.repl, '$1'+val.replace('$','\\$')+'$3');
|
2015-02-06 04:30:11 +01:00
|
|
|
|
break;
|
2015-02-05 16:05:29 +01:00
|
|
|
|
}
|
2015-02-07 08:46:16 +01:00
|
|
|
|
this.setDefineLine(name, newline);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the define's line in the text to a new line,
|
|
|
|
|
* then update, highlight, and scroll to the line
|
|
|
|
|
*/
|
|
|
|
|
setDefineLine: function(name, newline) {
|
|
|
|
|
var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
|
|
|
|
|
var $c = $(inf.field), txt = $c.text();
|
|
|
|
|
|
|
|
|
|
var hilite_token = '[HIGHLIGHTER-TOKEN]';
|
|
|
|
|
|
|
|
|
|
txt = txt.replace(inf.line, hilite_token + newline);
|
2015-02-06 04:30:11 +01:00
|
|
|
|
inf.line = newline;
|
2015-02-07 08:46:16 +01:00
|
|
|
|
|
2015-02-06 04:30:11 +01:00
|
|
|
|
this.log(newline, 2);
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
// Convert txt into HTML before storing
|
|
|
|
|
var html = $('<div/>').text(txt).html().replace(hilite_token, '<span></span>');
|
|
|
|
|
|
|
|
|
|
// Set the final text including the highlighter
|
|
|
|
|
$c.html(html);
|
2015-02-07 02:57:31 +01:00
|
|
|
|
|
2015-02-07 03:09:14 +01:00
|
|
|
|
// Scroll to reveal the define
|
|
|
|
|
this.scrollToDefine(name);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2015-02-07 08:46:16 +01:00
|
|
|
|
* Scroll a pre box to reveal a #define
|
2015-02-07 03:09:14 +01:00
|
|
|
|
*/
|
|
|
|
|
scrollToDefine: function(name, always) {
|
|
|
|
|
this.log('scrollToDefine:'+name,4);
|
|
|
|
|
var $elm = $('#'+name), inf = $elm[0].defineInfo, $c = $(inf.field);
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
// Scroll to the altered text if it isn't visible
|
|
|
|
|
var halfHeight = $c.height()/2, scrollHeight = $c.prop('scrollHeight'),
|
|
|
|
|
textScrollY = inf.lineNum * scrollHeight/(inf.adv ? total_config_adv_lines : total_config_lines) - halfHeight;
|
|
|
|
|
|
|
|
|
|
if (textScrollY < 0)
|
|
|
|
|
textScrollY = 0;
|
|
|
|
|
else if (textScrollY > scrollHeight)
|
|
|
|
|
textScrollY = scrollHeight - 1;
|
|
|
|
|
|
2015-02-07 03:09:14 +01:00
|
|
|
|
if (always == true || Math.abs($c.prop('scrollTop') - textScrollY) > halfHeight)
|
2015-02-07 02:57:31 +01:00
|
|
|
|
$c.animate({ scrollTop: textScrollY < 0 ? 0 : textScrollY });
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
/**
|
|
|
|
|
* Set a form field to the current #define value in the config text
|
|
|
|
|
*/
|
|
|
|
|
setFieldFromDefine: function(name) {
|
|
|
|
|
var $elm = $('#'+name), val = this.defineValue(name);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
2015-02-06 04:30:11 +01:00
|
|
|
|
this.log('setFieldFromDefine:' + name + ' to ' + val, 4);
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
// Set the field value
|
|
|
|
|
$elm.attr('type') == 'checkbox' ? $elm.prop('checked', val) : $elm.val(''+val);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
|
|
|
|
|
// If the item has a checkbox then set enabled state too
|
|
|
|
|
var $cb = $('#'+name+'-switch');
|
|
|
|
|
if ($cb.length) {
|
2015-02-07 02:57:31 +01:00
|
|
|
|
var on = self.defineIsEnabled(name);
|
|
|
|
|
$elm.attr('disabled', !on); // enable/disable the form field (could also dim it)
|
|
|
|
|
$cb.prop('checked', on); // check/uncheck the checkbox
|
2015-02-05 16:05:29 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 02:57:31 +01:00
|
|
|
|
/**
|
|
|
|
|
* Purge #define information for one of the config files
|
|
|
|
|
*/
|
|
|
|
|
purgeDefineInfo: function(adv) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (adv === undefined) adv = false;
|
2015-02-07 12:02:26 +01:00
|
|
|
|
$('[name]').each(function() {
|
|
|
|
|
var inf = this.defineInfo;
|
|
|
|
|
if (inf && adv === inf.adv) $(this).removeProp('defineInfo');
|
2015-02-07 02:57:31 +01:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update #define information for one of the config files
|
|
|
|
|
*/
|
|
|
|
|
refreshDefineInfo: function(adv) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (adv === undefined) adv = false;
|
2015-02-07 12:02:26 +01:00
|
|
|
|
$('[name]').each(function() {
|
|
|
|
|
var inf = this.defineInfo;
|
|
|
|
|
if (inf && adv == inf.adv) this.defineInfo = self.getDefineInfo(this.id, adv);
|
2015-02-07 02:57:31 +01:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get information about a #define from configuration file text:
|
|
|
|
|
*
|
|
|
|
|
* Pre-examine the #define for its prefix, value position, suffix, etc.
|
|
|
|
|
* Construct a regex for the #define to quickly find (and replace) values.
|
|
|
|
|
* Store the existing #define line as the key to finding it later.
|
|
|
|
|
* Determine the line number of the #define so it can be scrolled to.
|
|
|
|
|
*/
|
2015-02-06 04:30:11 +01:00
|
|
|
|
getDefineInfo: function(name, adv) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (adv === undefined) adv = false;
|
2015-02-06 04:30:11 +01:00
|
|
|
|
this.log('getDefineInfo:'+name,4);
|
2015-02-07 15:20:04 +01:00
|
|
|
|
var $elm = $('#'+name), elm = $elm[0],
|
|
|
|
|
$c = adv ? $config_adv : $config,
|
|
|
|
|
txt = $c.text();
|
2015-02-06 04:30:11 +01:00
|
|
|
|
|
|
|
|
|
// a switch line with no value
|
2015-02-07 15:20:04 +01:00
|
|
|
|
var findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm'),
|
|
|
|
|
result = findDef.exec(txt),
|
|
|
|
|
info = { type:0, adv:adv, field:$c[0], val_i: 2 };
|
2015-02-06 04:30:11 +01:00
|
|
|
|
if (result !== null) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
$.extend(info, {
|
|
|
|
|
val_i: 1,
|
|
|
|
|
type: 'switch',
|
|
|
|
|
line: result[0], // whole line
|
|
|
|
|
pre: result[1] === undefined ? '' : result[1].replace('//',''),
|
2015-02-06 04:30:11 +01:00
|
|
|
|
define: result[2],
|
2015-02-07 11:04:44 +01:00
|
|
|
|
post: result[3] === undefined ? '' : result[3]
|
|
|
|
|
});
|
2015-02-07 15:20:04 +01:00
|
|
|
|
info.regex = new RegExp('([ \\t]*//)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
|
|
|
|
|
info.repl = new RegExp('([ \\t]*)(\/\/)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
|
2015-02-05 16:05:29 +01:00
|
|
|
|
}
|
2015-02-07 11:04:44 +01:00
|
|
|
|
else {
|
|
|
|
|
// a define with quotes
|
|
|
|
|
findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
|
2015-02-07 15:20:04 +01:00
|
|
|
|
result = findDef.exec(txt);
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (result !== null) {
|
|
|
|
|
$.extend(info, {
|
|
|
|
|
type: 'quoted',
|
|
|
|
|
line: result[0],
|
|
|
|
|
pre: result[1] === undefined ? '' : result[1].replace('//',''),
|
|
|
|
|
define: result[2],
|
|
|
|
|
post: result[4] === undefined ? '' : result[4]
|
|
|
|
|
});
|
2015-02-07 15:20:04 +01:00
|
|
|
|
info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
|
|
|
|
|
info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
|
2015-02-07 11:04:44 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// a define with no quotes
|
2015-02-07 15:20:04 +01:00
|
|
|
|
findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
|
|
|
|
|
result = findDef.exec(txt);
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (result !== null) {
|
|
|
|
|
$.extend(info, {
|
|
|
|
|
type: 'plain',
|
|
|
|
|
line: result[0],
|
|
|
|
|
pre: result[1] === undefined ? '' : result[1].replace('//',''),
|
|
|
|
|
define: result[2],
|
|
|
|
|
post: result[4] === undefined ? '' : result[4]
|
|
|
|
|
});
|
2015-02-07 15:20:04 +01:00
|
|
|
|
info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
|
|
|
|
|
info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
|
2015-02-07 11:04:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-06 04:30:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (info.type) {
|
2015-02-07 15:20:04 +01:00
|
|
|
|
// Get the end-of-line comment, if there is one
|
2015-02-07 19:34:16 +01:00
|
|
|
|
var comment = '';
|
2015-02-07 15:20:04 +01:00
|
|
|
|
findDef = new RegExp('.*#define[ \\t].*/[/*]+[ \\t]*(.*)');
|
|
|
|
|
if (info.line.search(findDef) >= 0) {
|
|
|
|
|
comment = info.line.replace(findDef, '$1');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Get all the comments immediately before the item
|
|
|
|
|
var r, s;
|
2015-02-07 19:34:16 +01:00
|
|
|
|
findDef = new RegExp('(([ \\t]*(//|#)[^\n]+\n){1,4})([ \\t]*\n){0,1}' + info.line, 'g');
|
2015-02-07 15:20:04 +01:00
|
|
|
|
if (r = findDef.exec(txt)) {
|
2015-02-07 20:04:10 +01:00
|
|
|
|
findDef = new RegExp('^[ \\t]*//+[ \\t]*(.*)[ \\t]*$', 'gm');
|
2015-02-07 19:34:16 +01:00
|
|
|
|
while((s = findDef.exec(r[1])) !== null) {
|
2015-02-07 20:04:10 +01:00
|
|
|
|
if (s[1].match(/^#define[ \\t]/) != null) {
|
2015-02-07 19:34:16 +01:00
|
|
|
|
comment = '';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
comment += s[1] + "\n";
|
2015-02-07 15:20:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$.extend(info, {
|
|
|
|
|
comment: comment.trim(),
|
|
|
|
|
lineNum: this.getLineNumberOfText(info.line, txt)
|
|
|
|
|
});
|
2015-02-05 16:05:29 +01:00
|
|
|
|
}
|
2015-02-07 11:04:44 +01:00
|
|
|
|
else
|
|
|
|
|
info = null;
|
2015-02-06 04:30:11 +01:00
|
|
|
|
|
2015-02-07 15:20:04 +01:00
|
|
|
|
this.log(info,2);
|
|
|
|
|
|
2015-02-07 11:04:44 +01:00
|
|
|
|
return info;
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
2015-02-07 08:46:16 +01:00
|
|
|
|
/**
|
|
|
|
|
* Count the number of lines before a match, return -1 on fail
|
|
|
|
|
*/
|
2015-02-07 11:04:44 +01:00
|
|
|
|
getLineNumberOfText: function(line, txt) {
|
2015-02-07 02:57:31 +01:00
|
|
|
|
var pos = txt.indexOf(line);
|
2015-02-07 09:46:14 +01:00
|
|
|
|
return (pos < 0) ? pos : txt.substr(0, pos).lineCount();
|
2015-02-06 04:30:11 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
log: function(o,l) {
|
|
|
|
|
if (l === undefined) l = 0;
|
|
|
|
|
if (this.logging>=l*1) console.log(o);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
logOnce: function(o) {
|
2015-02-07 11:04:44 +01:00
|
|
|
|
if (o.didLogThisObject === undefined) {
|
2015-02-06 04:30:11 +01:00
|
|
|
|
this.log(o);
|
2015-02-05 16:05:29 +01:00
|
|
|
|
o.didLogThisObject = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
EOF: null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
// Typically the app would be in its own file, but this would be here
|
|
|
|
|
configuratorApp.init();
|
|
|
|
|
|
|
|
|
|
});
|