Skip to content

Node.js based forum software built for the modern web

rewrote a bit of the code so that templates.js is called asynchronously, and moved indentation levels to CSS instead of inlined styling.

Julian Lam478850ac05ad
diff --git a/public/less/admin/manage/categories.less b/public/less/admin/manage/categories.less
index 020e618382..a1238f9f23 100644
--- a/public/less/admin/manage/categories.less
+++ b/public/less/admin/manage/categories.less
@@ -1,9 +1,14 @@
div.categories {
ul {
+ .no-select;
list-style-type: none;
margin: 0;
padding: 0;
+
+ > li > ul > li {
+ margin-left: 4.5rem;
+ }
}
.fa-ul {
diff --git a/public/src/admin/manage/categories.js b/public/src/admin/manage/categories.js
index bf087b3e8c..27882cf1fb 100644
--- a/public/src/admin/manage/categories.js
+++ b/public/src/admin/manage/categories.js
@@ -1,23 +1,29 @@
"use strict";
-/*global define, socket, app, bootbox, templates, ajaxify, RELATIVE_PATH*/
+/*global define, socket, app, bootbox, templates, ajaxify, RELATIVE_PATH, Sortable */
define('admin/manage/categories', function() {
var Categories = {}, newCategoryId = -1, sortables, itemTemplate;
Categories.init = function() {
- $.get(RELATIVE_PATH + '/templates/admin/partials/categories/category-item.tpl', function(data){
- itemTemplate = data;
-
- socket.emit('admin.categories.getAll', function(error, payload){
- if(error){
- return app.alertError(error.message);
- }
+ socket.emit('admin.categories.getAll', function(error, payload){
+ if(error){
+ return app.alertError(error.message);
+ }
- Categories.render(payload);
- });
+ Categories.render(payload);
});
$('button[data-action="create"]').on('click', Categories.create);
+
+ // Enable/Disable toggle events
+ $('.categories').on('click', 'button[data-action="toggle"]', function() {
+ var self = $(this),
+ rowEl = self.parents('li'),
+ cid = rowEl.attr('data-cid'),
+ disabled = rowEl.hasClass('disabled');
+
+ Categories.toggle(cid, disabled);
+ });
};
Categories.create = function() {
@@ -58,10 +64,26 @@ define('admin/manage/categories', function() {
.appendTo(container);
}else{
sortables = {};
- renderList(categories, 0, container, 0);
+ renderList(categories, container, 0);
}
};
+ Categories.toggle = function(cid, state) {
+ var payload = {};
+
+ payload[cid] = {
+ disabled: !state | 0
+ };
+
+ socket.emit('admin.categories.update', payload, function(err, result) {
+ if (err) {
+ return app.alertError(err.message);
+ } else {
+ ajaxify.refresh();
+ }
+ });
+ }
+
function itemDidAdd(e){
newCategoryId = e.to.dataset.cid;
}
@@ -96,83 +118,31 @@ define('admin/manage/categories', function() {
* @param container {object} parent jquery element for the list
* @param parentId {number} parent category identifier
*/
- function renderList(categories, level, container, parentId){
- var i = 0, len = categories.length, category, marginLeft = 48, listItem;
- var list = $('<ul />', {'data-cid': parentId});
-
- if(level > 0){
- list.css('margin-left', marginLeft);
- }
-
- for(i; i < len; ++i){
- category = categories[i];
-
- listItem = $('<li></li>', {'data-cid': category.cid})
- .append(renderListItem(category))
- .appendTo(list);
-
- if(category.disabled){
- listItem.addClass('disabled');
- }
-
- if(category.children.length > 0){
- renderList(category.children, level + 1, listItem, category.cid);
- }
- }
-
- list.appendTo(container);
- sortables[parentId] = Sortable.create(list[0], {
- group: 'cross-categories',
- animation: 150,
- handle: '.icon',
- dataIdAttr: 'data-cid',
- ghostClass: "placeholder",
- onAdd: itemDidAdd,
- onEnd: itemDragDidEnd
- });
- }
-
- function renderListItem(categoryEntity){
- var listItem = $(templates.parse(
- itemTemplate,
- categoryEntity
- ));
-
- var icon = listItem.find('.icon'),
- button = listItem.find('[data-action="toggle"]');
-
- if(categoryEntity.backgroundImage){
- icon.css('background-image', 'url(' + categoryEntity.backgroundImage + ')');
- }
-
- icon
- .css('color', categoryEntity.color)
- .css('background-color', categoryEntity.bgColor);
-
- if(categoryEntity.disabled){
- button.text('Enable').addClass('btn-success');
- }else{
- button.text('Disable').addClass('btn-danger');
- }
-
- // Category enable/disable
- button.on('click', function(e) {
- var payload = {};
-
- payload[categoryEntity.cid] = {
- disabled: !categoryEntity.disabled | 0
- };
-
- socket.emit('admin.categories.update', payload, function(err, result) {
- if (err) {
- return app.alertError(err.message);
- } else {
- ajaxify.refresh();
+ function renderList(categories, container, parentId){
+ templates.parse('admin/partials/categories/category-rows', {
+ cid: parentId,
+ categories: categories
+ }, function(html) {
+ container.append(html);
+
+ // Handle and children categories in this level have
+ for(var x=0,numCategories=categories.length;x<numCategories;x++) {
+ if (categories[x].hasOwnProperty('children') && categories[x].children.length > 0) {
+ renderList(categories[x].children, $('li[data-cid="' + categories[x].cid + '"]'), categories[x].cid);
}
+ }
+
+ // Make list sortable
+ sortables[parentId] = Sortable.create($('ul[data-cid="' + parentId + '"]')[0], {
+ group: 'cross-categories',
+ animation: 150,
+ handle: '.icon',
+ dataIdAttr: 'data-cid',
+ ghostClass: "placeholder",
+ onAdd: itemDidAdd,
+ onEnd: itemDragDidEnd
});
});
-
- return listItem;
}
return Categories;
diff --git a/src/views/admin/partials/categories/category-item.tpl b/src/views/admin/partials/categories/category-item.tpl
deleted file mode 100644
index 579c2156f9..0000000000
--- a/src/views/admin/partials/categories/category-item.tpl
+++ /dev/null
@@ -1,26 +0,0 @@
-<div class="row">
- <div class="col-md-9">
- <div class="clearfix">
- <div class="icon">
- <i data-name="icon" value="{icon}" class="fa {icon}"></i>
- </div>
- <div class="information">
- <h5 class="header">{name}</h5>
-
- <p class="description">{description}</p>
- </div>
- </div>
- </div>
- <div class="col-md-3">
- <div class="clearfix pull-right">
- <ul class="fa-ul stats">
- <li class="fa-li"><i class="fa fa-book"></i> {topic_count}</li>
- <li class="fa-li"><i class="fa fa-pencil"></i> {post_count}</li>
- </ul>
- <div class="btn-group">
- <button data-action="toggle" data-disabled="{disabled}" class="btn btn-xs"></button>
- <a href="./categories/{cid}" class="btn btn-default btn-xs">Edit</a>
- </div>
- </div>
- </div>
-</div>
\ No newline at end of file
diff --git a/src/views/admin/partials/categories/category-rows.tpl b/src/views/admin/partials/categories/category-rows.tpl
new file mode 100644
index 0000000000..b30059783d
--- /dev/null
+++ b/src/views/admin/partials/categories/category-rows.tpl
@@ -0,0 +1,33 @@
+<ul data-cid="{cid}">
+<!-- BEGIN categories -->
+ <li data-cid="{categories.cid}" <!-- IF categories.disabled -->class="disabled"<!-- ENDIF categories.disabled -->>
+ <div class="row">
+ <div class="col-md-9">
+ <div class="clearfix">
+ <div class="icon" style="color: {categories.color}; background-color: {categories.bgColor};<!-- IF categories.backgroundImage --> background-image: url('{categories.backgroundImage}');<!-- ENDIF categories.backgroundImage -->">
+ <i data-name="icon" value="{categories.icon}" class="fa {categories.icon}"></i>
+ </div>
+ <div class="information">
+ <h5 class="header">{categories.name}</h5>
+ <p class="description">{categories.description}</p>
+ </div>
+ </div>
+ </div>
+ <div class="col-md-3">
+ <div class="clearfix pull-right">
+ <ul class="fa-ul stats">
+ <li class="fa-li"><i class="fa fa-book"></i> {categories.topic_count}</li>
+ <li class="fa-li"><i class="fa fa-pencil"></i> {categories.post_count}</li>
+ </ul>
+ <div class="btn-group">
+ <button data-action="toggle" data-disabled="{categories.disabled}" class="btn btn-xs <!-- IF categories.disabled -->btn-primary<!-- ELSE -->btn-danger<!-- ENDIF categories.disabled -->">
+ <!-- IF categories.disabled -->Enable<!-- ELSE -->Disable<!-- ENDIF categories.disabled -->
+ </button>
+ <a href="./categories/{categories.cid}" class="btn btn-default btn-xs">Edit</a>
+ </div>
+ </div>
+ </div>
+ </div>
+ </li>
+<!-- END categories -->
+</ul>
\ No newline at end of file