2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2017-02-17 21:55:19 -07:00
|
|
|
|
2014-12-30 13:04:31 -05:00
|
|
|
define('admin/manage/category', [
|
|
|
|
|
'uploader',
|
2015-03-25 19:29:24 -04:00
|
|
|
'iconSelect',
|
2015-03-25 15:42:15 -04:00
|
|
|
'admin/modules/colorpicker',
|
2017-05-30 17:21:30 -04:00
|
|
|
'categorySelector',
|
2018-04-11 16:38:54 -04:00
|
|
|
'benchpress',
|
2019-04-02 11:56:03 -04:00
|
|
|
], function (uploader, iconSelect, colorpicker, categorySelector, Benchpress) {
|
2014-12-30 13:04:31 -05:00
|
|
|
var Category = {};
|
2016-12-09 18:53:08 +03:00
|
|
|
var modified_categories = {};
|
2014-12-30 13:04:31 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Category.init = function () {
|
2017-05-29 14:47:01 -04:00
|
|
|
$('#category-settings select').each(function () {
|
2014-12-30 13:04:31 -05:00
|
|
|
var $this = $(this);
|
|
|
|
|
$this.val($this.attr('data-value'));
|
|
|
|
|
});
|
|
|
|
|
|
2017-05-29 14:47:01 -04:00
|
|
|
$('#category-selector').on('change', function () {
|
2017-12-20 14:49:20 -05:00
|
|
|
ajaxify.go('admin/manage/categories/' + $(this).val() + window.location.hash);
|
2017-05-29 14:47:01 -04:00
|
|
|
});
|
|
|
|
|
|
2014-12-30 13:04:31 -05:00
|
|
|
function enableColorPicker(idx, inputEl) {
|
2017-02-17 20:20:42 -07:00
|
|
|
var $inputEl = $(inputEl);
|
|
|
|
|
var previewEl = $inputEl.parents('[data-cid]').find('.category-preview');
|
2014-12-30 13:04:31 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
colorpicker.enable($inputEl, function (hsb, hex) {
|
2014-12-30 13:04:31 -05:00
|
|
|
if ($inputEl.attr('data-name') === 'bgColor') {
|
2015-07-22 04:52:54 -04:00
|
|
|
previewEl.css('background-color', '#' + hex);
|
2014-12-30 13:04:31 -05:00
|
|
|
} else if ($inputEl.attr('data-name') === 'color') {
|
|
|
|
|
previewEl.css('color', '#' + hex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modified($inputEl[0]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 13:03:34 -04:00
|
|
|
handleTags();
|
2015-09-20 15:38:54 -04:00
|
|
|
|
2019-01-03 13:08:42 -05:00
|
|
|
$('#category-settings input, #category-settings select').on('change', function (ev) {
|
|
|
|
|
modified(ev.target);
|
|
|
|
|
});
|
2014-12-30 13:04:31 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('[data-name="imageClass"]').on('change', function () {
|
2015-07-22 04:52:54 -04:00
|
|
|
$('.category-preview').css('background-size', $(this).val());
|
|
|
|
|
});
|
|
|
|
|
|
2015-01-01 15:35:22 -05:00
|
|
|
$('[data-name="bgColor"], [data-name="color"]').each(enableColorPicker);
|
2014-12-30 13:04:31 -05:00
|
|
|
|
2016-12-09 18:53:08 +03:00
|
|
|
$('#save').on('click', function () {
|
|
|
|
|
if (Object.keys(modified_categories).length) {
|
|
|
|
|
socket.emit('admin.categories.update', modified_categories, function (err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result && result.length) {
|
|
|
|
|
app.flags._unsaved = false;
|
|
|
|
|
app.alert({
|
|
|
|
|
title: 'Updated Categories',
|
|
|
|
|
message: 'Category IDs ' + result.join(', ') + ' was successfully updated.',
|
|
|
|
|
type: 'success',
|
2017-02-17 19:31:21 -07:00
|
|
|
timeout: 2000,
|
2016-12-09 18:53:08 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
modified_categories = {};
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('.purge').on('click', function (e) {
|
2015-03-25 17:51:46 -04:00
|
|
|
e.preventDefault();
|
2014-12-30 13:04:31 -05:00
|
|
|
|
2018-04-11 16:38:54 -04:00
|
|
|
Benchpress.parse('admin/partials/categories/purge', {
|
|
|
|
|
name: ajaxify.data.category.name,
|
|
|
|
|
topic_count: ajaxify.data.category.topic_count,
|
|
|
|
|
}, function (html) {
|
|
|
|
|
var modal = bootbox.dialog({
|
|
|
|
|
title: '[[admin/manage/categories:purge]]',
|
|
|
|
|
message: html,
|
|
|
|
|
size: 'large',
|
|
|
|
|
buttons: {
|
|
|
|
|
save: {
|
|
|
|
|
label: '[[modules:bootbox.confirm]]',
|
|
|
|
|
className: 'btn-primary',
|
|
|
|
|
callback: function () {
|
|
|
|
|
modal.find('.modal-footer button').prop('disabled', true);
|
|
|
|
|
|
|
|
|
|
var intervalId = setInterval(function () {
|
|
|
|
|
socket.emit('categories.getTopicCount', ajaxify.data.category.cid, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var percent = 0;
|
|
|
|
|
if (ajaxify.data.category.topic_count > 0) {
|
|
|
|
|
percent = Math.max(0, (1 - (count / ajaxify.data.category.topic_count))) * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modal.find('.progress-bar').css({ width: percent + '%' });
|
|
|
|
|
});
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
socket.emit('admin.categories.purge', ajaxify.data.category.cid, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (intervalId) {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
}
|
|
|
|
|
modal.modal('hide');
|
|
|
|
|
app.alertSuccess('[[admin/manage/categories:alert.purge-success]]');
|
|
|
|
|
ajaxify.go('admin/manage/categories');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2014-12-30 13:04:31 -05:00
|
|
|
});
|
|
|
|
|
});
|
2015-01-01 15:35:22 -05:00
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('.copy-settings').on('click', function () {
|
2019-04-02 11:56:03 -04:00
|
|
|
Benchpress.parse('admin/partials/categories/copy-settings', {
|
|
|
|
|
categories: ajaxify.data.allCategories,
|
|
|
|
|
}, function (html) {
|
|
|
|
|
var selectedCid;
|
|
|
|
|
var modal = bootbox.dialog({
|
|
|
|
|
title: '[[modules:composer.select_category]]',
|
|
|
|
|
message: html,
|
|
|
|
|
buttons: {
|
|
|
|
|
save: {
|
|
|
|
|
label: '[[modules:bootbox.confirm]]',
|
|
|
|
|
className: 'btn-primary',
|
|
|
|
|
callback: function () {
|
|
|
|
|
if (!selectedCid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.emit('admin.categories.copySettingsFrom', {
|
|
|
|
|
fromCid: selectedCid,
|
|
|
|
|
toCid: ajaxify.data.category.cid,
|
|
|
|
|
copyParent: modal.find('#copyParent').prop('checked'),
|
|
|
|
|
}, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modal.modal('hide');
|
|
|
|
|
app.alertSuccess('[[admin/manage/categories:alert.copy-success]]');
|
|
|
|
|
ajaxify.refresh();
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
modal.find('.modal-footer button').prop('disabled', true);
|
|
|
|
|
categorySelector.init(modal.find('[component="category-selector"]'), function (selectedCategory) {
|
|
|
|
|
selectedCid = selectedCategory && selectedCategory.cid;
|
|
|
|
|
if (selectedCid) {
|
|
|
|
|
modal.find('.modal-footer button').prop('disabled', false);
|
2016-04-01 15:02:17 +03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-05-23 12:59:33 +03:00
|
|
|
return false;
|
2016-04-01 15:02:17 +03:00
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('.upload-button').on('click', function () {
|
2016-03-24 16:15:22 +02:00
|
|
|
var inputEl = $(this);
|
|
|
|
|
var cid = inputEl.attr('data-cid');
|
2015-01-01 15:35:22 -05:00
|
|
|
|
2016-02-29 14:23:12 +02:00
|
|
|
uploader.show({
|
2016-12-09 20:47:28 -07:00
|
|
|
title: '[[admin/manage/categories:alert.upload-image]]',
|
2016-02-29 14:23:12 +02:00
|
|
|
route: config.relative_path + '/api/admin/category/uploadpicture',
|
2017-02-18 12:30:49 -07:00
|
|
|
params: { cid: cid },
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (imageUrlOnServer) {
|
2016-03-24 16:15:22 +02:00
|
|
|
$('#category-image').val(imageUrlOnServer);
|
2015-03-25 15:42:15 -04:00
|
|
|
var previewBox = inputEl.parent().parent().siblings('.category-preview');
|
2015-07-22 04:52:54 -04:00
|
|
|
previewBox.css('background', 'url(' + imageUrlOnServer + '?' + new Date().getTime() + ')');
|
2016-03-24 16:15:22 +02:00
|
|
|
|
|
|
|
|
modified($('#category-image'));
|
2015-01-01 15:35:22 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('#category-image').on('change', function () {
|
2016-03-24 16:15:22 +02:00
|
|
|
$('.category-preview').css('background-image', $(this).val() ? ('url("' + $(this).val() + '")') : '');
|
2019-03-27 18:21:35 -04:00
|
|
|
modified($('#category-image'));
|
2016-03-24 16:15:22 +02:00
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('.delete-image').on('click', function (e) {
|
2015-03-27 15:43:37 +03:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
2016-03-24 16:15:22 +02:00
|
|
|
var inputEl = $('#category-image');
|
|
|
|
|
var previewBox = $('.category-preview');
|
2015-03-27 15:43:37 +03:00
|
|
|
|
|
|
|
|
inputEl.val('');
|
|
|
|
|
previewBox.css('background-image', '');
|
|
|
|
|
modified(inputEl[0]);
|
2015-03-27 15:58:13 +03:00
|
|
|
$(this).parent().addClass('hide').hide();
|
2015-03-27 15:43:37 +03:00
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('.category-preview').on('click', function () {
|
2015-01-01 16:24:47 -05:00
|
|
|
iconSelect.init($(this).find('i'), modified);
|
|
|
|
|
});
|
|
|
|
|
|
2017-05-29 17:12:02 -04:00
|
|
|
$('[type="checkbox"]').on('change', function () {
|
|
|
|
|
modified($(this));
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-07 17:23:41 -04:00
|
|
|
$('button[data-action="setParent"], button[data-action="changeParent"]').on('click', Category.launchParentSelector);
|
2016-10-13 11:43:39 +02:00
|
|
|
$('button[data-action="removeParent"]').on('click', function () {
|
2016-10-13 11:42:29 +02:00
|
|
|
var payload = {};
|
2015-07-06 14:33:43 -04:00
|
|
|
payload[ajaxify.data.category.cid] = {
|
2017-02-17 19:31:21 -07:00
|
|
|
parentCid: 0,
|
2015-03-25 17:21:38 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
socket.emit('admin.categories.update', payload, function (err) {
|
2015-03-25 17:21:38 -04:00
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
2015-07-07 17:23:41 -04:00
|
|
|
$('button[data-action="removeParent"]').parent().addClass('hide');
|
2015-09-20 15:38:54 -04:00
|
|
|
$('button[data-action="changeParent"]').parent().addClass('hide');
|
2015-07-07 17:23:41 -04:00
|
|
|
$('button[data-action="setParent"]').removeClass('hide');
|
2015-03-25 17:21:38 -04:00
|
|
|
});
|
|
|
|
|
});
|
2020-01-14 23:30:56 +01:00
|
|
|
$('button[data-action="toggle"]').on('click', function () {
|
|
|
|
|
var payload = {};
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
var disabled = $this.attr('data-disabled') === '1';
|
|
|
|
|
payload[ajaxify.data.category.cid] = {
|
|
|
|
|
disabled: disabled ? 0 : 1,
|
|
|
|
|
};
|
|
|
|
|
socket.emit('admin.categories.update', payload, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
$this.translateText(!disabled ? '[[admin/manage/categories:enable]]' : '[[admin/manage/categories:disable]]');
|
|
|
|
|
$this.toggleClass('btn-primary', !disabled).toggleClass('btn-danger', disabled);
|
|
|
|
|
$this.attr('data-disabled', disabled ? 0 : 1);
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-12-30 13:04:31 -05:00
|
|
|
};
|
|
|
|
|
|
2016-12-09 18:53:08 +03:00
|
|
|
function modified(el) {
|
|
|
|
|
var cid = ajaxify.data.category.cid;
|
|
|
|
|
|
|
|
|
|
if (cid) {
|
2017-05-29 17:12:02 -04:00
|
|
|
var value;
|
|
|
|
|
if ($(el).is(':checkbox')) {
|
|
|
|
|
value = $(el).is(':checked') ? 1 : 0;
|
|
|
|
|
} else {
|
|
|
|
|
value = $(el).val();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 18:53:08 +03:00
|
|
|
modified_categories[cid] = modified_categories[cid] || {};
|
2017-05-29 17:12:02 -04:00
|
|
|
modified_categories[cid][$(el).attr('data-name')] = value;
|
2016-12-09 18:53:08 +03:00
|
|
|
|
|
|
|
|
app.flags = app.flags || {};
|
|
|
|
|
app.flags._unsaved = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleTags() {
|
|
|
|
|
var tagEl = $('#tag-whitelist');
|
|
|
|
|
tagEl.tagsinput({
|
|
|
|
|
confirmKeys: [13, 44],
|
2017-02-17 19:31:21 -07:00
|
|
|
trimValue: true,
|
2016-12-09 18:53:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ajaxify.data.category.tagWhitelist.forEach(function (tag) {
|
|
|
|
|
tagEl.tagsinput('add', tag);
|
|
|
|
|
});
|
2017-08-02 13:03:34 -04:00
|
|
|
|
2017-02-18 15:05:36 -07:00
|
|
|
tagEl.on('itemAdded itemRemoved', function () {
|
2016-12-09 18:53:08 +03:00
|
|
|
modified(tagEl);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Category.launchParentSelector = function () {
|
2018-11-06 13:34:29 -05:00
|
|
|
var parents = [parseInt(ajaxify.data.category.cid, 10)];
|
2017-05-30 14:10:12 -04:00
|
|
|
var categories = ajaxify.data.allCategories.filter(function (category) {
|
2018-11-06 13:34:29 -05:00
|
|
|
var isChild = parents.includes(parseInt(category.parentCid, 10));
|
|
|
|
|
if (isChild) {
|
|
|
|
|
parents.push(parseInt(category.cid, 10));
|
|
|
|
|
}
|
|
|
|
|
return category && !category.disabled && parseInt(category.cid, 10) !== parseInt(ajaxify.data.category.cid, 10) && !isChild;
|
2017-05-30 14:10:12 -04:00
|
|
|
});
|
2015-03-25 17:21:38 -04:00
|
|
|
|
2017-12-20 14:49:20 -05:00
|
|
|
categorySelector.modal(categories, function (parentCid) {
|
2017-05-30 14:10:12 -04:00
|
|
|
var payload = {};
|
2016-12-09 20:47:28 -07:00
|
|
|
|
2017-05-30 14:10:12 -04:00
|
|
|
payload[ajaxify.data.category.cid] = {
|
|
|
|
|
parentCid: parentCid,
|
|
|
|
|
};
|
2016-12-09 20:47:28 -07:00
|
|
|
|
2017-05-30 14:10:12 -04:00
|
|
|
socket.emit('admin.categories.update', payload, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
var parent = ajaxify.data.allCategories.filter(function (category) {
|
|
|
|
|
return category && parseInt(category.cid, 10) === parseInt(parentCid, 10);
|
2015-03-25 17:21:38 -04:00
|
|
|
});
|
2017-05-30 14:10:12 -04:00
|
|
|
parent = parent[0];
|
|
|
|
|
|
|
|
|
|
$('button[data-action="removeParent"]').parent().removeClass('hide');
|
|
|
|
|
$('button[data-action="setParent"]').addClass('hide');
|
|
|
|
|
var buttonHtml = '<i class="fa ' + parent.icon + '"></i> ' + parent.name;
|
|
|
|
|
$('button[data-action="changeParent"]').html(buttonHtml).parent().removeClass('hide');
|
2015-03-25 17:21:38 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-30 13:04:31 -05:00
|
|
|
return Category;
|
2017-02-18 02:30:48 -07:00
|
|
|
});
|