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() { function init() {
Config.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 = { var Config = {
layout: '',
configPath: '', configPath: '',
configOptionValues: '', configOptionValues: '',
init: function () { init: function () {
@ -16,6 +57,7 @@ var Config = {
}, },
storeConfigLayout: function (request) { storeConfigLayout: function (request) {
Config.layout = request.responseXML; Config.layout = request.responseXML;
ConfigXML.parseXML(request.responseXML);
new Ajax.Request('/xml-rpc?method=stats',{method: 'get',onComplete: Config.updateStatus}); new Ajax.Request('/xml-rpc?method=stats',{method: 'get',onComplete: Config.updateStatus});
}, },
updateStatus: function (request) { updateStatus: function (request) {
@ -27,21 +69,21 @@ var Config = {
}, },
showConfig: function (request) { showConfig: function (request) {
Config.configOptionValues = request.responseXML; Config.configOptionValues = request.responseXML;
var sections = $A(Config.layout.getElementsByTagName('section')); var sections = ConfigXML.getSections();
sections.each(function (section) { sections.each(function (section) {
var head = document.createElement('div'); var head = document.createElement('div');
head.className= 'naviheader'; head.className= 'naviheader';
head.appendChild(document.createTextNode(section.getAttribute('name'))); head.appendChild(document.createTextNode(section));
var body = document.createElement('div'); var body = document.createElement('div');
body.className = 'navibox'; body.className = 'navibox';
if ('Server' == section.getAttribute('name')) { if ('Server' == section) {
body.appendChild(Builder.node('span',{id:'config_path'},'Config File')); body.appendChild(Builder.node('span',{id:'config_path'},'Config File'));
body.appendChild(document.createTextNode(Config.configPath)); body.appendChild(document.createTextNode(Config.configPath));
body.appendChild(Builder.node('br')); body.appendChild(Builder.node('br'));
body.appendChild(Builder.node('div',{style: 'clear: both;'})); body.appendChild(Builder.node('div',{style: 'clear: both;'}));
} }
$A(section.getElementsByTagName('item')).each(function (item) { ConfigXML.getItems(section).each(function (itemId) {
body.appendChild(Config._buildItem(item)); body.appendChild(Config._buildItem(section,itemId));
}); });
$('theform').appendChild(head); $('theform').appendChild(head);
$('theform').appendChild(body); $('theform').appendChild(body);
@ -68,39 +110,40 @@ var Config = {
} }
} }
}, },
_buildItem: function(item) { _buildItem: function(section,itemId) {
var ret; var ret;
var itemId = item.getAttribute('id');
var href; var href;
var span; 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': case 'short_text':
ret = BuildElement.input(itemId, ret = BuildElement.input(postId,
Element.textContent(item.getElementsByTagName('name')[0]), item.name,
Config._getConfigOptionValue(itemId),20, Config._getConfigOptionValue(itemId),20,
Element.textContent(item.getElementsByTagName('short_description')[0]), item.short_description,
''); '');
break; break;
case 'long_text': case 'long_text':
ret = BuildElement.input(itemId, ret = BuildElement.input(postId,
Element.textContent(item.getElementsByTagName('name')[0]), item.name,
Config._getConfigOptionValue(itemId),80, Config._getConfigOptionValue(itemId),80,
Element.textContent(item.getElementsByTagName('short_description')[0]), item.short_description,
''); '');
break; break;
case 'short_text_multiple': case 'short_text_multiple':
ret = document.createDocumentFragment(); ret = document.createDocumentFragment();
Config._getConfigOptionValue(itemId,true).each(function (value,i) { Config._getConfigOptionValue(itemId,true).each(function (value,i) {
var span = document.createElement('span'); var span = document.createElement('span');
span.appendChild(BuildElement.input(itemId+i, span.appendChild(BuildElement.input(postId+i,
Element.textContent(item.getElementsByTagName('name')[0]), item.name,
value,20, value,20,
Element.textContent(item.getElementsByTagName('short_description')[0]) item.short_description
)); ));
ret.appendChild(span); ret.appendChild(span);
}); });
href = Builder.node('a',{href:'javascript://',className:'addItemHref'}, href = Builder.node('a',{href:'javascript://',className:'addItemHref'},
Element.textContent(item.getElementsByTagName('add_item_text')[0])); item.add_item_text);
ret.appendChild(href); ret.appendChild(href);
Event.observe(href,'click',Config._addItem); Event.observe(href,'click',Config._addItem);
ret.appendChild(Builder.node('div',{style:'clear: both'})); ret.appendChild(Builder.node('div',{style:'clear: both'}));
@ -110,15 +153,15 @@ var Config = {
ret = document.createDocumentFragment(); ret = document.createDocumentFragment();
Config._getConfigOptionValue(itemId,true).each(function (value,i) { Config._getConfigOptionValue(itemId,true).each(function (value,i) {
var span = document.createElement('span'); var span = document.createElement('span');
span.appendChild(BuildElement.input(itemId+i, span.appendChild(BuildElement.input(postId+i,
Element.textContent(item.getElementsByTagName('name')[0]), item.name,
value,80, value,80,
Element.textContent(item.getElementsByTagName('short_description')[0]) item.short_description
)); ));
ret.appendChild(span); ret.appendChild(span);
}); });
href = Builder.node('a',{href:'javascript://',className:'addItemHref'}, href = Builder.node('a',{href:'javascript://',className:'addItemHref'},
Element.textContent(item.getElementsByTagName('add_item_text')[0])); item.add_item_text);
ret.appendChild(href); ret.appendChild(href);
Event.observe(href,'click',Config._addItem); Event.observe(href,'click',Config._addItem);
ret.appendChild(Builder.node('div',{style:'clear: both'})); ret.appendChild(Builder.node('div',{style:'clear: both'}));
@ -126,13 +169,13 @@ var Config = {
case 'select': case 'select':
var value = Config._getConfigOptionValue(itemId); var value = Config._getConfigOptionValue(itemId);
if (!value) { if (!value) {
value = Element.textContent(item.getElementsByTagName('default_value')[0]); value = item.default_value;
} }
ret = BuildElement.select(itemId, ret = BuildElement.select(postId,
Element.textContent(item.getElementsByTagName('name')[0]), item.name,
item.getElementsByTagName('option'), item.options,
value, value,
Element.textContent(item.getElementsByTagName('short_description')[0]) item.short_description
); );
break; break;
} }
@ -164,7 +207,7 @@ var BuildElement = {
label.appendChild(document.createTextNode(displayName)); label.appendChild(document.createTextNode(displayName));
frag.appendChild(label); frag.appendChild(label);
frag.appendChild(Builder.node('input',{id:id,name:id,className: 'text', frag.appendChild(Builder.node('input',{id: id,name: id,className: 'text',
value: value,size: size})); value: value,size: size}));
frag.appendChild(document.createTextNode('\u00a0')); frag.appendChild(document.createTextNode('\u00a0'));
frag.appendChild(document.createTextNode(short_description)); frag.appendChild(document.createTextNode(short_description));
@ -179,10 +222,10 @@ var BuildElement = {
label.appendChild(document.createTextNode(displayName)); label.appendChild(document.createTextNode(displayName));
frag.appendChild(label); frag.appendChild(label);
var select = Builder.node('select',{id: id,name: id, size: 1}); var select = Builder.node('select',{id: id,name: id,size: 1});
$A(options).each(function (option) { $A(options).each(function (option) {
select.appendChild(Builder.node('option',{value: option.getAttribute('value')}, select.appendChild(Builder.node('option',{value: option.value},
Element.textContent(option))); option.label));
}); });
select.value = value; select.value = value;
frag.appendChild(select); frag.appendChild(select);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config> <config>
<section name="Server"> <section name="Server">
<item id="servername"> <item id="servername" config_section="general">
<name>Server Name</name> <name>Server Name</name>
<short_description> <short_description>
The name iTunes and other daap clients should see The name iTunes and other daap clients should see
@ -13,43 +13,43 @@
</long_description> </long_description>
<type>short_text</type> <type>short_text</type>
</item> </item>
<item id="web_root"> <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>long_text</type>
</item> </item>
<item id="port"> <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>short_text</type>
</item> </item>
<item id="logfile"> <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>long_text</type>
</item> </item>
<item id="runas"> <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>short_text</type>
</item> </item>
<item id="admin_pw"> <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>short_text</type>
</item> </item>
<item id="password"> <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>short_text</type>
</item> </item>
<item id="compress"> <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?
@ -59,7 +59,7 @@
<option value="1">Yes</option> <option value="1">Yes</option>
<default_value>0</default_value> <default_value>0</default_value>
</item> </item>
<item id="debug"> <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.
@ -79,24 +79,24 @@
</item> </item>
</section> </section>
<section name="Music Files"> <section name="Music_Files" config_section="general">
<item id="mp3_dir"> <item id="mp3_dir">
<name>MP3 Directory</name> <name>MP3 Directory</name>
<short_description></short_description> <short_description></short_description>
<type>long_text_multiple</type> <type>long_text_multiple</type>
<add_item_text>Add Directory</add_item_text> <add_item_text>Add Directory</add_item_text>
</item> </item>
<item id="extensions"> <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>short_text</type>
</item> </item>
<item id="playlist"> <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>long_text</type>
</item> </item>
<item id="process_m3u"> <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?
@ -106,20 +106,20 @@
<option value="1">Yes</option> <option value="1">Yes</option>
<default_value>0</default_value> <default_value>0</default_value>
</item> </item>
<item id="compdirs"> <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>long_text_multiple</type>
<add_item_text>Add Directory</add_item_text> <add_item_text>Add Directory</add_item_text>
</item> </item>
<item id="art_filename"> <item id="art_filename" config_section="general">
<name>Art Filename</name> <name>Art Filename</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type>long_text</type>
</item> </item>
</section> </section>
<section name="Database"> <section name="Database" config_section="general">
<item id="db_type"> <item id="db_type">
<name>Database Type</name> <name>Database Type</name>
<short_description></short_description> <short_description></short_description>
@ -128,12 +128,12 @@
<option value="sqlite3">sqlite3</option> <option value="sqlite3">sqlite3</option>
<default_value>sqlite</default_value> <default_value>sqlite</default_value>
</item> </item>
<item id="db_parms"> <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>long_text</type>
</item> </item>
<item id="scan_type"> <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>select</type>
@ -142,14 +142,14 @@
<option value="2">2 - Painfully aggressive</option> <option value="2">2 - Painfully aggressive</option>
<default_value>2</default_value> <default_value>2</default_value>
</item> </item>
<item id="rescan_interval"> <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>short_text</type>
</item> </item>
<item id="always_scan"> <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>select</type>
@ -159,13 +159,13 @@
</item> </item>
</section> </section>
<section name="Plugins"> <section name="Plugins" config_section="plugins">
<item id="plugin_dir"> <item id="plugin_dir">
<name>Plugin Directory</name> <name>Plugin Directory</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type>long_text</type>
</item> </item>
<item id="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>short_text_multiple</type>
@ -173,13 +173,13 @@
</item> </item>
</section> </section>
<section name="Transcoding"> <section name="Transcoding" config_section="general">
<item id="ssc_prog"> <item id="ssc_prog">
<name>SSC Program</name> <name>SSC Program</name>
<short_description></short_description> <short_description></short_description>
<type>long_text</type> <type>long_text</type>
</item> </item>
<item id="ssc_codectypes"> <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>long_text</type>