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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. // Return this anonymous object as configuratorApp
  60. return {
  61. my_public_var: 4,
  62. logging: 1,
  63. init: function() {
  64. self = this; // a 'this' for use when 'this' is something else
  65. // Set up the form
  66. this.initConfigForm();
  67. // Make tabs for the fieldsets
  68. var $fset = $('#config_form fieldset');
  69. var $tabs = $('<ul>',{class:'tabs'}), ind = 1;
  70. $('#config_form fieldset').each(function(){
  71. var tabID = 'TAB'+ind;
  72. $(this).addClass(tabID);
  73. var $leg = $(this).find('legend');
  74. var $link = $('<a>',{href:'#'+ind,id:tabID}).text($leg.text());
  75. $tabs.append($('<li>').append($link));
  76. $link.click(function(e){
  77. e.preventDefault;
  78. var ind = this.id;
  79. $tabs.find('.active').removeClass('active');
  80. $(this).addClass('active');
  81. $fset.hide();
  82. $fset.filter('.'+this.id).show();
  83. return false;
  84. });
  85. ind++;
  86. });
  87. $tabs.appendTo('#tabs');
  88. $('<br>',{class:'clear'}).appendTo('#tabs');
  89. $tabs.find('a:first').trigger('click');
  90. // Make a droppable file uploader, if possible
  91. var $uploader = $('#file-upload');
  92. var fileUploader = new BinaryFileUploader({
  93. element: $uploader[0],
  94. onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
  95. });
  96. if (!fileUploader.hasFileUploaderSupport())
  97. this.setMessage("Your browser doesn't support the file reading API.", 'error');
  98. // Read boards.h, Configuration.h, Configuration_adv.h
  99. var ajax_count = 0, success_count = 0;
  100. var loaded_items = {};
  101. var config_files = [boards_file, config_file, config_adv_file];
  102. $.each(config_files, function(i,fname){
  103. $.ajax({
  104. url: marlin_config+'/'+fname,
  105. type: 'GET',
  106. async: true,
  107. cache: false,
  108. success: function(txt) {
  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. if (inf.comment) {
  324. var $tipme = $elm.prev('label');
  325. if ($tipme.length) {
  326. $tipme.hover(
  327. function() {
  328. var pos = $tipme.position();
  329. $tooltip.text(inf.comment)
  330. .append('<span>')
  331. .css({bottom:($tooltip.parent().outerHeight()-pos.top)+'px',left:(pos.left+70)+'px'})
  332. .show();
  333. if (hover_timer) {
  334. clearTimeout(hover_timer);
  335. hover_timer = null;
  336. }
  337. },
  338. function() {
  339. hover_timer = setTimeout(function(){
  340. hover_timer = null;
  341. $tooltip.fadeOut(400);
  342. }, 400);
  343. }
  344. );
  345. }
  346. }
  347. }
  348. this.setFieldFromDefine(name);
  349. },
  350. /**
  351. * Handle any value field being changed
  352. */
  353. handleChange: function() { self.updateDefineFromField(this.id); },
  354. /**
  355. * Handle a switch checkbox being changed
  356. */
  357. handleSwitch: function() {
  358. var $elm = $(this), $prev = $elm.prev();
  359. var on = $elm.prop('checked') || false;
  360. $prev.attr('disabled', !on);
  361. self.setDefineEnabled($prev[0].id, on);
  362. },
  363. /**
  364. * Get the current value of a #define (from the config text)
  365. */
  366. defineValue: function(name) {
  367. this.log('defineValue:'+name,4);
  368. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  369. if (inf == null) return 'n/a';
  370. var result = inf.regex.exec($(inf.field).text());
  371. this.log(result,2);
  372. return inf.type == 'switch' ? result[inf.val_i] != '//' : result[inf.val_i];
  373. },
  374. /**
  375. * Get the current enabled state of a #define (from the config text)
  376. */
  377. defineIsEnabled: function(name) {
  378. this.log('defineIsEnabled:'+name,4);
  379. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  380. if (inf == null) return false;
  381. var result = inf.regex.exec($(inf.field).text());
  382. this.log(result,2);
  383. var on = result !== null ? result[1].trim() != '//' : true;
  384. this.log(name + ' = ' + on, 2);
  385. return on;
  386. },
  387. /**
  388. * Set a #define enabled or disabled by altering the config text
  389. */
  390. setDefineEnabled: function(name, val) {
  391. this.log('setDefineEnabled:'+name,4);
  392. var $elm = $('#'+name), inf = $elm[0].defineInfo;
  393. if (inf == null) return;
  394. var slash = val ? '' : '//';
  395. var newline = inf.line
  396. .replace(/^([ \t]*)(\/\/)([ \t]*)/, '$1$3') // remove slashes
  397. .replace(inf.pre+inf.define, inf.pre+slash+inf.define); // add them back
  398. this.setDefineLine(name, newline);
  399. },
  400. /**
  401. * Update a #define (from the form) by altering the config text
  402. */
  403. updateDefineFromField: function(name) {
  404. this.log('updateDefineFromField:'+name,4);
  405. var $elm = $('#'+name), inf = $elm[0].defineInfo;
  406. if (inf == null) return;
  407. var isCheck = $elm.attr('type') == 'checkbox',
  408. val = isCheck ? $elm.prop('checked') : $elm.val();
  409. var newline;
  410. switch(inf.type) {
  411. case 'switch':
  412. var slash = val ? '' : '//';
  413. newline = inf.line.replace(inf.repl, '$1'+slash+'$3');
  414. break;
  415. case 'quoted':
  416. case 'plain':
  417. if (isCheck)
  418. this.setMessage(name + ' should not be a checkbox!', 'error');
  419. else
  420. newline = inf.line.replace(inf.repl, '$1'+val.replace('$','\\$')+'$3');
  421. break;
  422. }
  423. this.setDefineLine(name, newline);
  424. },
  425. /**
  426. * Set the define's line in the text to a new line,
  427. * then update, highlight, and scroll to the line
  428. */
  429. setDefineLine: function(name, newline) {
  430. var $elm = $('#'+name), elm = $elm[0], inf = elm.defineInfo;
  431. var $c = $(inf.field), txt = $c.text();
  432. var hilite_token = '[HIGHLIGHTER-TOKEN]';
  433. txt = txt.replace(inf.line, hilite_token + newline);
  434. inf.line = newline;
  435. this.log(newline, 2);
  436. // Convert txt into HTML before storing
  437. var html = $('<div/>').text(txt).html().replace(hilite_token, '<span></span>');
  438. // Set the final text including the highlighter
  439. $c.html(html);
  440. // Scroll to reveal the define
  441. this.scrollToDefine(name);
  442. },
  443. /**
  444. * Scroll a pre box to reveal a #define
  445. */
  446. scrollToDefine: function(name, always) {
  447. this.log('scrollToDefine:'+name,4);
  448. var $elm = $('#'+name), inf = $elm[0].defineInfo, $c = $(inf.field);
  449. // Scroll to the altered text if it isn't visible
  450. var halfHeight = $c.height()/2, scrollHeight = $c.prop('scrollHeight'),
  451. textScrollY = inf.lineNum * scrollHeight/(inf.adv ? total_config_adv_lines : total_config_lines) - halfHeight;
  452. if (textScrollY < 0)
  453. textScrollY = 0;
  454. else if (textScrollY > scrollHeight)
  455. textScrollY = scrollHeight - 1;
  456. if (always == true || Math.abs($c.prop('scrollTop') - textScrollY) > halfHeight)
  457. $c.animate({ scrollTop: textScrollY < 0 ? 0 : textScrollY });
  458. },
  459. /**
  460. * Set a form field to the current #define value in the config text
  461. */
  462. setFieldFromDefine: function(name) {
  463. var $elm = $('#'+name), val = this.defineValue(name);
  464. this.log('setFieldFromDefine:' + name + ' to ' + val, 4);
  465. // Set the field value
  466. $elm.attr('type') == 'checkbox' ? $elm.prop('checked', val) : $elm.val(''+val);
  467. // If the item has a checkbox then set enabled state too
  468. var $cb = $('#'+name+'-switch');
  469. if ($cb.length) {
  470. var on = self.defineIsEnabled(name);
  471. $elm.attr('disabled', !on); // enable/disable the form field (could also dim it)
  472. $cb.prop('checked', on); // check/uncheck the checkbox
  473. }
  474. },
  475. /**
  476. * Purge #define information for one of the config files
  477. */
  478. purgeDefineInfo: function(adv) {
  479. if (adv === undefined) adv = false;
  480. $('[name]').each(function() {
  481. var inf = this.defineInfo;
  482. if (inf && adv === inf.adv) $(this).removeProp('defineInfo');
  483. });
  484. },
  485. /**
  486. * Update #define information for one of the config files
  487. */
  488. refreshDefineInfo: function(adv) {
  489. if (adv === undefined) adv = false;
  490. $('[name]').each(function() {
  491. var inf = this.defineInfo;
  492. if (inf && adv == inf.adv) this.defineInfo = self.getDefineInfo(this.id, adv);
  493. });
  494. },
  495. /**
  496. * Get information about a #define from configuration file text:
  497. *
  498. * Pre-examine the #define for its prefix, value position, suffix, etc.
  499. * Construct a regex for the #define to quickly find (and replace) values.
  500. * Store the existing #define line as the key to finding it later.
  501. * Determine the line number of the #define so it can be scrolled to.
  502. */
  503. getDefineInfo: function(name, adv) {
  504. if (adv === undefined) adv = false;
  505. this.log('getDefineInfo:'+name,4);
  506. var $elm = $('#'+name), elm = $elm[0],
  507. $c = adv ? $config_adv : $config,
  508. txt = $c.text();
  509. // a switch line with no value
  510. var findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm'),
  511. result = findDef.exec(txt),
  512. info = { type:0, adv:adv, field:$c[0], val_i: 2 };
  513. if (result !== null) {
  514. $.extend(info, {
  515. val_i: 1,
  516. type: 'switch',
  517. line: result[0], // whole line
  518. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  519. define: result[2],
  520. post: result[3] === undefined ? '' : result[3]
  521. });
  522. info.regex = new RegExp('([ \\t]*//)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
  523. info.repl = new RegExp('([ \\t]*)(\/\/)?([ \\t]*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
  524. }
  525. else {
  526. // a define with quotes
  527. findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
  528. result = findDef.exec(txt);
  529. if (result !== null) {
  530. $.extend(info, {
  531. type: 'quoted',
  532. line: result[0],
  533. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  534. define: result[2],
  535. post: result[4] === undefined ? '' : result[4]
  536. });
  537. info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
  538. info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
  539. }
  540. else {
  541. // a define with no quotes
  542. findDef = new RegExp('^([ \\t]*//)?([ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
  543. result = findDef.exec(txt);
  544. if (result !== null) {
  545. $.extend(info, {
  546. type: 'plain',
  547. line: result[0],
  548. pre: result[1] === undefined ? '' : result[1].replace('//',''),
  549. define: result[2],
  550. post: result[4] === undefined ? '' : result[4]
  551. });
  552. info.regex = new RegExp('([ \\t]*//)?[ \\t]*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
  553. info.repl = new RegExp('(([ \\t]*//)?[ \\t]*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
  554. }
  555. }
  556. }
  557. if (info.type) {
  558. // Get the end-of-line comment, if there is one
  559. var comment = '';
  560. findDef = new RegExp('.*#define[ \\t].*/[/*]+[ \\t]*(.*)');
  561. if (info.line.search(findDef) >= 0) {
  562. comment = info.line.replace(findDef, '$1');
  563. }
  564. else {
  565. // Get all the comments immediately before the item
  566. var r, s;
  567. findDef = new RegExp('(([ \\t]*(//|#)[^\n]+\n){1,4})([ \\t]*\n){0,1}' + info.line, 'g');
  568. if (r = findDef.exec(txt)) {
  569. findDef = new RegExp('^[ \\t]*(//+[ \\t]*.*)[ \\t]*$', 'gm');
  570. while((s = findDef.exec(r[1])) !== null) {
  571. if (s[1].match(/^\/\/[ \\t]*#define[ \\t]/) != null) {
  572. comment = '';
  573. break;
  574. }
  575. comment += s[1] + "\n";
  576. }
  577. }
  578. }
  579. $.extend(info, {
  580. comment: comment.trim(),
  581. lineNum: this.getLineNumberOfText(info.line, txt)
  582. });
  583. }
  584. else
  585. info = null;
  586. this.log(info,2);
  587. return info;
  588. },
  589. /**
  590. * Count the number of lines before a match, return -1 on fail
  591. */
  592. getLineNumberOfText: function(line, txt) {
  593. var pos = txt.indexOf(line);
  594. return (pos < 0) ? pos : txt.substr(0, pos).lineCount();
  595. },
  596. log: function(o,l) {
  597. if (l === undefined) l = 0;
  598. if (this.logging>=l*1) console.log(o);
  599. },
  600. logOnce: function(o) {
  601. if (o.didLogThisObject === undefined) {
  602. this.log(o);
  603. o.didLogThisObject = true;
  604. }
  605. },
  606. EOF: null
  607. };
  608. })();
  609. // Typically the app would be in its own file, but this would be here
  610. configuratorApp.init();
  611. });