From 95f5d64b9606f395640399d865172906e88f316a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Betn=C3=A9r?= Date: Tue, 16 May 2006 21:39:22 +0000 Subject: [PATCH] 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. --- admin-root/config.js | 109 +++++++++++++++++++++++++++++------------- admin-root/config.xml | 48 +++++++++---------- 2 files changed, 100 insertions(+), 57 deletions(-) diff --git a/admin-root/config.js b/admin-root/config.js index 2044b351..2cb19b74 100644 --- a/admin-root/config.js +++ b/admin-root/config.js @@ -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; } @@ -164,7 +207,7 @@ var BuildElement = { label.appendChild(document.createTextNode(displayName)); 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})); frag.appendChild(document.createTextNode('\u00a0')); frag.appendChild(document.createTextNode(short_description)); @@ -179,10 +222,10 @@ var BuildElement = { label.appendChild(document.createTextNode(displayName)); 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) { - 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); diff --git a/admin-root/config.xml b/admin-root/config.xml index f344317d..fc7a55b0 100644 --- a/admin-root/config.xml +++ b/admin-root/config.xml @@ -1,7 +1,7 @@
- + Server Name The name iTunes and other daap clients should see @@ -13,43 +13,43 @@ short_text - + Web Root long_text - + Port The port the server should run at; default is 3689 short_text - + Logfile long_text - + Run As short_text - + Admin password The password for this administration interface. short_text - + MP3 Password The password clients need to access this server. short_text - + Compress Should browsing data be compressed on the way to the client? @@ -59,7 +59,7 @@ 0 - + Debug Level Possible values are 0 to 9, 9 being the most detailed debug level. @@ -79,24 +79,24 @@
-
+
MP3 Directory long_text_multiple Add Directory - + Extensions short_text - + Playlist File long_text - + Process .m3u Files Should m3u files be processed as playlists? @@ -106,20 +106,20 @@ 0 - + Compilation Directories long_text_multiple Add Directory - + Art Filename long_text
-
+
Database Type @@ -128,12 +128,12 @@ sqlite - + Database Directory long_text - + Scan Type select @@ -142,14 +142,14 @@ 2 - + Rescan Interval How often should mt-daapd look for new files? In seconds. short_text - + Always Scan select @@ -159,13 +159,13 @@
-
+
Plugin Directory long_text - + Plugins short_text_multiple @@ -173,13 +173,13 @@
-
+
SSC Program long_text - + SSC Codec Types long_text