2013-10-22 15:54:02 -04:00
|
|
|
define(['uploader'], function(uploader) {
|
2013-10-03 15:04:25 -04:00
|
|
|
var Settings = {};
|
|
|
|
|
|
|
|
|
|
Settings.init = function() {
|
|
|
|
|
Settings.prepare();
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-29 12:28:21 -05:00
|
|
|
Settings.prepare = function(callback) {
|
2013-10-13 14:30:39 -04:00
|
|
|
// Come back in 125ms if the config isn't ready yet
|
|
|
|
|
if (!app.config) {
|
2013-10-03 15:04:25 -04:00
|
|
|
setTimeout(function() {
|
|
|
|
|
Settings.prepare();
|
2013-10-13 14:30:39 -04:00
|
|
|
}, 125);
|
2013-10-03 15:04:25 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate the fields on the page from the config
|
|
|
|
|
var fields = document.querySelectorAll('#content [data-field]'),
|
|
|
|
|
numFields = fields.length,
|
|
|
|
|
saveBtn = document.getElementById('save'),
|
|
|
|
|
x, key, inputType;
|
2014-02-14 15:08:56 -05:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
for (x = 0; x < numFields; x++) {
|
|
|
|
|
key = fields[x].getAttribute('data-field');
|
|
|
|
|
inputType = fields[x].getAttribute('type');
|
|
|
|
|
if (fields[x].nodeName === 'INPUT') {
|
2013-10-13 14:30:39 -04:00
|
|
|
if (app.config[key]) {
|
2013-10-03 15:04:25 -04:00
|
|
|
switch (inputType) {
|
|
|
|
|
case 'text':
|
2014-01-14 12:25:27 -05:00
|
|
|
case 'password':
|
2013-10-03 15:04:25 -04:00
|
|
|
case 'textarea':
|
|
|
|
|
case 'number':
|
2013-10-13 14:30:39 -04:00
|
|
|
fields[x].value = app.config[key];
|
2013-10-03 15:04:25 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'checkbox':
|
2013-12-05 13:11:27 -05:00
|
|
|
fields[x].checked = parseInt(app.config[key], 10) === 1;
|
2013-10-03 15:04:25 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (fields[x].nodeName === 'TEXTAREA') {
|
2013-10-13 14:30:39 -04:00
|
|
|
if (app.config[key]) fields[x].value = app.config[key];
|
2013-11-25 17:48:55 -05:00
|
|
|
} else if (fields[x].nodeName === 'SELECT') {
|
|
|
|
|
if (app.config[key]) fields[x].value = app.config[key];
|
2013-10-03 15:04:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveBtn.addEventListener('click', function(e) {
|
2014-02-14 15:08:56 -05:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
for (x = 0; x < numFields; x++) {
|
2014-02-14 15:08:56 -05:00
|
|
|
saveField(fields[x]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-02-14 15:08:56 -05:00
|
|
|
function saveField(field) {
|
|
|
|
|
var key = field.getAttribute('data-field'),
|
|
|
|
|
value;
|
|
|
|
|
|
|
|
|
|
if (field.nodeName === 'INPUT') {
|
|
|
|
|
inputType = field.getAttribute('type');
|
|
|
|
|
switch (inputType) {
|
|
|
|
|
case 'text':
|
|
|
|
|
case 'password':
|
|
|
|
|
case 'textarea':
|
|
|
|
|
case 'number':
|
|
|
|
|
value = field.value;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'checkbox':
|
|
|
|
|
value = field.checked ? '1' : '0';
|
|
|
|
|
break;
|
2013-10-03 15:04:25 -04:00
|
|
|
}
|
2014-02-14 15:08:56 -05:00
|
|
|
} else if (field.nodeName === 'TEXTAREA') {
|
|
|
|
|
value = field.value;
|
|
|
|
|
} else if (field.nodeName === 'SELECT') {
|
|
|
|
|
value = field.value;
|
|
|
|
|
}
|
2013-10-03 15:04:25 -04:00
|
|
|
|
2014-02-14 15:08:56 -05:00
|
|
|
socket.emit('admin.config.set', {
|
|
|
|
|
key: key,
|
|
|
|
|
value: value
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return app.alert({
|
2014-01-16 18:06:19 -05:00
|
|
|
alert_id: 'config_status',
|
|
|
|
|
timeout: 2500,
|
2014-02-14 15:08:56 -05:00
|
|
|
title: 'Changes Not Saved',
|
|
|
|
|
message: 'NodeBB encountered a problem saving your changes',
|
|
|
|
|
type: 'danger'
|
2014-01-16 18:06:19 -05:00
|
|
|
});
|
2014-02-14 15:08:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(app.config[key] !== undefined) {
|
|
|
|
|
app.config[key] = value;
|
|
|
|
|
}
|
2014-01-16 18:06:19 -05:00
|
|
|
|
2014-02-14 15:08:56 -05:00
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'config_status',
|
|
|
|
|
timeout: 2500,
|
|
|
|
|
title: 'Changes Saved',
|
|
|
|
|
message: 'Your changes to the NodeBB configuration have been saved.',
|
|
|
|
|
type: 'success'
|
2013-10-03 15:04:25 -04:00
|
|
|
});
|
2014-02-14 15:08:56 -05:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-10-22 15:54:02 -04:00
|
|
|
|
|
|
|
|
$('#uploadLogoBtn').on('click', function() {
|
2014-02-11 17:16:17 -05:00
|
|
|
uploader.open(RELATIVE_PATH + '/admin/uploadlogo', {}, 0, function(image) {
|
2013-10-22 15:54:02 -04:00
|
|
|
$('#logoUrl').val(image);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploader.hideAlerts();
|
|
|
|
|
});
|
2013-12-09 12:52:12 -05:00
|
|
|
|
2013-12-09 13:01:57 -05:00
|
|
|
$('#uploadFaviconBtn').on('click', function() {
|
2014-02-11 17:16:17 -05:00
|
|
|
uploader.open(RELATIVE_PATH + '/admin/uploadfavicon', {}, 0, function(icon) {
|
2013-12-09 13:18:31 -05:00
|
|
|
$('#faviconUrl').val(icon);
|
2013-12-09 12:52:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
uploader.hideAlerts();
|
|
|
|
|
});
|
2014-01-09 17:51:26 -05:00
|
|
|
|
|
|
|
|
$('#settings-tab a').click(function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
$(this).tab('show');
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2014-01-29 12:28:21 -05:00
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2013-10-03 15:04:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Settings.remove = function(key) {
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('admin.config.remove', key);
|
2013-10-03 15:04:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Settings;
|
|
|
|
|
});
|