Big fat refactoring of config.js and config.xml

This commit is contained in:
Anders Betnér 2006-05-20 21:51:14 +00:00
parent 1c55d153ff
commit 5eba5fc5ac
2 changed files with 135 additions and 148 deletions

View File

@ -9,11 +9,10 @@ Event.observe(window,'load',init);
else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1) else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
navigator.OS = 'nix';*/ navigator.OS = 'nix';*/
//Inform user if server restart needed //Inform user if server restart needed
//config.xml: Add browse file/dir button, add multiple, add textbox size?
//disable all page elements when read only //disable all page elements when read only
//better errormessage for not writable config //better errormessage for not writable config
//make tabs? //make tabs?
//add browse to all path and file options //create the path/file browser
// Config isn't defined until after the Event.observe above // Config isn't defined until after the Event.observe above
// I could have put it below Config = ... but I want all window.load events // I could have put it below Config = ... but I want all window.load events
@ -47,26 +46,31 @@ var ConfigXML = {
parseXML: function(xmlDoc) { parseXML: function(xmlDoc) {
$A(xmlDoc.getElementsByTagName('section')).each(function (section) { $A(xmlDoc.getElementsByTagName('section')).each(function (section) {
var items = {}; var items = {};
var option;
$A(section.getElementsByTagName('item')).each(function (item) { $A(section.getElementsByTagName('item')).each(function (item) {
option = {config_section: item.getAttribute('config_section'), var returnItem = {};
name: Element.textContent(item.getElementsByTagName('name')[0]), returnItem['config_section'] = item.getAttribute('config_section');
short_description: Element.textContent(item.getElementsByTagName('short_description')[0]), returnItem['id'] = item.getAttribute('id');
type: Element.textContent(item.getElementsByTagName('type')[0])}; $A(item.childNodes).each(function (node) {
if ('select' == option.type) { if (Element.textContent(node) == '') {
var options = []; return;
$A(item.getElementsByTagName('option')).each(function (option) { }
options.push({value: option.getAttribute('value'), if ('options' == node.nodeName) {
label: Element.textContent(option)}); var options = [];
}); $A(item.getElementsByTagName('option')).each(function (option) {
option.options = options; options.push({value: option.getAttribute('value'),
option.default_value = Element.textContent(item.getElementsByTagName('default_value')[0]); label: Element.textContent(option)});
} });
if (('short_text_multiple' == option.type) || returnItem['options'] = options;
('long_text_multiple' == option.type)) { } else {
option.add_item_text = Element.textContent(item.getElementsByTagName('add_item_text')[0]); returnItem[node.nodeName] = Element.textContent(node);
} }
items[item.getAttribute('id')] = option; if (node.hasAttributes) {
$A(node.attributes).each(function (attr) {
returnItem[attr.name] = attr.value;
});
}
});
items[item.getAttribute('id')] = returnItem;
}); });
ConfigXML.config[section.getAttribute('name')] = items; ConfigXML.config[section.getAttribute('name')] = items;
}); });
@ -77,6 +81,9 @@ var ConfigInitialValues = {
getValues: function () { getValues: function () {
return $H(this.values); return $H(this.values);
}, },
getValue: function (section,id) {
return this.values[section+':'+id];
},
parseXML: function (xmldoc) { parseXML: function (xmldoc) {
// IE and w3c treat xmldoc differently make shore firstChild is firstchild of <config> // IE and w3c treat xmldoc differently make shore firstChild is firstchild of <config>
if (xmldoc.childNodes[1] && xmldoc.childNodes[1].nodeName == 'config') { if (xmldoc.childNodes[1] && xmldoc.childNodes[1].nodeName == 'config') {
@ -87,7 +94,7 @@ var ConfigInitialValues = {
sections.each(function (section) { sections.each(function (section) {
var sectionName = section.nodeName; var sectionName = section.nodeName;
$A(section.childNodes).each(function (node) { $A(section.childNodes).each(function (node) {
if (node.childNodes.length > 1) { if (node.firstChild.hasChildNodes()) {
var values = []; var values = [];
$A(node.childNodes).each(function (n) { $A(node.childNodes).each(function (n) {
values.push(Element.textContent(n)); values.push(Element.textContent(n));
@ -157,97 +164,72 @@ var Config = {
buttons.appendChild(spacer); buttons.appendChild(spacer);
buttons.appendChild(save); buttons.appendChild(save);
} else { } else {
// What about all them unix variants? //###TODO What about all them unix variants?
buttons.appendChild(save); buttons.appendChild(save);
buttons.appendChild(spacer); buttons.appendChild(spacer);
buttons.appendChild(cancel); buttons.appendChild(cancel);
} }
} }
}, },
_parseConfigOptionValues: function (xmldoc) {
},
_getConfigOptionValue: function(id,multiple) {
if (multiple) {
var ret = [];
var option = Config.configOptionValues.getElementsByTagName(id);
if (option.length > 0) {
$A(option[0].getElementsByTagName('item')).each(function (item) {
ret.push(Element.textContent(item));
});
} else {
ret.push('');
}
return ret;
} else {
var value = Config.configOptionValues.getElementsByTagName(id);
if (value.length > 0) {
return Element.textContent(value[0]);
} else {
return '';
}
}
},
_buildItem: function(section,itemId) { _buildItem: function(section,itemId) {
var frag = document.createDocumentFragment(); var frag = document.createDocumentFragment();
var href; var href;
var span; var span;
var item = ConfigXML.getOption(section,itemId); var item = ConfigXML.getOption(section,itemId);
var postId = item.config_section + ':' + itemId; var postId = item.config_section + ':' + itemId;
var inputSize = 80;
var noBrowse = false; var noBrowse = false;
switch(item.type) { switch(item.type) {
case 'short_text': case 'text':
inputSize = 20; if (item.multiple) {
// Yes, we're falling through var values = ConfigInitialValues.getValue(item.config_section,item.id);
case 'long_text': if (!values || values.length == 0) {
frag.appendChild(BuildElement.input(postId,postId, values = [];
item.name, values.push('');
Config._getConfigOptionValue(itemId), }
inputSize, values.each(function (val,i) {
item.short_description, var span = document.createElement('span');
'')); span.appendChild(BuildElement.input(postId+i,postId,
frag.appendChild(Builder.node('br')); item.name,
break; val || item.default_value || '',
case 'short_text_multiple': item.size || 20,
inputSize = 20; item.short_description,
noBrowse = true; ''));
// Yes, we're falling through if (item.browse) {
case 'long_text_multiple': href = Builder.node('a',{href: 'javascript://'},'Browse');
Config._getConfigOptionValue(itemId,true).each(function (value,i) { Event.observe(href,'click',Config._browse);
var span = document.createElement('span'); span.appendChild(href);
span.appendChild(BuildElement.input(postId+i,postId, }
item.name, span.appendChild(document.createTextNode('\u00a0\u00a0'));
value,inputSize, href = Builder.node('a',{href: 'javascript://'},'Remove');
item.short_description Event.observe(href,'click',Config._removeItem);
)); span.appendChild(href);
if (!noBrowse) { span.appendChild(Builder.node('br'));
frag.appendChild(span);
});
href = Builder.node('a',{href:'javascript://',className:'addItemHref'},item.add_item_label);
frag.appendChild(href);
Event.observe(href,'click',Config._addItem);
frag.appendChild(Builder.node('div',{style:'clear: both'}));
} else {
frag.appendChild(BuildElement.input(postId,postId,
item.name,
ConfigInitialValues.getValue(item.config_section,item.id) || item.default_value || '',
item.size || 20,
item.short_description,
''));
if (item.browse) {
href = Builder.node('a',{href: 'javascript://'},'Browse'); href = Builder.node('a',{href: 'javascript://'},'Browse');
Event.observe(href,'click',Config._browse); Event.observe(href,'click',Config._browse);
span.appendChild(href); frag.appendChild(href);
} }
span.appendChild(document.createTextNode('\u00a0\u00a0')); frag.appendChild(Builder.node('br'));
href = Builder.node('a',{href: 'javascript://'},'Remove');
Event.observe(href,'click',Config._removeItem);
span.appendChild(href);
span.appendChild(Builder.node('br'));
frag.appendChild(span);
});
href = Builder.node('a',{href:'javascript://',className:'addItemHref'},
item.add_item_text);
frag.appendChild(href);
Event.observe(href,'click',Config._addItem);
frag.appendChild(Builder.node('div',{style:'clear: both'}));
break;
case 'select':
var value = Config._getConfigOptionValue(itemId);
if (!value) {
value = item.default_value;
} }
break;
case 'select':
frag.appendChild(BuildElement.select(postId, frag.appendChild(BuildElement.select(postId,
item.name, item.name,
item.options, item.options,
value, ConfigInitialValues.getValue(item.config_section,item.id) || item.default_value,
item.short_description)); item.short_description));
frag.appendChild(Builder.node('br')); frag.appendChild(Builder.node('br'));
break; break;
@ -291,7 +273,7 @@ var Config = {
} }
}, },
_browse: function(e) { _browse: function(e) {
alert('Browse directories'); alert('Browse');
} }
} }
var BuildElement = { var BuildElement = {
@ -310,8 +292,10 @@ var BuildElement = {
frag.appendChild(Builder.node('input',{id: id,name: name,className: 'text', frag.appendChild(Builder.node('input',{id: id,name: name,className: 'text',
value: value,size: size, disabled: 'disabled'})); value: value,size: size, disabled: 'disabled'}));
} }
frag.appendChild(document.createTextNode('\u00a0')); frag.appendChild(document.createTextNode('\u00a0'));
frag.appendChild(document.createTextNode(short_description)); if (short_description) {
frag.appendChild(document.createTextNode(short_description));
}
return frag; return frag;
}, },

View File

@ -11,71 +11,73 @@
name of the database exported via DAAP. Also know as "What shows up in name of the database exported via DAAP. Also know as "What shows up in
iTunes". iTunes".
</long_description> </long_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="web_root" config_section="general"> <item id="web_root" config_section="general">
<name>Web Root</name> <name>Web Root</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="directory">text</type>
</item> </item>
<item id="port" config_section="general"> <item id="port" config_section="general">
<name>Port</name> <name>Port</name>
<short_description> <short_description>
The port the server should run at; default is 3689 The port the server should run at; default is 3689
</short_description> </short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="logfile" config_section="general"> <item id="logfile" config_section="general">
<name>Logfile</name> <name>Logfile</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="file">text</type>
</item> </item>
<item id="runas" config_section="general"> <item id="runas" config_section="general">
<name>Run As</name> <name>Run As</name>
<short_description></short_description> <short_description></short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="admin_pw" config_section="general"> <item id="admin_pw" config_section="general">
<name>Admin password</name> <name>Admin password</name>
<short_description> <short_description>
The password for this administration interface. The password for this administration interface.
</short_description> </short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="password" config_section="general"> <item id="password" config_section="general">
<name>MP3 Password</name> <name>MP3 Password</name>
<short_description> <short_description>
The password clients need to access this server. The password clients need to access this server.
</short_description> </short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="compress" config_section="general"> <item id="compress" config_section="general">
<name>Compress</name> <name>Compress</name>
<short_description> <short_description>
Should browsing data be compressed on the way to the client? Should browsing data be compressed on the way to the client?
</short_description> </short_description>
<type>select</type> <type default_value="0">select</type>
<option value="0">No</option> <options>
<option value="1">Yes</option> <option value="0">No</option>
<default_value>0</default_value> <option value="1">Yes</option>
</options>
</item> </item>
<item id="debug" config_section="general"> <item id="debug" config_section="general">
<name>Debug Level</name> <name>Debug Level</name>
<short_description> <short_description>
Possible values are 0 to 9, 9 being the most detailed debug level. Possible values are 0 to 9, 9 being the most detailed debug level.
</short_description> </short_description>
<type>select</type> <type default_value="0">select</type>
<option value="0">0</option> <options>
<option value="1">1</option> <option value="0">0</option>
<option value="2">2</option> <option value="1">1</option>
<option value="3">3</option> <option value="2">2</option>
<option value="4">4</option> <option value="3">3</option>
<option value="5">5</option> <option value="4">4</option>
<option value="6">6</option> <option value="5">5</option>
<option value="7">7</option> <option value="6">6</option>
<option value="8">8</option> <option value="7">7</option>
<option value="9">9</option> <option value="8">8</option>
<default_value>0</default_value> <option value="9">9</option>
</options>
</item> </item>
</section> </section>
@ -83,34 +85,33 @@
<item id="mp3_dir" config_section="general"> <item id="mp3_dir" config_section="general">
<name>MP3 Directory</name> <name>MP3 Directory</name>
<short_description></short_description> <short_description></short_description>
<type>long_text_multiple</type> <type size="80" multiple="true" add_item_label="Add mp3 directory" browse="directory">text</type>
<add_item_text>Add mp3 directory</add_item_text>
</item> </item>
<item id="extensions" config_section="general"> <item id="extensions" config_section="general">
<name>Extensions</name> <name>Extensions</name>
<short_description></short_description> <short_description></short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="playlist" config_section="general"> <item id="playlist" config_section="general">
<name>Playlist File</name> <name>Playlist File</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="file">text</type>
</item> </item>
<item id="process_m3u" config_section="general"> <item id="process_m3u" config_section="general">
<name>Process .m3u Files</name> <name>Process .m3u Files</name>
<short_description> <short_description>
Should m3u files be processed as playlists? Should m3u files be processed as playlists?
</short_description> </short_description>
<type>select</type> <type default_value="0">select</type>
<option value="0">No</option> <options>
<option value="1">Yes</option> <option value="0">No</option>
<default_value>0</default_value> <option value="1">Yes</option>
</options>
</item> </item>
<item id="compdirs" config_section="general"> <item id="compdirs" config_section="general">
<name>Compilation Directories</name> <name>Compilation Directories</name>
<short_description></short_description> <short_description></short_description>
<type>long_text_multiple</type> <type size="80" multiple="true" add_item_label="Add compilation directory" browse="directory">text</type>
<add_item_text>Add compilation directory</add_item_text>
</item> </item>
</section> </section>
@ -118,39 +119,42 @@
<item id="db_type" config_section="general"> <item id="db_type" config_section="general">
<name>Database Type</name> <name>Database Type</name>
<short_description></short_description> <short_description></short_description>
<type>select</type> <type default="sqlite">select</type>
<option value="sqlite">sqlite</option> <options>
<option value="sqlite3">sqlite3</option> <option value="sqlite">sqlite</option>
<default_value>sqlite</default_value> <option value="sqlite3">sqlite3</option>
</options>
</item> </item>
<item id="db_parms" config_section="general"> <item id="db_parms" config_section="general">
<name>Database Directory</name> <name>Database Directory</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="directory">text</type>
</item> </item>
<item id="scan_type" config_section="general"> <item id="scan_type" config_section="general">
<name>Scan Type</name> <name>Scan Type</name>
<short_description></short_description> <short_description></short_description>
<type>select</type> <type default="2">select</type>
<option value="0">0 - Normal</option> <options>
<option value="1">1 - Aggressive</option> <option value="0">0 - Normal</option>
<option value="2">2 - Painfully aggressive</option> <option value="1">1 - Aggressive</option>
<default_value>2</default_value> <option value="2">2 - Painfully aggressive</option>
</options>
</item> </item>
<item id="rescan_interval" config_section="general"> <item id="rescan_interval" config_section="general">
<name>Rescan Interval</name> <name>Rescan Interval</name>
<short_description> <short_description>
How often should mt-daapd look for new files? In seconds. How often should mt-daapd look for new files? In seconds.
</short_description> </short_description>
<type>short_text</type> <type size="20">text</type>
</item> </item>
<item id="always_scan" config_section="general"> <item id="always_scan" config_section="general">
<name>Always Scan</name> <name>Always Scan</name>
<short_description></short_description> <short_description></short_description>
<type>select</type> <type default="0">select</type>
<option value="0">No</option> <options>
<option value="1">Yes</option> <option value="0">No</option>
<default_value>0</default_value> <option value="1">Yes</option>
</options>
</item> </item>
</section> </section>
@ -158,13 +162,12 @@
<item id="plugin_dir" config_section="plugins"> <item id="plugin_dir" config_section="plugins">
<name>Plugin Directory</name> <name>Plugin Directory</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="directory">text</type>
</item> </item>
<item id="plugins" config_section="plugins"> <item id="plugins" config_section="plugins">
<name>Plugins</name> <name>Plugins</name>
<short_description></short_description> <short_description></short_description>
<type>short_text_multiple</type> <type size="20" multiple="true" add_item_label="Add plugin">text</type>
<add_item_text>Add plugin</add_item_text>
</item> </item>
</section> </section>
@ -172,12 +175,12 @@
<item id="ssc_prog" config_section="general"> <item id="ssc_prog" config_section="general">
<name>SSC Program</name> <name>SSC Program</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80" browse="file">text</type>
</item> </item>
<item id="ssc_codectypes" config_section="general"> <item id="ssc_codectypes" config_section="general">
<name>SSC Codec Types</name> <name>SSC Codec Types</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type size="80">text</type>
</item> </item>
</section> </section>
</config> </config>