My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

configurator.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. hover_timer;
  58. // Return this anonymous object as configuratorApp
  59. return {
  60. my_public_var: 4,
  61. logging: 1,
  62. init: function() {
  63. self = this; // a 'this' for use when 'this' is something else
  64. // Set up the form
  65. this.initConfigForm();
  66. // Make tabs for the fieldsets
  67. var $fset = $('#config_form fieldset');
  68. var $tabs = $('<ul>',{class:'tabs'}), ind = 1;
  69. $('#config_form fieldset').each(function(){
  70. var tabID = 'TAB'+ind;
  71. $(this).addClass(tabID);
  72. var $leg = $(this).find('legend');
  73. var $link = $('<a>',{href:'#'+ind,id:tabID}).text($leg.text());
  74. $tabs.append($('<li>').append($link));
  75. $link.click(function(e){
  76. e.preventDefault;
  77. var ind = this.id;
  78. $tabs.find('.active').removeClass('active');
  79. $(this).addClass('active');
  80. $fset.hide();
  81. $fset.filter('.'+this.id).show();
  82. return false;
  83. });
  84. ind++;
  85. });
  86. $tabs.appendTo('#tabs');
  87. $('<br>',{class:'clear'}).appendTo('#tabs');
  88. $tabs.find('a:first').trigger('click');
  89. // Make a droppable file uploader, if possible
  90. var $uploader = $('#file-upload');
  91. var fileUploader = new BinaryFileUploader({
  92. element: $uploader[0],
  93. onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
  94. });
  95. if (!fileUploader.hasFileUploaderSupport())
  96. this.setMessage("Your browser doesn't support the file reading API.", 'error');
  97. // Read boards.h, Configuration.h, Configuration_adv.h
  98. var ajax_count = 0, success_count = 0;
  99. var loaded_items = {};
  100. var config_files = [boards_file, config_file, config_adv_file];
  101. $.each(config_files, function(i,fname){
  102. $.ajax({
  103. url: marlin_config+'/'+fname,
  104. type: 'GET',
  105. async: true,
  106. cache: false,
  107. success: function(txt) {
  108. loaded_items[fname] = function(){ self.fileLoaded(fname, txt); };
  109. success_count++;
  110. },
  111. complete: function() {
  112. ajax_count++;
  113. if (ajax_count >= 3) {
  114. $.each(config_files, function(i,fname){ if (loaded_items[fname] !== undefined) loaded_items[fname](); });
  115. self.refreshConfigForm();
  116. if (success_count < ajax_count)
  117. self.setMessage('Unable to load configurations. Use the upload field instead.', 'error');
  118. }
  119. }
  120. });
  121. });
  122. },
  123. setMessage: function(msg,type) {
  124. if (msg) {
  125. if (type === undefined) type = 'message';
  126. var $err = $('<p class="'+type+'">'+msg+'</p>'), err = $err[0];
  127. $('#message').prepend($err);
  128. var baseColor = $err.css('color').replace(/rgba?\(([^),]+,[^),]+,[^),]+).*/, 'rgba($1,');
  129. var d = new Date();
  130. err.startTime = d.getTime();
  131. err.pulser = setInterval(function(){
  132. d = new Date();
  133. var pulse_time = (d.getTime() - err.startTime);
  134. $err.css({color:baseColor+(0.5+Math.sin(pulse_time/200)*0.4)+')'});
  135. if (pulse_time > 5000) {
  136. clearInterval(err.pulser);
  137. $err.remove();
  138. }
  139. }, 50);
  140. }
  141. else {
  142. $('#message p.error, #message p.warning').each(function() {
  143. if (this.pulser !== undefined && this.pulser)
  144. clearInterval(this.pulser);
  145. $(this).remove();
  146. });
  147. }
  148. },
  149. /**
  150. * Init the boards array from a boards.h file
  151. */
  152. initBoardsFromText: function(txt) {
  153. boards_list = {};
  154. var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[\\w_]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
  155. while((r = findDef.exec(txt)) !== null) {
  156. boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')');
  157. }
  158. this.log("Loaded boards", 3); this.log(boards_list, 3);
  159. has_boards = true;
  160. },
  161. /**
  162. * Init the thermistors array from the Configuration.h file
  163. */
  164. initThermistorsFromText: function(txt) {
  165. // Get all the thermistors and save them into an object
  166. var r, s, findDef = new RegExp('(//.*\n)+\\s+(#define[ \\t]+TEMP_SENSOR_0)', 'g');
  167. r = findDef.exec(txt);
  168. findDef = new RegExp('^//[ \\t]*([-\\d]+)[ \\t]+is[ \\t]+(.*)[ \\t]*$', 'gm');
  169. while((s = findDef.exec(r[0])) !== null) {
  170. therms_list[s[1]] = s[1].prePad(4, '  ') + " — " + s[2];
  171. }
  172. },
  173. /**
  174. * Handle a file being dropped on the file field
  175. */
  176. handleFileLoad: function(txt, $uploader) {
  177. txt += '';
  178. var filename = $uploader.val().replace(/.*[\/\\](.*)$/, '$1');
  179. switch(filename) {
  180. case boards_file:
  181. case config_file:
  182. case config_adv_file:
  183. this.fileLoaded(filename, txt);
  184. break;
  185. default:
  186. this.log("Can't parse "+filename, 1);
  187. break;
  188. }
  189. },
  190. /**
  191. * Process a file after it's been successfully loaded
  192. */
  193. fileLoaded: function(filename, txt) {
  194. this.log("fileLoaded:"+filename,4);
  195. switch(filename) {
  196. case boards_file:
  197. this.initBoardsFromText(txt);
  198. $('#MOTHERBOARD').html('').addOptions(boards_list);
  199. if (has_config) this.initField('MOTHERBOARD');
  200. this.setMessage(boards_file+' loaded successfully.');
  201. break;
  202. case config_file:
  203. if (has_boards) {
  204. $config.text(txt);
  205. total_config_lines = txt.lineCount();
  206. this.initThermistorsFromText(txt);
  207. this.purgeDefineInfo(false);
  208. this.refreshConfigForm();
  209. this.setMessage(config_file+' loaded successfully.');
  210. has_config = true;
  211. }
  212. else {
  213. this.setMessage("Upload a " + boards_file + " file first!", 'error');
  214. }
  215. break;
  216. case config_adv_file:
  217. if (has_config) {
  218. $config_adv.text(txt);
  219. total_config_adv_lines = txt.lineCount();
  220. this.purgeDefineInfo(true);
  221. this.refreshConfigForm();
  222. this.setMessage(config_adv_file+' loaded successfully.');
  223. has_config_adv = true;
  224. }
  225. else {
  226. this.setMessage("Upload a " + config_file + " file first!", 'error');
  227. }
  228. break;
  229. }
  230. },
  231. /**
  232. * Add enhancements to the form
  233. */
  234. initConfigForm: function() {
  235. // Modify form fields and make the form responsive.
  236. // As values change on the form, we could update the
  237. // contents of text areas containing the configs, for
  238. // example.
  239. // while(!$config_adv.text() == null) {}
  240. // while(!$config.text() == null) {}
  241. // Go through all form items with names
  242. $('#config_form').find('[name]').each(function() {
  243. // Set its id to its name
  244. var name = $(this).attr('name');
  245. $(this).attr({id: name});
  246. // Attach its label sibling
  247. var $label = $(this).prev('label');
  248. if ($label.length) $label.attr('for',name);
  249. });
  250. // Get all 'switchable' class items and add a checkbox
  251. $('#config_form .switchable').each(function(){
  252. $(this).after(
  253. $('<input>',{type:'checkbox',value:'1',class:'enabler'}).prop('checked',true)
  254. .attr('id',this.id + '-switch')
  255. .change(self.handleSwitch)
  256. );
  257. });
  258. // Add options to the popup menus
  259. $('#SERIAL_PORT').addOptions([0,1,2,3,4,5,6,7]);
  260. $('#BAUDRATE').addOptions([2400,9600,19200,38400,57600,115200,250000]);
  261. $('#EXTRUDERS').addOptions([1,2,3,4]);
  262. $('#POWER_SUPPLY').addOptions({'1':'ATX','2':'Xbox 360'});
  263. // Replace the Serial popup menu with a stepper control
  264. $('#serial_stepper').jstepper({
  265. min: 0,
  266. max: 3,
  267. val: $('#SERIAL_PORT').val(),
  268. arrowWidth: '18px',
  269. arrowHeight: '15px',
  270. color: '#FFF',
  271. acolor: '#F70',
  272. hcolor: '#FF0',
  273. id: 'select-me',
  274. textStyle: {width:'1.5em',fontSize:'120%',textAlign:'center'},
  275. onChange: function(v) { $('#SERIAL_PORT').val(v).trigger('change'); }
  276. });
  277. },
  278. /**
  279. * Update all fields on the form after loading a configuration
  280. */
  281. refreshConfigForm: function() {
  282. /**
  283. * For now I'm manually creating these references
  284. * but I should be able to parse Configuration.h
  285. * and iterate the #defines.
  286. *
  287. * For any #ifdef blocks I can create field groups
  288. * which can be dimmed together when the option
  289. * is disabled.
  290. *
  291. * Then we only need to specify exceptions to
  292. * standard behavior, (which is to add a text field)
  293. */
  294. this.initField('SERIAL_PORT');
  295. this.initField('BAUDRATE');
  296. this.initField('BTENABLED');
  297. $('#MOTHERBOARD').html('').addOptions(boards_list);
  298. this.initField('MOTHERBOARD');
  299. this.initField('CUSTOM_MENDEL_NAME');
  300. this.initField('MACHINE_UUID');
  301. this.initField('EXTRUDERS');
  302. this.initField('POWER_SUPPLY');
  303. this.initField('PS_DEFAULT_OFF');
  304. $('#TEMP_SENSOR_0, #TEMP_SENSOR_1, #TEMP_SENSOR_2, #TEMP_SENSOR_BED').html('').addOptions(therms_list);
  305. this.initField('TEMP_SENSOR_0');
  306. this.initField('TEMP_SENSOR_1');
  307. this.initField('TEMP_SENSOR_2');
  308. this.initField('TEMP_SENSOR_BED');
  309. this.initField('TEMP_SENSOR_1_AS_REDUNDANT');
  310. this.initField('MAX_REDUNDANT_TEMP_SENSOR_DIFF');
  311. this.initField('TEMP_RESIDENCY_TIME');
  312. },
  313. /**
  314. * Make a field responsive and initialize its defineInfo
  315. */
  316. initField: function(name, adv) {
  317. this.log("initField:"+name,4);
  318. var $elm = $('#'+name), elm = $elm[0];
  319. if (elm.defineInfo == null) {
  320. var inf = elm.defineInfo = this.getDefineInfo(name, adv);
  321. $elm.on($elm.attr('type') == 'text' ? 'input' : 'change', this.handleChange);
  322. if (inf.comment) {
  323. var $tipme = $elm.prev('label');
  324. if (inf.comment && $tipme.length) {
  325. var $tt = $('#tooltip');
  326. $tipme.hover(
  327. function() {
  328. var offs = $tipme.offset();
  329. $tt.text(inf.comment)
  330. .append('<span>')
  331. .css({bottom:($tt.parent().height()-offs.top+20)+'px',left:(offs.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. $tt.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. var comment = '';
  559. // Get the end-of-line comment, if there is one
  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}\\s{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[0])) !== null) {
  571. if (s[1].match(/\/\/[ \\t]*#define/) == null)
  572. comment += s[1] + "\n";
  573. }
  574. }
  575. }
  576. $.extend(info, {
  577. comment: comment.trim(),
  578. lineNum: this.getLineNumberOfText(info.line, txt)
  579. });
  580. }
  581. else
  582. info = null;
  583. this.log(info,2);
  584. return info;
  585. },
  586. /**
  587. * Count the number of lines before a match, return -1 on fail
  588. */
  589. getLineNumberOfText: function(line, txt) {
  590. var pos = txt.indexOf(line);
  591. return (pos < 0) ? pos : txt.substr(0, pos).lineCount();
  592. },
  593. log: function(o,l) {
  594. if (l === undefined) l = 0;
  595. if (this.logging>=l*1) console.log(o);
  596. },
  597. logOnce: function(o) {
  598. if (o.didLogThisObject === undefined) {
  599. this.log(o);
  600. o.didLogThisObject = true;
  601. }
  602. },
  603. EOF: null
  604. };
  605. })();
  606. // Typically the app would be in its own file, but this would be here
  607. configuratorApp.init();
  608. });