Refactored config.js to parse the config.xml into a javascript

object and then build config.html from that.
Added a hackery translation between mt-daapd.conf sections
and the config.html sections, this will later be removed once
the sections conform.
This commit is contained in:
Anders Betnér 2006-05-16 21:39:22 +00:00
parent 1003812ee9
commit 95f5d64b96
2 changed files with 100 additions and 57 deletions

View File

@ -6,9 +6,50 @@ Event.observe(window,'load',function (e) {Config.init();});
function init() {
Config.init();
}
function hej() {
alert(Form.serialize('theform'));
}
var ConfigXML = {
config: [],
getOption: function (section,id) {
return this.config[section][id];
},
getSections: function () {
return $H(this.config).keys();
},
getItems: function(section) {
return $H(this.config[section]).keys();
},
parseXML: function(xmlDoc) {
$A(xmlDoc.getElementsByTagName('section')).each(function (section) {
var items = {};
var option;
$A(section.getElementsByTagName('item')).each(function (item) {
option = {config_section: item.getAttribute('config_section'),
name: Element.textContent(item.getElementsByTagName('name')[0]),
short_description: Element.textContent(item.getElementsByTagName('short_description')[0]),
type: Element.textContent(item.getElementsByTagName('type')[0])};
if ('select' == option.type) {
var options = [];
$A(item.getElementsByTagName('option')).each(function (option) {
options.push({value: option.getAttribute('value'),
label: Element.textContent(option)});
});
option.options = options;
option.default_value = Element.textContent(item.getElementsByTagName('default_value')[0]);
}
if (('short_text_multiple' == option.type) ||
('long_text_multiple' == option.type)) {
option.add_item_text = Element.textContent(item.getElementsByTagName('add_item_text')[0]);
}
items[item.getAttribute('id')] = option;
});
ConfigXML.config[section.getAttribute('name')] = items;
});
}
};
var Config = {
layout: '',
configPath: '',
configOptionValues: '',
init: function () {
@ -16,6 +57,7 @@ var Config = {
},
storeConfigLayout: function (request) {
Config.layout = request.responseXML;
ConfigXML.parseXML(request.responseXML);
new Ajax.Request('/xml-rpc?method=stats',{method: 'get',onComplete: Config.updateStatus});
},
updateStatus: function (request) {
@ -27,21 +69,21 @@ var Config = {
},
showConfig: function (request) {
Config.configOptionValues = request.responseXML;
var sections = $A(Config.layout.getElementsByTagName('section'));
var sections = ConfigXML.getSections();
sections.each(function (section) {
var head = document.createElement('div');
head.className= 'naviheader';
head.appendChild(document.createTextNode(section.getAttribute('name')));
head.appendChild(document.createTextNode(section));
var body = document.createElement('div');
body.className = 'navibox';
if ('Server' == section.getAttribute('name')) {
if ('Server' == section) {
body.appendChild(Builder.node('span',{id:'config_path'},'Config File'));
body.appendChild(document.createTextNode(Config.configPath));
body.appendChild(Builder.node('br'));
body.appendChild(Builder.node('div',{style: 'clear: both;'}));
}
$A(section.getElementsByTagName('item')).each(function (item) {
body.appendChild(Config._buildItem(item));
ConfigXML.getItems(section).each(function (itemId) {
body.appendChild(Config._buildItem(section,itemId));
});
$('theform').appendChild(head);
$('theform').appendChild(body);
@ -68,39 +110,40 @@ var Config = {
}
}
},
_buildItem: function(item) {
_buildItem: function(section,itemId) {
var ret;
var itemId = item.getAttribute('id');
var href;
var span;
switch(Element.textContent(item.getElementsByTagName('type')[0])) {
var item = ConfigXML.getOption(section,itemId);
var postId = item.config_section + ':' + itemId;
switch(item.type) {
case 'short_text':
ret = BuildElement.input(itemId,
Element.textContent(item.getElementsByTagName('name')[0]),
ret = BuildElement.input(postId,
item.name,
Config._getConfigOptionValue(itemId),20,
Element.textContent(item.getElementsByTagName('short_description')[0]),
item.short_description,
'');
break;
case 'long_text':
ret = BuildElement.input(itemId,
Element.textContent(item.getElementsByTagName('name')[0]),
ret = BuildElement.input(postId,
item.name,
Config._getConfigOptionValue(itemId),80,
Element.textContent(item.getElementsByTagName('short_description')[0]),
item.short_description,
'');
break;
case 'short_text_multiple':
ret = document.createDocumentFragment();
Config._getConfigOptionValue(itemId,true).each(function (value,i) {
var span = document.createElement('span');
span.appendChild(BuildElement.input(itemId+i,
Element.textContent(item.getElementsByTagName('name')[0]),
span.appendChild(BuildElement.input(postId+i,
item.name,
value,20,
Element.textContent(item.getElementsByTagName('short_description')[0])
item.short_description
));
ret.appendChild(span);
});
href = Builder.node('a',{href:'javascript://',className:'addItemHref'},
Element.textContent(item.getElementsByTagName('add_item_text')[0]));
item.add_item_text);
ret.appendChild(href);
Event.observe(href,'click',Config._addItem);
ret.appendChild(Builder.node('div',{style:'clear: both'}));
@ -110,15 +153,15 @@ var Config = {
ret = document.createDocumentFragment();
Config._getConfigOptionValue(itemId,true).each(function (value,i) {
var span = document.createElement('span');
span.appendChild(BuildElement.input(itemId+i,
Element.textContent(item.getElementsByTagName('name')[0]),
span.appendChild(BuildElement.input(postId+i,
item.name,
value,80,
Element.textContent(item.getElementsByTagName('short_description')[0])
item.short_description
));
ret.appendChild(span);
});
href = Builder.node('a',{href:'javascript://',className:'addItemHref'},
Element.textContent(item.getElementsByTagName('add_item_text')[0]));
item.add_item_text);
ret.appendChild(href);
Event.observe(href,'click',Config._addItem);
ret.appendChild(Builder.node('div',{style:'clear: both'}));
@ -126,13 +169,13 @@ var Config = {
case 'select':
var value = Config._getConfigOptionValue(itemId);
if (!value) {
value = Element.textContent(item.getElementsByTagName('default_value')[0]);
value = item.default_value;
}
ret = BuildElement.select(itemId,
Element.textContent(item.getElementsByTagName('name')[0]),
item.getElementsByTagName('option'),
ret = BuildElement.select(postId,
item.name,
item.options,
value,
Element.textContent(item.getElementsByTagName('short_description')[0])
item.short_description
);
break;
}
@ -181,8 +224,8 @@ var BuildElement = {
var select = Builder.node('select',{id: id,name: id,size: 1});
$A(options).each(function (option) {
select.appendChild(Builder.node('option',{value: option.getAttribute('value')},
Element.textContent(option)));
select.appendChild(Builder.node('option',{value: option.value},
option.label));
});
select.value = value;
frag.appendChild(select);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config>
<section name="Server">
<item id="servername">
<item id="servername" config_section="general">
<name>Server Name</name>
<short_description>
The name iTunes and other daap clients should see
@ -13,43 +13,43 @@
</long_description>
<type>short_text</type>
</item>
<item id="web_root">
<item id="web_root" config_section="general">
<name>Web Root</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="port">
<item id="port" config_section="general">
<name>Port</name>
<short_description>
The port the server should run at; default is 3689
</short_description>
<type>short_text</type>
</item>
<item id="logfile">
<item id="logfile" config_section="general">
<name>Logfile</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="runas">
<item id="runas" config_section="general">
<name>Run As</name>
<short_description></short_description>
<type>short_text</type>
</item>
<item id="admin_pw">
<item id="admin_pw" config_section="general">
<name>Admin password</name>
<short_description>
The password for this administration interface.
</short_description>
<type>short_text</type>
</item>
<item id="password">
<item id="password" config_section="general">
<name>MP3 Password</name>
<short_description>
The password clients need to access this server.
</short_description>
<type>short_text</type>
</item>
<item id="compress">
<item id="compress" config_section="general">
<name>Compress</name>
<short_description>
Should browsing data be compressed on the way to the client?
@ -59,7 +59,7 @@
<option value="1">Yes</option>
<default_value>0</default_value>
</item>
<item id="debug">
<item id="debug" config_section="general">
<name>Debug Level</name>
<short_description>
Possible values are 0 to 9, 9 being the most detailed debug level.
@ -79,24 +79,24 @@
</item>
</section>
<section name="Music Files">
<section name="Music_Files" config_section="general">
<item id="mp3_dir">
<name>MP3 Directory</name>
<short_description></short_description>
<type>long_text_multiple</type>
<add_item_text>Add Directory</add_item_text>
</item>
<item id="extensions">
<item id="extensions" config_section="general">
<name>Extensions</name>
<short_description></short_description>
<type>short_text</type>
</item>
<item id="playlist">
<item id="playlist" config_section="general">
<name>Playlist File</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="process_m3u">
<item id="process_m3u" config_section="general">
<name>Process .m3u Files</name>
<short_description>
Should m3u files be processed as playlists?
@ -106,20 +106,20 @@
<option value="1">Yes</option>
<default_value>0</default_value>
</item>
<item id="compdirs">
<item id="compdirs" config_section="general">
<name>Compilation Directories</name>
<short_description></short_description>
<type>long_text_multiple</type>
<add_item_text>Add Directory</add_item_text>
</item>
<item id="art_filename">
<item id="art_filename" config_section="general">
<name>Art Filename</name>
<short_description></short_description>
<type>long_text</type>
</item>
</section>
<section name="Database">
<section name="Database" config_section="general">
<item id="db_type">
<name>Database Type</name>
<short_description></short_description>
@ -128,12 +128,12 @@
<option value="sqlite3">sqlite3</option>
<default_value>sqlite</default_value>
</item>
<item id="db_parms">
<item id="db_parms" config_section="general">
<name>Database Directory</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="scan_type">
<item id="scan_type" config_section="general">
<name>Scan Type</name>
<short_description></short_description>
<type>select</type>
@ -142,14 +142,14 @@
<option value="2">2 - Painfully aggressive</option>
<default_value>2</default_value>
</item>
<item id="rescan_interval">
<item id="rescan_interval" config_section="general">
<name>Rescan Interval</name>
<short_description>
How often should mt-daapd look for new files? In seconds.
</short_description>
<type>short_text</type>
</item>
<item id="always_scan">
<item id="always_scan" config_section="general">
<name>Always Scan</name>
<short_description></short_description>
<type>select</type>
@ -159,13 +159,13 @@
</item>
</section>
<section name="Plugins">
<section name="Plugins" config_section="plugins">
<item id="plugin_dir">
<name>Plugin Directory</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="plugins">
<item id="plugins" config_section="plugins">
<name>Plugins</name>
<short_description></short_description>
<type>short_text_multiple</type>
@ -173,13 +173,13 @@
</item>
</section>
<section name="Transcoding">
<section name="Transcoding" config_section="general">
<item id="ssc_prog">
<name>SSC Program</name>
<short_description></short_description>
<type>long_text</type>
</item>
<item id="ssc_codectypes">
<item id="ssc_codectypes" config_section="general">
<name>SSC Codec Types</name>
<short_description></short_description>
<type>long_text</type>