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 23KB

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