2014-01-31 13:17:28 -05:00
|
|
|
define(['forum/admin/settings'], function(Settings) {
|
2013-10-03 15:04:25 -04:00
|
|
|
var Themes = {};
|
2013-07-14 13:25:25 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
Themes.init = function() {
|
|
|
|
|
var scriptEl = document.createElement('script');
|
|
|
|
|
scriptEl.src = 'http://api.bootswatch.com/3/?callback=bootswatchListener';
|
|
|
|
|
document.body.appendChild(scriptEl);
|
2013-07-14 13:25:25 -04:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
var bootstrapThemeContainer = document.querySelector('#bootstrap_themes'),
|
|
|
|
|
installedThemeContainer = document.querySelector('#installed_themes'),
|
|
|
|
|
themeEvent = function(e) {
|
|
|
|
|
if (e.target.hasAttribute('data-action')) {
|
|
|
|
|
switch (e.target.getAttribute('data-action')) {
|
|
|
|
|
case 'use':
|
|
|
|
|
var parentEl = $(e.target).parents('li'),
|
2013-10-19 17:25:17 -04:00
|
|
|
themeType = parentEl.attr('data-type'),
|
2013-10-03 15:04:25 -04:00
|
|
|
cssSrc = parentEl.attr('data-css'),
|
2013-10-21 12:07:37 -04:00
|
|
|
themeId = parentEl.attr('data-theme');
|
|
|
|
|
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('admin.themes.set', {
|
2013-10-21 12:07:37 -04:00
|
|
|
type: themeType,
|
|
|
|
|
id: themeId,
|
|
|
|
|
src: cssSrc
|
|
|
|
|
}, function(err) {
|
|
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'admin:theme',
|
|
|
|
|
type: 'success',
|
|
|
|
|
title: 'Theme Changed',
|
|
|
|
|
message: 'You have successfully changed your NodeBB\'s theme. Please restart to see the changes.',
|
|
|
|
|
timeout: 2500
|
2013-10-19 17:25:17 -04:00
|
|
|
});
|
2013-10-21 12:07:37 -04:00
|
|
|
});
|
2013-10-19 17:25:17 -04:00
|
|
|
break;
|
2013-10-03 15:04:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-01-20 21:15:52 -05:00
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
bootstrapThemeContainer.addEventListener('click', themeEvent);
|
|
|
|
|
installedThemeContainer.addEventListener('click', themeEvent);
|
|
|
|
|
|
|
|
|
|
var revertEl = document.getElementById('revert_theme');
|
|
|
|
|
revertEl.addEventListener('click', function() {
|
|
|
|
|
bootbox.confirm('Are you sure you wish to remove the custom theme and restore the NodeBB default theme?', function(confirm) {
|
|
|
|
|
if (confirm) {
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('admin.themes.set', {
|
2013-10-21 12:10:23 -04:00
|
|
|
type: 'local',
|
|
|
|
|
id: 'nodebb-theme-cerulean'
|
|
|
|
|
}, function(err) {
|
|
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'admin:theme',
|
|
|
|
|
type: 'success',
|
|
|
|
|
title: 'Theme Changed',
|
|
|
|
|
message: 'You have successfully reverted your NodeBB back to it\'s default theme. Please restart to see the changes.',
|
|
|
|
|
timeout: 3500
|
|
|
|
|
});
|
2013-10-03 15:04:25 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
// Installed Themes
|
2014-01-17 12:42:19 -05:00
|
|
|
socket.emit('admin.themes.getInstalled', function(err, themes) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
var instListEl = document.getElementById('installed_themes'),
|
|
|
|
|
themeFrag = document.createDocumentFragment(),
|
|
|
|
|
liEl = document.createElement('li');
|
2013-10-19 17:25:17 -04:00
|
|
|
liEl.setAttribute('data-type', 'local');
|
2013-10-03 15:04:25 -04:00
|
|
|
|
|
|
|
|
if (themes.length > 0) {
|
|
|
|
|
for (var x = 0, numThemes = themes.length; x < numThemes; x++) {
|
|
|
|
|
liEl.setAttribute('data-theme', themes[x].id);
|
2013-11-01 11:27:02 -04:00
|
|
|
liEl.innerHTML = '<img src="' + (themes[x].screenshot ? '/css/previews/' + themes[x].id : RELATIVE_PATH + '/images/themes/default.png') + '" />' +
|
2013-10-03 15:04:25 -04:00
|
|
|
'<div>' +
|
|
|
|
|
'<div class="pull-right">' +
|
|
|
|
|
'<button class="btn btn-primary" data-action="use">Use</button> ' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'<h4>' + themes[x].name + '</h4>' +
|
|
|
|
|
'<p>' +
|
|
|
|
|
themes[x].description +
|
|
|
|
|
(themes[x].url ? ' (<a href="' + themes[x].url + '">Homepage</a>)' : '') +
|
|
|
|
|
'</p>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'<div class="clear">';
|
|
|
|
|
themeFrag.appendChild(liEl.cloneNode(true));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// No themes found
|
|
|
|
|
liEl.className = 'no-themes';
|
|
|
|
|
liEl.innerHTML = 'No installed themes found';
|
|
|
|
|
themeFrag.appendChild(liEl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instListEl.innerHTML = '';
|
|
|
|
|
instListEl.appendChild(themeFrag);
|
|
|
|
|
});
|
2014-01-31 13:17:28 -05:00
|
|
|
|
2014-02-01 22:38:52 -05:00
|
|
|
// Proper tabbing for "Custom CSS" field
|
|
|
|
|
var customCSSEl = $('textarea[data-field]')[0];
|
|
|
|
|
tabIndent.config.tab = ' ';
|
|
|
|
|
tabIndent.render(customCSSEl);
|
|
|
|
|
|
2014-02-19 12:46:55 -05:00
|
|
|
Themes.prepareWidgets();
|
|
|
|
|
|
2014-01-31 13:17:28 -05:00
|
|
|
Settings.prepare();
|
2013-10-03 15:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Themes.render = function(bootswatch) {
|
2013-07-14 13:25:25 -04:00
|
|
|
var themeFrag = document.createDocumentFragment(),
|
2013-06-06 15:34:12 -04:00
|
|
|
themeEl = document.createElement('li'),
|
2013-07-17 16:17:19 -04:00
|
|
|
themeContainer = document.querySelector('#bootstrap_themes'),
|
2013-06-06 15:34:12 -04:00
|
|
|
numThemes = bootswatch.themes.length;
|
|
|
|
|
|
2013-10-19 17:25:17 -04:00
|
|
|
themeEl.setAttribute('data-type', 'bootswatch');
|
|
|
|
|
|
2013-09-17 13:07:30 -04:00
|
|
|
for (var x = 0; x < numThemes; x++) {
|
2013-06-06 15:34:12 -04:00
|
|
|
var theme = bootswatch.themes[x];
|
2014-01-20 13:41:46 -05:00
|
|
|
themeEl.setAttribute('data-css', theme.cssCdn);
|
2013-06-06 15:34:12 -04:00
|
|
|
themeEl.setAttribute('data-theme', theme.name);
|
2013-09-17 13:07:30 -04:00
|
|
|
themeEl.innerHTML = '<img src="' + theme.thumbnail + '" />' +
|
|
|
|
|
'<div>' +
|
|
|
|
|
'<div class="pull-right">' +
|
|
|
|
|
'<button class="btn btn-primary" data-action="use">Use</button> ' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'<h4>' + theme.name + '</h4>' +
|
|
|
|
|
'<p>' + theme.description + '</p>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'<div class="clear">';
|
2013-06-06 15:34:12 -04:00
|
|
|
themeFrag.appendChild(themeEl.cloneNode(true));
|
|
|
|
|
}
|
|
|
|
|
themeContainer.innerHTML = '';
|
|
|
|
|
themeContainer.appendChild(themeFrag);
|
|
|
|
|
}
|
2013-08-28 03:04:57 +08:00
|
|
|
|
2014-02-19 12:46:55 -05:00
|
|
|
Themes.prepareWidgets = function() {
|
|
|
|
|
$('#widgets .panel').draggable({
|
|
|
|
|
helper: function(e) {
|
|
|
|
|
return $(e.target).parents('.panel').clone().addClass('block').width($(e.target.parentNode).width());
|
|
|
|
|
},
|
|
|
|
|
connectToSortable: ".widget-area"
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-19 15:07:38 -05:00
|
|
|
function appendToggle(el) {
|
|
|
|
|
if (!el.hasClass('block')) {
|
|
|
|
|
el.addClass('block')
|
|
|
|
|
.children('.panel-heading')
|
|
|
|
|
.append('<div class="pull-right pointer"><span class="delete-widget"><i class="fa fa-times-circle"></i></span> <span class="toggle-widget"><i class="fa fa-chevron-down"></i></span></div>');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 12:46:55 -05:00
|
|
|
$('#widgets .widget-area').sortable({
|
|
|
|
|
update: function (event, ui) {
|
2014-02-19 15:07:38 -05:00
|
|
|
appendToggle(ui.item);
|
|
|
|
|
},
|
|
|
|
|
connectWith: "div"
|
2014-02-19 12:46:55 -05:00
|
|
|
}).on('click', '.toggle-widget', function() {
|
|
|
|
|
$(this).parents('.panel').children('.panel-body').toggleClass('hidden');
|
2014-02-19 15:07:38 -05:00
|
|
|
}).on('click', '.delete-widget', function() {
|
|
|
|
|
var panel = $(this).parents('.panel');
|
2014-02-19 15:43:05 -05:00
|
|
|
|
2014-02-19 15:07:38 -05:00
|
|
|
bootbox.confirm('Are you sure you wish to delete this widget?', function(confirm) {
|
|
|
|
|
if (confirm) {
|
|
|
|
|
panel.remove();
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-02-19 12:46:55 -05:00
|
|
|
});
|
2014-02-19 13:22:02 -05:00
|
|
|
|
|
|
|
|
$('#widgets .btn[data-template]').on('click', function() {
|
|
|
|
|
var btn = $(this),
|
|
|
|
|
template = btn.attr('data-template'),
|
|
|
|
|
location = btn.attr('data-location'),
|
|
|
|
|
area = btn.parents('.area').children('.widget-area'),
|
|
|
|
|
widgets = [];
|
|
|
|
|
|
|
|
|
|
area.find('.panel[data-widget]').each(function() {
|
2014-02-19 15:07:38 -05:00
|
|
|
var widgetData = {},
|
|
|
|
|
data = $(this).find('form').serializeArray();
|
|
|
|
|
|
|
|
|
|
for (var d in data) {
|
|
|
|
|
if (data.hasOwnProperty(d)) {
|
|
|
|
|
if (data[d].name) {
|
|
|
|
|
widgetData[data[d].name] = data[d].value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgets.push({
|
|
|
|
|
widget: this.getAttribute('data-widget'),
|
|
|
|
|
data: widgetData
|
|
|
|
|
});
|
2014-02-19 13:22:02 -05:00
|
|
|
});
|
|
|
|
|
|
2014-02-19 15:07:38 -05:00
|
|
|
socket.emit('admin.widgets.set', {
|
2014-02-19 13:22:02 -05:00
|
|
|
template: template,
|
|
|
|
|
location: location,
|
|
|
|
|
widgets: widgets
|
|
|
|
|
}, function(err) {
|
|
|
|
|
app.alert({
|
|
|
|
|
alert_id: 'admin:widgets',
|
|
|
|
|
type: err ? 'danger' : 'success',
|
|
|
|
|
title: err ? 'Error' : 'Widgets Updated',
|
|
|
|
|
message: err ? err : 'Successfully updated widgets',
|
|
|
|
|
timeout: 2500
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-19 15:07:38 -05:00
|
|
|
|
|
|
|
|
function populateWidget(widget, data) {
|
|
|
|
|
widget.find('input, textarea').each(function() {
|
|
|
|
|
var input = $(this),
|
|
|
|
|
value = data[input.attr('name')];
|
|
|
|
|
|
|
|
|
|
if (this.type === 'checkbox') {
|
|
|
|
|
input.attr('checked', !!value);
|
|
|
|
|
} else {
|
|
|
|
|
input.val(value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
|
}
|
2014-02-19 12:46:55 -05:00
|
|
|
|
2014-02-19 15:07:38 -05:00
|
|
|
$.get(RELATIVE_PATH + '/api/admin/themes', function(data) {
|
|
|
|
|
var areas = data.areas;
|
|
|
|
|
|
|
|
|
|
for (var a in areas) {
|
|
|
|
|
if (areas.hasOwnProperty(a)) {
|
|
|
|
|
var area = areas[a],
|
|
|
|
|
widgetArea = $('#widgets .area [data-template="' + area.template + '"][data-location="' + area.location + '"]').parents('.area').find('.widget-area');
|
|
|
|
|
|
|
|
|
|
for (var i in area.data) {
|
|
|
|
|
if (area.data.hasOwnProperty(i)) {
|
|
|
|
|
var data = area.data[i],
|
|
|
|
|
widgetEl = $('.available-widgets [data-widget="' + data.widget + '"]').clone();
|
|
|
|
|
|
|
|
|
|
widgetArea.append(populateWidget(widgetEl, data.data));
|
|
|
|
|
appendToggle(widgetEl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-02-19 12:46:55 -05:00
|
|
|
};
|
|
|
|
|
|
2013-10-03 15:04:25 -04:00
|
|
|
return Themes;
|
|
|
|
|
});
|