You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/public/src/modules/categorySelector.js

83 lines
2.3 KiB
JavaScript

8 years ago
'use strict';
6 years ago
define('categorySelector', ['benchpress', 'translator', 'categorySearch'], function (Benchpress, translator, categorySearch) {
8 years ago
var categorySelector = {};
categorySelector.init = function (el, callback) {
8 years ago
callback = callback || function () {};
var selector = {
el: el,
selectedCategory: null,
};
8 years ago
el.on('click', '[data-cid]', function () {
var categoryEl = $(this);
if (categoryEl.hasClass('disabled')) {
return false;
}
selector.selectCategory(categoryEl.attr('data-cid'));
callback(selector.selectedCategory);
8 years ago
});
6 years ago
6 years ago
categorySearch.init(el);
8 years ago
selector.selectCategory = function (cid) {
var categoryEl = selector.el.find('[data-cid="' + cid + '"]');
selector.selectedCategory = {
cid: cid,
name: categoryEl.attr('data-name'),
};
8 years ago
if (categoryEl.length) {
selector.el.find('[component="category-selector-selected"]').html(categoryEl.find('[component="category-markup"]').html());
} else {
selector.el.find('[component="category-selector-selected"]').translateHtml('[[topic:thread_tools.select_category]]');
}
};
selector.getSelectedCategory = function () {
return selector.selectedCategory;
};
selector.getSelectedCid = function () {
return selector.selectedCategory ? selector.selectedCategory.cid : 0;
8 years ago
};
return selector;
8 years ago
};
categorySelector.modal = function (categories, callback) {
if (typeof categories === 'function') {
callback = categories;
categories = ajaxify.data.allCategories;
}
Benchpress.parse('admin/partials/categories/select-category', {
categories: categories,
}, function (html) {
translator.translate(html, function (html) {
var modal = bootbox.dialog({
title: '[[modules:composer.select_category]]',
message: html,
buttons: {
save: {
label: '[[global:select]]',
className: 'btn-primary',
callback: submit,
},
},
});
var selector = categorySelector.init(modal.find('[component="category-selector"]'));
function submit(ev) {
ev.preventDefault();
if (selector.selectedCategory) {
callback(selector.selectedCategory.cid);
modal.modal('hide');
}
return false;
}
modal.find('form').on('submit', submit);
});
});
};
8 years ago
return categorySelector;
});