Use info.repl in updateDefineFromField, fix regex

This commit is contained in:
Scott Lahteine 2015-02-07 02:04:44 -08:00
parent 12a3975341
commit 2647402095
3 changed files with 63 additions and 74 deletions

View File

@ -6,8 +6,8 @@ body { margin: 0; padding: 0; background: #56A; color: #FFC; font-family: sans-s
#main { max-width: 1000px; margin: 0 auto; }
#main { padding: 0 4%; width: 92%; }
#main { font-family: monospace; }
#message { width: 80%; margin: 0 auto 0.25em; color: #FF0; text-align: center; }
h1, #message { text-align: center; }
#message { width: 80%; margin: 0 auto 0.25em; color: #FF0; }
#message p { padding: 2px 0; }
#message p.error, #message p.message { color: #F00; background: #FF4; font-weight: bold; border-radius: 0.8em; }
#message p.message { color: #080; background: #CFC; }

View File

@ -13,7 +13,7 @@
</head>
<body>
<section id="main">
<h1>Marlin Configurator 0.1a</h1>
<h1>Marlin Configurator</h1>
<div id="message">
<p class="info">Enter values in the form, get a Marlin configuration.<br/>Will include a drop-down of known configurations.</p>

View File

@ -125,7 +125,7 @@ var configuratorApp = (function(){
complete: function() {
ajax_count++;
if (ajax_count >= 3) {
$.each(config_files, function(i,fname){ if (typeof loaded_items[fname] != 'undefined') loaded_items[fname](); });
$.each(config_files, function(i,fname){ if (loaded_items[fname] !== undefined) loaded_items[fname](); });
self.refreshConfigForm();
if (success_count < ajax_count)
self.setMessage('Unable to load configurations. Use the upload field instead.', 'error');
@ -137,7 +137,7 @@ var configuratorApp = (function(){
setMessage: function(msg,type) {
if (msg) {
if (typeof type == 'undefined') type = 'message';
if (type === undefined) type = 'message';
var $err = $('<p class="'+type+'">'+msg+'</p>'), err = $err[0];
$('#message').prepend($err);
var baseColor = $err.css('color').replace(/rgba?\(([^),]+,[^),]+,[^),]+).*/, 'rgba($1,');
@ -155,7 +155,7 @@ var configuratorApp = (function(){
}
else {
$('#message p.error, #message p.warning').each(function() {
if (typeof this.pulser != 'undefined' && this.pulser)
if (this.pulser !== undefined && this.pulser)
clearInterval(this.pulser);
$(this).remove();
});
@ -440,30 +440,16 @@ var configuratorApp = (function(){
switch(inf.type) {
case 'switch':
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + inf.post);
newline = inf.line.replace(inf.repl, '$1'+slash+'$3');
break;
case 'quoted':
if (isCheck) {
this.log(name + ' should not be a checkbox', 1);
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + '"'+val+'"' + inf.post);
}
else {
newline = inf.pre + inf.define + '"'+val+'"' + inf.post;
}
break;
case 'plain':
if (isCheck) {
this.log(name + ' should not be a checkbox', 1);
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + val + inf.post);
}
else {
newline = inf.pre + inf.define + val + inf.post;
}
if (isCheck)
this.setMessage(name + ' should not be a checkbox!', 'error');
else
newline = inf.line.replace(inf.repl, '$1'+val.replace('$','\\$')+'$3');
break;
}
this.setDefineLine(name, newline);
},
@ -536,7 +522,7 @@ var configuratorApp = (function(){
* Purge #define information for one of the config files
*/
purgeDefineInfo: function(adv) {
if (typeof adv == 'undefined') adv = false;
if (adv === undefined) adv = false;
$('[defineInfo]').each(function() {
if (adv === this.defineInfo.adv) $(this).removeProp('defineInfo');
});
@ -546,7 +532,7 @@ var configuratorApp = (function(){
* Update #define information for one of the config files
*/
refreshDefineInfo: function(adv) {
if (typeof adv == 'undefined') adv = false;
if (adv === undefined) adv = false;
$('[defineInfo]').each(function() {
if (adv == this.defineInfo.adv) this.defineInfo = self.getDefineInfo(this.id, adv);
});
@ -561,7 +547,7 @@ var configuratorApp = (function(){
* Determine the line number of the #define so it can be scrolled to.
*/
getDefineInfo: function(name, adv) {
if (typeof adv == 'undefined') adv = false;
if (adv === undefined) adv = false;
this.log('getDefineInfo:'+name,4);
var $elm = $('#'+name), elm = $elm[0];
var $c = adv ? $config_adv : $config;
@ -569,63 +555,66 @@ var configuratorApp = (function(){
// a switch line with no value
var findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm');
var result = findDef.exec($c.text());
var info = { type:0, adv:adv, field:$c[0], val_i: 2 };
if (result !== null) {
var info = {
type:'switch', adv:adv, field:$c[0], val_i: 1,
line: result[0], // whole line
pre: result[1] === undefined ? '' : result[1].replace('//',''),
$.extend(info, {
val_i: 1,
type: 'switch',
line: result[0], // whole line
pre: result[1] === undefined ? '' : result[1].replace('//',''),
define: result[2],
post: result[3] === undefined ? '' : result[3]
};
info.repl = info.regex = new RegExp('(.*//)?(.*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
info.lineNum = this.getLineInText(info.line, $c.text());
this.log(info,2);
return info;
post: result[3] === undefined ? '' : result[3]
});
info.regex = new RegExp('( *//)?( *' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
info.repl = new RegExp('( *)(\/\/)?( *' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
}
else {
// a define with quotes
findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
result = findDef.exec($c.text());
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]
});
info.regex = new RegExp('( *//)? *' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
info.repl = new RegExp('(( *//)? *' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
}
else {
// a define with no quotes
findDef = new RegExp('^( *//)?( *#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
result = findDef.exec($c.text());
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]
});
info.regex = new RegExp('( *//)? *' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
info.repl = new RegExp('(( *//)? *' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
}
}
}
// a define with quotes
findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
result = findDef.exec($c.text());
if (result !== null) {
var info = {
type:'quoted', adv:adv, field:$c[0], val_i: 2,
line: result[0],
pre: result[1] === undefined ? '' : result[1].replace('//',''),
define: result[2],
post: result[4] === undefined ? '' : result[4]
};
info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
info.lineNum = this.getLineInText(info.line, $c.text());
if (info.type) {
info.lineNum = this.getLineNumberOfText(info.line, $c.text());
this.log(info,2);
return info;
}
else
info = null;
// a define with no quotes
findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
result = findDef.exec($c.text());
if (result !== null) {
var info = {
type:'plain', adv:adv, field:$c[0], val_i: 2,
line: result[0],
pre: result[1] === undefined ? '' : result[1].replace('//',''),
define: result[2],
post: result[4] === undefined ? '' : result[4]
};
info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
info.lineNum = this.getLineInText(info.line, $c.text());
this.log(info,2);
return info;
}
return null;
return info;
},
/**
* Count the number of lines before a match, return -1 on fail
*/
getLineInText: function(line, txt) {
getLineNumberOfText: function(line, txt) {
var pos = txt.indexOf(line);
return (pos < 0) ? pos : txt.substr(0, pos).lineCount();
},
@ -636,7 +625,7 @@ var configuratorApp = (function(){
},
logOnce: function(o) {
if (typeof o.didLogThisObject === 'undefined') {
if (o.didLogThisObject === undefined) {
this.log(o);
o.didLogThisObject = true;
}