From f2710e11d1ea9ba9f0feaf7d67066836f990e425 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 2 Mar 2015 05:23:36 -0800 Subject: [PATCH] Optimize initial loading, code cleanup --- Marlin/configurator/css/configurator.css | 13 +- Marlin/configurator/js/configurator.js | 226 ++++++++++++----------- 2 files changed, 120 insertions(+), 119 deletions(-) diff --git a/Marlin/configurator/css/configurator.css b/Marlin/configurator/css/configurator.css index b660a78d14..88332075fb 100644 --- a/Marlin/configurator/css/configurator.css +++ b/Marlin/configurator/css/configurator.css @@ -91,17 +91,16 @@ label { margin-right: -450px; text-align: right; } -label.blocked, span.label.blocked { color: #AAA; } +label.blocked, label.added.blocked, label.added.blocked.sublabel { color: #AAA; } -span.label { - display: block; - float: left; +label.added.sublabel { + width: auto; margin: 11px -2.5em 0 1em; - padding-right: 3em; + padding: 0 3em 0 0; font-style: italic; color: #444; } -label+span.label { +label+label.added.sublabel { margin-left: 0; } @@ -341,5 +340,5 @@ select.one_of_4+span.label+select.one_of_4+span.label+select.one_of_4+span.label } -/*span.label.blocked, .blocked { display: none; }*/ +/*label.blocked, .blocked { display: none; }*/ diff --git a/Marlin/configurator/js/configurator.js b/Marlin/configurator/js/configurator.js index dc911f8c64..f14255ab28 100644 --- a/Marlin/configurator/js/configurator.js +++ b/Marlin/configurator/js/configurator.js @@ -146,6 +146,8 @@ window.configuratorApp = (function(){ $tooltip = $('#tooltip'), $cfg = $('#config_text'), $adv = $('#config_adv_text'), $config = $cfg.find('pre'), $config_adv = $adv.find('pre'), + config_file_list = [ boards_file, config_file, config_adv_file ], + config_list = [ $config, $config_adv ], define_info = {}, define_list = [[],[]], define_groups = [{},{}], @@ -199,10 +201,9 @@ window.configuratorApp = (function(){ // Read boards.h, Configuration.h, Configuration_adv.h var ajax_count = 0, success_count = 0; var loaded_items = {}; - var config_files = [boards_file, config_file, config_adv_file]; var isGithub = config.type == 'github'; var rateLimit = 0; - $.each(config_files, function(i,fname){ + $.each(config_file_list, function(i,fname){ var url = config_path(fname); $.ajax({ url: url, @@ -215,7 +216,7 @@ window.configuratorApp = (function(){ if (req.status == 200) { if (typeof req.responseText === 'string') { var txt = req.responseText; - loaded_items[fname] = function(){ self.fileLoaded(fname, txt); }; + loaded_items[fname] = function(){ self.fileLoaded(fname, txt, true); }; success_count++; // self.setMessage('The request for "'+fname+'" may be malformed.', 'error'); } @@ -236,13 +237,13 @@ window.configuratorApp = (function(){ timeLeft: Math.floor(txt.meta['X-RateLimit-Reset'] - Date.now()/1000), }; } - loaded_items[fname] = function(){ self.fileLoaded(fname, isGithub ? atob(txt.data.content.replace(/\s/g, '')) : txt); }; + loaded_items[fname] = function(){ self.fileLoaded(fname, isGithub ? atob(txt.data.content.replace(/\s/g, '')) : txt, true); }; success_count++; } }, complete: function() { ajax_count++; - if (ajax_count >= config_files.length) { + if (ajax_count >= config_file_list.length) { // If not all files loaded set an error if (success_count < ajax_count) self.setMessage('Unable to load configurations. Try the upload field.', 'error'); @@ -260,7 +261,7 @@ window.configuratorApp = (function(){ } } // Post-process all the loaded files - $.each(config_files, function(){ if (loaded_items[this]) loaded_items[this](); }); + $.each(config_file_list, function(){ if (loaded_items[this]) loaded_items[this](); }); } } }); @@ -270,9 +271,9 @@ window.configuratorApp = (function(){ /** * Make a download link visible and active */ - activateDownloadLink: function(adv) { - var $c = adv ? $config_adv : $config, txt = $c.text(); - var filename = (adv ? config_adv_file : config_file); + activateDownloadLink: function(cindex) { + var filename = config_file_list[cindex+1]; + var $c = config_list[cindex], txt = $c.text(); $c.prevAll('.download:first') .unbind('mouseover click') .mouseover(function() { @@ -303,8 +304,7 @@ window.configuratorApp = (function(){ .click(function(){ var $button = $(this); var zip = new JSZip(); - zip.file(config_file, $config.text()); - zip.file(config_adv_file, $config_adv.text()); + for (var e in [0,1]) zip.file(config_file_list[e+1], config_list[e].text()); var zipped = zip.generate({type:'blob'}); saveAs(zipped, $button.attr('download')); return false; @@ -341,10 +341,11 @@ window.configuratorApp = (function(){ /** * Get all the unique define names */ - initDefineList: function(index, txt) { + initDefineList: function(cindex) { var section = 'hidden', leave_out_defines = ['CONFIGURATION_H', 'CONFIGURATION_ADV_H'], define_sect = {}, + txt = config_list[cindex].text(), r, findDef = new RegExp('(@section|#define)[ \\t]+(\\w+)', 'gm'); while((r = findDef.exec(txt)) !== null) { var name = r[2]; @@ -353,27 +354,27 @@ window.configuratorApp = (function(){ else if ($.inArray(name, leave_out_defines) < 0 && !(name in define_section) && !(name in define_sect)) define_sect[name] = section; } - define_list[index] = Object.keys(define_sect); + define_list[cindex] = Object.keys(define_sect); $.extend(define_section, define_sect); - this.log(define_list[index], 2); + this.log(define_list[cindex], 2); }, /** * Find the defines in one of the configs that are just variants. * Group them together for form-building and other uses. */ - refreshDefineGroups: function(index) { + refreshDefineGroups: function(cindex) { var findDef = /^(|.*_)(([XYZE](MAX|MIN))|(E[0-3]|[XYZE01234])|MAX|MIN|(bed)?K[pid])(_.*|)$/; var match_prev, patt, title, nameList, groups = {}, match_section; - $.each(define_list[index], function() { + $.each(define_list[cindex], function(i, name) { if (match_prev) { - if (match_prev.exec(this) && define_section[this] == match_section) { - nameList.push(this); + if (match_prev.exec(name) && define_section[name] == match_section) { + nameList.push(name); } else { if (nameList.length > 1) { - $.each(nameList, function(){ - groups[this] = { + $.each(nameList, function(i,n){ + groups[n] = { pattern: patt, title: title, count: nameList.length @@ -384,7 +385,7 @@ window.configuratorApp = (function(){ } } if (!match_prev) { - var r = findDef.exec(this); + var r = findDef.exec(name); if (r != null) { switch(r[2]) { case '0': @@ -425,13 +426,13 @@ window.configuratorApp = (function(){ patt = '^' + r[1] + patt + r[7] + '$'; title = r[1] + title + r[7]; match_prev = new RegExp(patt); - match_section = define_section[this]; - nameList = [ this ]; + match_section = define_section[name]; + nameList = [ name ]; } } } }); - define_groups[index] = groups; + define_groups[cindex] = groups; }, /** @@ -442,7 +443,7 @@ window.configuratorApp = (function(){ initDependentGroups: function() { var findBlock = /^[ \t]*#(ifn?def|if|else|endif)[ \t]*(.*)([ \t]*\/\/[^\n]+)?$/gm, leave_out_defines = ['CONFIGURATION_H', 'CONFIGURATION_ADV_H']; - $.each([$config, $config_adv], function(i, $v) { + $.each(config_list, function(i, $v) { var ifStack = []; var r, txt = $v.text(); while((r = findBlock.exec(txt)) !== null) { @@ -481,7 +482,7 @@ window.configuratorApp = (function(){ var cond = c[0], line = c[1]; self.log("pop " + c[0], 4); if (dependentGroups[cond] === undefined) dependentGroups[cond] = []; - dependentGroups[cond].push({adv:i,start:line,end:lineNum}); + dependentGroups[cond].push({cindex:i,start:line,end:lineNum}); if (r[1] == 'else') { // Reverse the condition cond = (cond.indexOf('!') === 0) ? cond.substr(1) : ('!'+cond); @@ -504,9 +505,8 @@ window.configuratorApp = (function(){ */ initDefineInfo: function() { $.each(define_list, function(e,def_list){ - var adv = e == 1; - $.each(def_list, function(i,name) { - define_info[name] = self.getDefineInfo(name, adv); + $.each(def_list, function(i, name) { + define_info[name] = self.getDefineInfo(name, e); }); }); }, @@ -515,10 +515,12 @@ window.configuratorApp = (function(){ * Create fields for defines in a config that have none * Use define_groups data to group fields together */ - createFieldsForDefines: function(index) { - var n = 0, fail_list = []; - var grouping = false, group_pattern, group_regex, subitem, group_section, group_class; - $.each(define_list[index], function(i,name) { + createFieldsForDefines: function(cindex) { + // var n = 0; + var grouping = false, group = define_groups[cindex], + g_pattern, g_regex, g_subitem, g_section, g_class, + fail_list = []; + $.each(define_list[cindex], function(i, name) { var section = define_section[name]; if (section != 'hidden' && !$('#'+name).length) { var inf = define_info[name]; @@ -529,11 +531,10 @@ window.configuratorApp = (function(){ // Is this field in a sequence? // Then see if it's the second or after - var group = define_groups[index]; if (grouping) { - if (name in group && group_pattern == group[name].pattern && group_section == section) { - subitem = true; - sublabel = group_regex.exec(name)[1]; + if (name in group && g_pattern == group[name].pattern && g_section == section) { + g_subitem = true; + sublabel = g_regex.exec(name)[1]; } else grouping = false; @@ -541,20 +542,20 @@ window.configuratorApp = (function(){ // Start grouping? if (!grouping && name in group) { grouping = true; - subitem = false; + g_subitem = false; var grp = group[name]; - group_pattern = grp.pattern; - group_class = 'one_of_' + grp.count; + g_pattern = grp.pattern; + g_class = 'one_of_' + grp.count; label_text = grp.title; - group_regex = new RegExp(group_pattern); - group_section = section; - sublabel = group_regex.exec(name)[1]; + g_regex = new RegExp(g_pattern); + g_section = section; + sublabel = g_regex.exec(name)[1]; } var $ff = $('#'+section), $newfield, avail = eval(inf.enabled); - if (!(grouping && subitem)) { + if (!(grouping && g_subitem)) { var $newlabel = $('