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)
navigator.OS = 'nix';*/
//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
//better errormessage for not writable config
//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
// I could have put it below Config = ... but I want all window.load events
@ -47,26 +46,31 @@ var ConfigXML = {
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;
var returnItem = {};
returnItem['config_section'] = item.getAttribute('config_section');
returnItem['id'] = item.getAttribute('id');
$A(item.childNodes).each(function (node) {
if (Element.textContent(node) == '') {
return;
}
if ('options' == node.nodeName) {
var options = [];
$A(item.getElementsByTagName('option')).each(function (option) {
options.push({value: option.getAttribute('value'),
label: Element.textContent(option)});
});
returnItem['options'] = options;
} else {
returnItem[node.nodeName] = Element.textContent(node);
}
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;
});
@ -77,6 +81,9 @@ var ConfigInitialValues = {
getValues: function () {
return $H(this.values);
},
getValue: function (section,id) {
return this.values[section+':'+id];
},
parseXML: function (xmldoc) {
// IE and w3c treat xmldoc differently make shore firstChild is firstchild of <config>
if (xmldoc.childNodes[1] && xmldoc.childNodes[1].nodeName == 'config') {
@ -87,7 +94,7 @@ var ConfigInitialValues = {
sections.each(function (section) {
var sectionName = section.nodeName;
$A(section.childNodes).each(function (node) {
if (node.childNodes.length > 1) {
if (node.firstChild.hasChildNodes()) {
var values = [];
$A(node.childNodes).each(function (n) {
values.push(Element.textContent(n));
@ -157,97 +164,72 @@ var Config = {
buttons.appendChild(spacer);
buttons.appendChild(save);
} else {
// What about all them unix variants?
//###TODO What about all them unix variants?
buttons.appendChild(save);
buttons.appendChild(spacer);
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) {
var frag = document.createDocumentFragment();
var href;
var span;
var item = ConfigXML.getOption(section,itemId);
var postId = item.config_section + ':' + itemId;
var inputSize = 80;
var noBrowse = false;
switch(item.type) {
case 'short_text':
inputSize = 20;
// Yes, we're falling through
case 'long_text':
frag.appendChild(BuildElement.input(postId,postId,
item.name,
Config._getConfigOptionValue(itemId),
inputSize,
item.short_description,
''));
frag.appendChild(Builder.node('br'));
break;
case 'short_text_multiple':
inputSize = 20;
noBrowse = true;
// Yes, we're falling through
case 'long_text_multiple':
Config._getConfigOptionValue(itemId,true).each(function (value,i) {
var span = document.createElement('span');
span.appendChild(BuildElement.input(postId+i,postId,
item.name,
value,inputSize,
item.short_description
));
if (!noBrowse) {
case 'text':
if (item.multiple) {
var values = ConfigInitialValues.getValue(item.config_section,item.id);
if (!values || values.length == 0) {
values = [];
values.push('');
}
values.each(function (val,i) {
var span = document.createElement('span');
span.appendChild(BuildElement.input(postId+i,postId,
item.name,
val || item.default_value || '',
item.size || 20,
item.short_description,
''));
if (item.browse) {
href = Builder.node('a',{href: 'javascript://'},'Browse');
Event.observe(href,'click',Config._browse);
span.appendChild(href);
}
span.appendChild(document.createTextNode('\u00a0\u00a0'));
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_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');
Event.observe(href,'click',Config._browse);
span.appendChild(href);
frag.appendChild(href);
}
span.appendChild(document.createTextNode('\u00a0\u00a0'));
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;
frag.appendChild(Builder.node('br'));
}
break;
case 'select':
frag.appendChild(BuildElement.select(postId,
item.name,
item.options,
value,
ConfigInitialValues.getValue(item.config_section,item.id) || item.default_value,
item.short_description));
frag.appendChild(Builder.node('br'));
break;
@ -291,7 +273,7 @@ var Config = {
}
},
_browse: function(e) {
alert('Browse directories');
alert('Browse');
}
}
var BuildElement = {
@ -310,8 +292,10 @@ var BuildElement = {
frag.appendChild(Builder.node('input',{id: id,name: name,className: 'text',
value: value,size: size, disabled: 'disabled'}));
}
frag.appendChild(document.createTextNode('\u00a0'));
frag.appendChild(document.createTextNode(short_description));
frag.appendChild(document.createTextNode('\u00a0'));
if (short_description) {
frag.appendChild(document.createTextNode(short_description));
}
return frag;
},

View File

@ -11,71 +11,73 @@
name of the database exported via DAAP. Also know as "What shows up in
iTunes".
</long_description>
<type>short_text</type>
<type size="20">text</type>
</item>
<item id="web_root" config_section="general">
<name>Web Root</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="directory">text</type>
</item>
<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>
<type size="20">text</type>
</item>
<item id="logfile" config_section="general">
<name>Logfile</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="file">text</type>
</item>
<item id="runas" config_section="general">
<name>Run As</name>
<short_description></short_description>
<type>short_text</type>
<type size="20">text</type>
</item>
<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>
<type size="20">text</type>
</item>
<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>
<type size="20">text</type>
</item>
<item id="compress" config_section="general">
<name>Compress</name>
<short_description>
Should browsing data be compressed on the way to the client?
</short_description>
<type>select</type>
<option value="0">No</option>
<option value="1">Yes</option>
<default_value>0</default_value>
<type default_value="0">select</type>
<options>
<option value="0">No</option>
<option value="1">Yes</option>
</options>
</item>
<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.
</short_description>
<type>select</type>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<default_value>0</default_value>
<type default_value="0">select</type>
<options>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</options>
</item>
</section>
@ -83,34 +85,33 @@
<item id="mp3_dir" config_section="general">
<name>MP3 Directory</name>
<short_description></short_description>
<type>long_text_multiple</type>
<add_item_text>Add mp3 directory</add_item_text>
<type size="80" multiple="true" add_item_label="Add mp3 directory" browse="directory">text</type>
</item>
<item id="extensions" config_section="general">
<name>Extensions</name>
<short_description></short_description>
<type>short_text</type>
<type size="20">text</type>
</item>
<item id="playlist" config_section="general">
<name>Playlist File</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="file">text</type>
</item>
<item id="process_m3u" config_section="general">
<name>Process .m3u Files</name>
<short_description>
Should m3u files be processed as playlists?
</short_description>
<type>select</type>
<option value="0">No</option>
<option value="1">Yes</option>
<default_value>0</default_value>
<type default_value="0">select</type>
<options>
<option value="0">No</option>
<option value="1">Yes</option>
</options>
</item>
<item id="compdirs" config_section="general">
<name>Compilation Directories</name>
<short_description></short_description>
<type>long_text_multiple</type>
<add_item_text>Add compilation directory</add_item_text>
<type size="80" multiple="true" add_item_label="Add compilation directory" browse="directory">text</type>
</item>
</section>
@ -118,39 +119,42 @@
<item id="db_type" config_section="general">
<name>Database Type</name>
<short_description></short_description>
<type>select</type>
<option value="sqlite">sqlite</option>
<option value="sqlite3">sqlite3</option>
<default_value>sqlite</default_value>
<type default="sqlite">select</type>
<options>
<option value="sqlite">sqlite</option>
<option value="sqlite3">sqlite3</option>
</options>
</item>
<item id="db_parms" config_section="general">
<name>Database Directory</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="directory">text</type>
</item>
<item id="scan_type" config_section="general">
<name>Scan Type</name>
<short_description></short_description>
<type>select</type>
<option value="0">0 - Normal</option>
<option value="1">1 - Aggressive</option>
<option value="2">2 - Painfully aggressive</option>
<default_value>2</default_value>
<type default="2">select</type>
<options>
<option value="0">0 - Normal</option>
<option value="1">1 - Aggressive</option>
<option value="2">2 - Painfully aggressive</option>
</options>
</item>
<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>
<type size="20">text</type>
</item>
<item id="always_scan" config_section="general">
<name>Always Scan</name>
<short_description></short_description>
<type>select</type>
<option value="0">No</option>
<option value="1">Yes</option>
<default_value>0</default_value>
<type default="0">select</type>
<options>
<option value="0">No</option>
<option value="1">Yes</option>
</options>
</item>
</section>
@ -158,13 +162,12 @@
<item id="plugin_dir" config_section="plugins">
<name>Plugin Directory</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="directory">text</type>
</item>
<item id="plugins" config_section="plugins">
<name>Plugins</name>
<short_description></short_description>
<type>short_text_multiple</type>
<add_item_text>Add plugin</add_item_text>
<type size="20" multiple="true" add_item_label="Add plugin">text</type>
</item>
</section>
@ -172,12 +175,12 @@
<item id="ssc_prog" config_section="general">
<name>SSC Program</name>
<short_description></short_description>
<type>long_text</type>
<type size="80" browse="file">text</type>
</item>
<item id="ssc_codectypes" config_section="general">
<name>SSC Codec Types</name>
<short_description></short_description>
<type>long_text</type>
<type size="80">text</type>
</item>
</section>
</config>