My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configurator.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * configurator.js
  3. *
  4. * Marlin Configuration Utility
  5. * - Web form for entering configuration options
  6. * - A reprap calculator to calculate movement values
  7. * - Uses HTML5 to generate downloadables in Javascript
  8. * - Reads and parses standard configuration files from local folders
  9. *
  10. * Supporting functions
  11. * - Parser to read Marlin Configuration.h and Configuration_adv.h files
  12. * - Utilities to replace values in configuration files
  13. */
  14. "use strict";
  15. $(function(){
  16. var marlin_config = 'config';
  17. // Extend String
  18. String.prototype.lpad = function(len, chr) {
  19. if (chr === undefined) { chr = ' '; }
  20. var s = this+'', need = len - s.length;
  21. if (need > 0) { s = new Array(need+1).join(chr) + s; }
  22. return s;
  23. };
  24. String.prototype.prePad = function(len, chr) { return len ? this.lpad(len, chr) : this; };
  25. String.prototype.zeroPad = function(len) { return this.prePad(len, '0'); };
  26. String.prototype.regEsc = function() { return this.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"); }
  27. String.prototype.lineCount = function() { var len = this.split(/\r?\n|\r/).length; return len > 0 ? len - 1 : len; };
  28. /**
  29. * selectField.addOptions takes an array or keyed object
  30. */
  31. $.fn.extend({
  32. addOptions: function(arrObj) {
  33. return this.each(function() {
  34. var sel = $(this);
  35. var isArr = Object.prototype.toString.call(arrObj) == "[object Array]";
  36. $.each(arrObj, function(k, v) {
  37. sel.append( $('<option>',{value:isArr?v:k}).text(v) );
  38. });
  39. });
  40. }
  41. });
  42. // The app is a singleton
  43. var configuratorApp = (function(){
  44. // private variables and functions go here
  45. var self,
  46. pi2 = Math.PI * 2,
  47. has_boards = false, has_config = false, has_config_adv = false,
  48. boards_file = 'boards.h',
  49. config_file = 'Configuration.h',
  50. config_adv_file = 'Configuration_adv.h',
  51. $config = $('#config_text'),
  52. $config_adv = $('#config_adv_text'),
  53. boards_list = {},
  54. therms_list = {},
  55. total_config_lines,
  56. total_config_adv_lines;
  57. // Return this anonymous object as configuratorApp
  58. return {
  59. my_public_var: 4,
  60. logging: 1,
  61. init: function() {
  62. self = this; // a 'this' for use when 'this' is something else
  63. // Set up the form
  64. this.initConfigForm();
  65. // Make tabs for the fieldsets
  66. var $fset = $('#config_form fieldset');
  67. var $tabs = $('<ul>',{class:'tabs'}), ind = 1;
  68. $('#config_form fieldset').each(function(){
  69. var tabID = 'TAB'+ind;
  70. $(this).addClass(tabID);
  71. var $leg = $(this).find('legend');
  72. var $link = $('<a>',{href:'#'+ind,id:tabID}).text($leg.text());
  73. $tabs.append($('<li>').append($link));
  74. $link.click(function(e){
  75. e.preventDefault;
  76. var ind = this.id;
  77. $tabs.find('.active').removeClass('active');
  78. $(this).addClass('active');
  79. $fset.hide();
  80. $fset.filter('.'+this.id).show();
  81. return false;
  82. });
  83. ind++;
  84. });
  85. $tabs.appendTo('#tabs');
  86. $('<br>',{class:'clear'}).appendTo('#tabs');
  87. $tabs.find('a:first').trigger('click');
  88. // Make a droppable file uploader, if possible
  89. var $uploader = $('#file-upload');
  90. var fileUploader = new BinaryFileUploader({
  91. element: $uploader[0],
  92. onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
  93. });
  94. if (!fileUploader.hasFileUploaderSupport())
  95. this.setMessage("Your browser doesn't support the file reading API.", 'error');
  96. // Read boards.h, Configuration.h, Configuration_adv.h
  97. var ajax_count = 0, success_count = 0;
  98. var loaded_items = {};
  99. var config_files = [boards_file, config_file, config_adv_file];
  100. $.each(config_files, function(i,fname){
  101. self.log("Loading " + fname + "...", 3);
  102. $.ajax({
  103. url: marlin_config+'/'+fname,
  104. type: 'GET',
  105. async: true,
  106. cache: false,
  107. success: function(txt) {
  108. self.log("Loaded " + fname + "...", 3);
  109. loaded_items[fname] = function(){ self.fileLoaded(fname, txt); };
  110. success_count++;
  111. },
  112. complete: function() {
  113. ajax_count++;
  114. if (ajax_count >= 3) {
  115. $.each(config_files, function(i,fname){ if (loaded_items[fname] !== undefined) loaded_items[fname](); });
  116. self.refreshConfigForm();
  117. if (success_count < ajax_count)
  118. self.setMessage('Unable to load configurations. Use the upload field instead.', 'error');
  119. }
  120. }
  121. });
  122. });
  123. },
  124. setMessage: function(msg,type) {
  125. if (msg) {
  126. if (type === undefined) type = 'message';
  127. var $err = $('<p class="'+type+'">'+msg+'</p>'), err = $err[0];
  128. $('#message').prepend($err);
  129. var baseColor = $err.css('color').replace(/rgba?\(([^),]+,[^),]+,[^),]+).*/, 'rgba($1,');
  130. var d = new Date();
  131. err.startTime = d.getTime();
  132. err.pulser = setInterval(function(){
  133. d = new Date();
  134. var pulse_time = (d.getTime() - err.startTime);
  135. $err.css({color:baseColor+(0.5+Math.sin(pulse_time/200)*0.4)+')'});
  136. if (pulse_time > 5000) {
  137. clearInterval(err.pulser);
  138. $err.remove();
  139. }
  140. }, 50);
  141. }
  142. else {
  143. $('#message p.error, #message p.warning').each(function() {
  144. if (this.pulser !== undefined && this.pulser)
  145. clearInterval(this.pulser);
  146. $(this).remove();
  147. });
  148. }
  149. },
  150. /**
  151. * Init the boards array from a boards.h file
  152. */
  153. initBoardsFromText: function(txt) {
  154. boards_list = {};
  155. var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[\\w_]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
  156. while((r = findDef.exec(txt)) !== null) {
  157. boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')');
  158. }
  159. this.log("Loaded boards", 3); this.log(boards_list, 3);
  160. has_boards = true;
  161. },
  162. /**
  163. * Init the thermistors array from the Configuration.h file
  164. */
  165. initThermistorsFromText: function(txt) {
  166. // Get all the thermistors and save them into an object
  167. var r, s, findDef = new RegExp('(//.*\n)+\\s+(#define[ \\t]+TEMP_SENSOR_0)', 'g');
  168. r = findDef.exec(txt);
  169. findDef = new RegExp('^//[ \\t]*([-\\d]+)[ \\t]+is[ \\t]+(.*)[ \\t]*$', 'gm');
  170. while((s = findDef.exec(r[0])) !== null) {
  171. therms_list[s[1]] = s[1].prePad(4, '  ') + " — " + s[2];
  172. }
  173. },
  174. /**
  175. * Handle a file being dropped on the file field
  176. */
  177. handleFileLoad: function(txt, $uploader) {
  178. txt += '';
  179. var filename = $uploader.val().replace(/.*[\/\\](.*)$/, '$1');
  180. switch(filename) {
  181. case boards_file:
  182. case config_file:
  183. case config_adv_file:
  184. this.fileLoaded(filename, txt);
  185. break;
  186. default:
  187. this.log("Can't parse "+filename, 1);
  188. break;
  189. }
  190. },
  191. /**
  192. * Process a file after it's been successfully loaded
  193. */
  194. fileLoaded: function(filename, txt) {
  195. this.log("fileLoaded:"+filename,4);
  196. switch(filename) {
  197. case boards_file:
  198. this.initBoardsFromText(txt);
  199. $('#MOTHERBOARD').html('').addOptions(boards_list);
  200. if (has_config) this.initField('MOTHERBOARD');
  201. this.setMessage(boards_file+' loaded successfully.');
  202. break;
  203. case config_file:
  204. if (has_boards) {
  205. $config.text(txt);
  206. total_config_lines = txt.lineCount();
  207. this.initThermistorsFromText(txt);
  208. this.purgeDefineInfo(false);
  209. this.refreshConfigForm();
  210. this.setMessage(config_file+' loaded successfully.');
  211. has_config = true;
  212. }
  213. else {
  214. this.setMessage("Upload a " + boards_file + " file first!", 'error');
  215. }
  216. break;
  217. case config_adv_file:
  218. if (has_config) {
  219. $config_adv.text(txt);
  220. total_config_adv_lines = txt.lineCount();
  221. this.purgeDefineInfo(true);
  222. this.refreshConfigForm();
  223. this.setMessage(config_adv_file+' loaded successfully.');
  224. has_config_adv = true;
  225. }
  226. else {
  227. this.setMessage("Upload a " + config_file + " file first!", 'error');
  228. }
  229. break;
  230. }
  231. },
  232. /**
  233. * Add enhancements to the form
  234. */
  235. initConfigForm: function() {
  236. // Modify form fields and make the form responsive.
  237. // As values change on the form, we could update the
  238. // contents of text areas containing the configs, for
  239. // example.
  240. // while(!$config_adv.text() == null) {}
  241. // while(!$config.text() == null) {}
  242. // Go through all form items with names
  243. $('#config_form').find('[name]').each(function() {
  244. // Set its id to its name
  245. var name = $(this).attr('name');
  246. $(this).attr({id: name});
  247. // Attach its label sibling
  248. var $label = $(this).prev('label');
  249. if ($label.length) $label.attr('for',name);
  250. });
  251. // Get all 'switchable' class items and add a checkbox
  252. $('#config_form .switchable').each(function(){
  253. $(this).after(
  254. $('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
  255. .attr('id',this.id + '-switch')
  256. .change(self.handleSwitch)
  257. );
  258. });
  259. // Add options to the popup menus
  260. $('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
  261. $('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
  262. $('#EXTRUDERS').addOptions([1,2,3,4]);
  263. $('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
  264. // Replace the Serial popup menu with a stepper control
  265. $('#serial_stepper').jstepper({
  266. min: 0,
  267. max: 3,
  268. val: $('#SERIAL_PORT').val(),
  269. arrowWidth: '18px',
  270. arrowHeight: '15px',
  271. color: '#FFF',
  272. acolor: '#F70',
  273. hcolor: '#FF0',
  274. id: 'select-me',
  275. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  276. onChange: function(v) { $('#SERIAL_PORT').val(v).trigger('change'); }
  277. });
  278. },
  279. /**
  280. * Update all fields on the form after loading a configuration
  281. */
  282. refreshConfigForm: function() {
  283. /**
  284. * For now I'm manually creating these references
  285. * but I should be able to parse Configuration.h
  286. * and iterate the #defines.
  287. *
  288. * For any #ifdef blocks I can create field groups
  289. * which can be dimmed together when the option
  290. * is disabled.
  291. *
  292. * Then we only need to specify exceptions to
  293. * standard behavior, (which is to add a text field)
  294. */
  295. this.initField('SERIAL_PORT');
  296. this.initField('BAUDRATE');
  297. this.initField('BTENABLED');
  298. $('#MOTHERBOARD').html('').addOptions(boards_list);
  299. this.initField('MOTHERBOARD');
  300. this.initField('CUSTOM_MENDEL_NAME');
  301. this.initField('MACHINE_UUID');
  302. this.initField('EXTRUDERS');
  303. this.initField('POWER_SUPPLY');
  304. this.initField('PS_DEFAULT_OFF');
  305. $('#TEMP_SENSOR_0, #TEMP_SENSOR_1, #TEMP_SENSOR_2, #TEMP_SENSOR_BED').html('').addOptions(therms_list);
  306. this.initField('TEMP_SENSOR_0');
  307. this.initField('TEMP_SENSOR_1');
  308. this.initField('TEMP_SENSOR_2');
  309. this.initField('TEMP_SENSOR_BED');
  310. this.initField('TEMP_SENSOR_1_AS_REDUNDANT');
  311. this.initField('MAX_REDUNDANT_TEMP_SENSOR_DIFF');
  312. this.initField('TEMP_RESIDENCY_TIME');
  313. },
  314. /**
  315. * Make a field responsive and initialize its defineInfo
  316. */
  317. initField: function(name, adv) {
  318. this.log("initField:"+name,4);
  319. var $elm = $('#'+name), elm = $elm[0];
  320. if (elm.defineInfo == null) {
  321. var inf = elm.defineInfo = this.getDefineInfo(name, adv);
  322. $elm.on($elm.attr('type') == 'text' ? 'input' : 'change', this.handleChange);
  323. var comm = inf.comment;
  324. var $tipme = $elm.prev('label');
  325. if ($tipme.length) {
  326. comm ?
  327. $tipme.addClass('tooltip').attr('data-tooltip',comm) :
  328. $tipme.removeClass('tooltip').removeAttr('data-tooltip');
  329. }
  330. }
  331. this.setFieldFromDefine(name);
  332. },
  333. /**
  334. * Handle any value field being changed
  335. */
  336. handleChange: function() { self.updateDefineFromField(this.id); },
  337. /**
  338. * Handle a switch checkbox being changed
  339. */
  340. handleSwitch: function() {
  341. var $elm = $(this), $prev = $elm.prev();
  342. var on = $elm.prop('checked') || false;
  343. $prev.attr('disabled', !on);
  344. self.setDefineEnabled($prev[0].id, on);
  345. },
  346. /**
  347. * Get the current value of a #define (from the config text)
  348. */
  349. defineValue: function(name) {
  350. this.log('defineValue:'+name,4);
  351. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  352. if (inf == null) return 'n/a';
  353. var result = inf.regex.exec($(inf.field).text());
  354. this.log(result,2);
  355. return inf.type == 'switch' ? result[inf.val_i] != '//' : result[inf.val_i];
  356. },
  357. /**
  358. * Get the current enabled state of a #define (from the config text)
  359. */
  360. defineIsEnabled: function(name) {
  361. this.log('defineIsEnabled:'+name,4);
  362. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  363. if (inf == null) return false;
  364. var result = inf.regex.exec($(inf.field).text());
  365. this.log(result,2);
  366. var on = result !== null ? result[1].trim() != '//' : true;
  367. this.log(name + ' = ' + on, 2);
  368. return on;
  369. },
  370. /**
  371. * Set a #define enabled or disabled by altering the config text
  372. */
  373. setDefineEnabled: function(name, val) {
  374. this.log('setDefineEnabled:'+name,4);
  375. var $elm = $('#'+name), inf = $elm[0].defineInfo;
  376. if (inf == null) return;
  377. var slash = val ? '' : '//';
  378. var newline = inf.line
  379. .replace(/^([ \t]*)(\/\/)([ \t]*)/, '$1$3') // remove slashes
  380. .replace(inf.pre+inf.define, inf.pre+slash+inf.define); // add them back
  381. this.setDefineLine(name, newline);
  382. },
  383. /**
  384. * Update a #define (from the form) by altering the config text
  385. */
  386. updateDefineFromField: function(name) {
  387. this.log('updateDefineFromField:'+name,4);
  388. var $elm = $('#'+name), inf = $elm[0].defineInfo;
  389. if (inf == null) return;
  390. var isCheck = $elm.attr('type') == 'checkbox',
  391. val = isCheck ? $elm.prop('checked') : $elm.val();
  392. var newline;
  393. switch(inf.type) {
  394. case 'switch':
  395. var slash = val ? '' : '//';
  396. newline = inf.line.replace(inf.repl, '$1'+slash+'$3');
  397. break;
  398. case 'quoted':
  399. case 'plain':
  400. if (isCheck)
  401. this.setMessage(name + ' should not be a checkbox!', 'error');
  402. else
  403. newline = inf.line.replace(inf.repl, '$1'+val.replace('$','\\$')+'$3');
  404. break;
  405. }
  406. this.setDefineLine(name, newline);
  407. },
  408. /**
  409. * Set the define's line in the text to a new line,
  410. * then update, highlight, and scroll to the line
  411. */
  412. setDefineLine: function(name, newline) {
  413. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  414. var $c = $(inf.field), txt = $c.text();
  415. var hilite_token = '[HIGHLIGHTER-TOKEN]';
  416. txt = txt.replace(inf.line, hilite_token + newline);
  417. inf.line = newline;
  418. this.log(newline, 2);
  419. // Convert txt into HTML before storing
  420. var html = $('<div/>').text(txt).html().replace(hilite_token, '<span></span>');
  421. // Set the final text including the highlighter
  422. $c.html(html);
  423. // Scroll to reveal the define
  424. this.scrollToDefine(name);
  425. },
  426. /**
  427. * Scroll a pre box to reveal a #define
  428. */
  429. scrollToDefine: function(name, always) {
  430. this.log('scrollToDefine:'+name,4);
  431. var $elm = $('#'+name), inf = $elm[0].defineInfo, $c = $(inf.field);
  432. // Scroll to the altered text if it isn't visible
  433. var halfHeight = $c.height()/2, scrollHeight = $c.prop('scrollHeight'),
  434. textScrollY = inf.lineNum * scrollHeight/(inf.adv ? total_config_adv_lines : total_config_lines) - halfHeight;
  435. if (textScrollY < 0)
  436. textScrollY = 0;
  437. else if (textScrollY > scrollHeight)
  438. textScrollY = scrollHeight - 1;
  439. if (always == true || Math.abs($c.prop('scrollTop') - textScrollY) > halfHeight)
  440. $c.animate({ scrollTop: textScrollY < 0 ? 0 : textScrollY });
  441. },
  442. /**
  443. * Set a form field to the current #define value in the config text
  444. */
  445. setFieldFromDefine: function(name) {
  446. var $elm = $('#'+name), val = this.defineValue(name);
  447. this.log('setFieldFromDefine:' + name + ' to ' + val, 4);
  448. // Set the field value
  449. $elm.attr('type') == 'checkbox' ? $elm.prop('checked', val) : $elm.val(''+val);
  450. // If the item has a checkbox then set enabled state too
  451. var $cb = $('#'+name+'-switch');
  452. if ($cb.length) {
  453. var on = self.defineIsEnabled(name);
  454. $elm.attr('disabled', !on); // enable/disable the form field (could also dim it)
  455. $cb.prop('checked', on); // check/uncheck the checkbox
  456. }
  457. },
  458. /**
  459. * Purge #define information for one of the config files
  460. */
  461. purgeDefineInfo: function(adv) {
  462. if (adv === undefined) adv = false;
  463. $('[name]').each(function() {
  464. var inf = this.defineInfo;
  465. if (inf && adv === inf.adv) $(this).removeProp('defineInfo');
  466. });
  467. },
  468. /**
  469. * Update #define information for one of the config files
  470. */
  471. refreshDefineInfo: function(adv) {
  472. if (adv === undefined) adv = false;
  473. $('[name]').each(function() {
  474. var inf = this.defineInfo;
  475. if (inf && adv == inf.adv) this.defineInfo = self.getDefineInfo(this.id, adv);
  476. });
  477. },
  478. /**
  479. * Get information about a #define from configuration file text:
  480. *
  481. * Pre-examine the #define for its prefix, value position, suffix, etc.
  482. * Construct a regex for the #define to quickly find (and replace) values.
  483. * Store the existing #define line as the key to finding it later.
  484. * Determine the line number of the #define so it can be scrolled to.
  485. */
  486. getDefineInfo: function(name, adv) {
  487. if (adv === undefined) adv = false;
  488. this.log('getDefineInfo:'+name,4);
  489. var $elm = $('#'+name), elm = $elm[0],
  490. $c = adv ? $config_adv : $config,
  491. txt = $c.text();
  492. // a switch line with no value
  493. var findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm'),
  494. result = findDef.exec(txt),
  495. info = { type:0, adv:adv, field:$c[0], val_i: 2 };
  496. if (result !== null) {
  497. $.extend(info, {
  498. val_i: 1,
  499. type: 'switch',
  500. line: result[0], // whole line
  501. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  502. define: result[2],
  503. post: result[3] === undefined ? '' : result[3]
  504. });
  505. info.regex = new RegExp('([ \\t]*//)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
  506. info.repl = new RegExp('([ \\t]*)(\/\/)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
  507. }
  508. else {
  509. // a define with quotes
  510. findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
  511. result = findDef.exec(txt);
  512. if (result !== null) {
  513. $.extend(info, {
  514. type: 'quoted',
  515. line: result[0],
  516. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  517. define: result[2],
  518. post: result[4] === undefined ? '' : result[4]
  519. });
  520. info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
  521. info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
  522. }
  523. else {
  524. // a define with no quotes
  525. findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
  526. result = findDef.exec(txt);
  527. if (result !== null) {
  528. $.extend(info, {
  529. type: 'plain',
  530. line: result[0],
  531. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  532. define: result[2],
  533. post: result[4] === undefined ? '' : result[4]
  534. });
  535. info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
  536. info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
  537. }
  538. }
  539. }
  540. if (info.type) {
  541. var comment = '';
  542. // Get the end-of-line comment, if there is one
  543. findDef = new RegExp('.*#define[ \\t].*/[/*]+[ \\t]*(.*)');
  544. if (info.line.search(findDef) >= 0) {
  545. comment = info.line.replace(findDef, '$1');
  546. }
  547. else {
  548. // Get all the comments immediately before the item
  549. var r, s;
  550. findDef = new RegExp('([ \\t]*(//|#)[^\n]+\n){1,4}\\s{0,1}' + info.line, 'g');
  551. if (r = findDef.exec(txt)) {
  552. findDef = new RegExp('^[ \\t]*//+[ \\t]*(.*)[ \\t]*$', 'gm');
  553. while((s = findDef.exec(r[0])) !== null) {
  554. if (s[1].match(/\/\/[ \\t]*#define/) == null)
  555. comment += s[1] + "\n";
  556. }
  557. }
  558. }
  559. $.extend(info, {
  560. comment: comment.trim(),
  561. lineNum: this.getLineNumberOfText(info.line, txt)
  562. });
  563. }
  564. else
  565. info = null;
  566. this.log(info,2);
  567. return info;
  568. },
  569. /**
  570. * Count the number of lines before a match, return -1 on fail
  571. */
  572. getLineNumberOfText: function(line, txt) {
  573. var pos = txt.indexOf(line);
  574. return (pos < 0) ? pos : txt.substr(0, pos).lineCount();
  575. },
  576. log: function(o,l) {
  577. if (l === undefined) l = 0;
  578. if (this.logging>=l*1) console.log(o);
  579. },
  580. logOnce: function(o) {
  581. if (o.didLogThisObject === undefined) {
  582. this.log(o);
  583. o.didLogThisObject = true;
  584. }
  585. },
  586. EOF: null
  587. };
  588. })();
  589. // Typically the app would be in its own file, but this would be here
  590. configuratorApp.init();
  591. });