index.js
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
'use strict';
var $ = require('jquery'),
common = require('../common/common');
var renderHd = false;
var g = new common.grid({
el: "#basicTable",
columns: [{
display: '',
name: 'foler',
render: function(item) {
var html = "", top = true;
if (!renderHd) {
html += $('#tableHdTemp').html();
init(item);
renderHd = true;
} else {init(item)}
return html;
function init(item){
if(top){
html += "<div id='category_id_"+ item.categoryId +"' class='level-wrap clearfix'>";
top = false;
}else{
html += "<div id='category_id_"+ item.categoryId +"' class='level-wrap clearfix' style='display: none'>"
}
html += common.util.__template2($('#tableTemp2').html(), item);
if(item.items){
for(var i=0; i<item.items.length; i++){
init(item.items[i]);
}
}
html += "</div>";
}
}
}
]
});
g.init($("#gridurl").val());
//编辑排序
$(document).on('change', '.saleCategoryNumInput', function() {
if(checkNumInput($(this))){
$(this).removeClass("errorNumInput");
var categoryId = $(this).attr("categoryId");
var orderBy = $(this).val();
savePosition(categoryId,orderBy);
}
});
//排序框校验
function checkNumInput(inputObj){
if(inputObj.val()){
if(!isNaN(inputObj.val())){
if(inputObj.val() < 0){
inputObj.focus().select().addClass("errorNumInput");
common.util.__tip("排序数字请大于等于0!");
return false;
}
}else{
inputObj.focus().select().addClass("errorNumInput");
common.util.__tip("排序必须为数字!");
return false;
}
}else{
inputObj.focus().addClass("errorNumInput");
common.util.__tip("排序不能为空!");
return false;
}
return true;
}
//排序实时保存
function savePosition(categoryId, orderBy){
common.util.__ajax({
url:'/sale/salesCategory/updateSC',
data: {
"categoryId": categoryId,
"orderBy": orderBy
}
},function(rs){
common.util.__tip("排序保存成功!","success");
},true);
}
//打开层级类目
$(document).on('click', '.folder', function() {
var $wrap = $(this).parent();
if (!$(this).hasClass('open')) {
if ($wrap.find('> .level-wrap').size() > 0) {
var position = $(this).css("background-position");
var a = position.substring(0,position.indexOf("px"));
$wrap.find('> .level-wrap').show().find(".folder").css("background-position", (parseInt(a)+36) + "px");
$(this).addClass('open');
}
} else {
$(this).removeClass('open');
if ($wrap.find('.level-wrap').size() > 0) {
$wrap.find('.level-wrap').hide().find(".folder").removeClass('open').css("background-position", "12px center");
}
}
});
var e = null,
tabTree = null,
tabTreeArr = [],
editHtml = $('#editTemp').html();
function initDialog(){
common.dialog.confirm(
'销售类目编辑',
"<div class='saleCategoryEdit'>加载中...</div>",
function(){
if(checkForm()){
saveBrand()
}else{
return false;
}
}, function(){}
);
$(".modal-dialog").css("width","900px");
}
function initForm(data) {
var parentName = "";
getParentsName(data.parentSC);
data.parentName = (parentName == ""?"(无根目录)":parentName);
$(".saleCategoryEdit").html(common.util.__template2(editHtml, data)),
e = new common.edit('#basicForm', {bucket: 'taobaocms'});
e.init();
//循环父类信息
function getParentsName(parentsData){
if(parentsData && parentsData.length > 0){
var parent = parentsData[0];
parentName += parent.categoryName;
if(parent.items) parentName += "<span style='color: #3071a9; font-weight: bold; padding: 0 5px'> > </span>";
getParentsName(parent.items, parentName);
}else{
return parentName;
}
}
}
//弹层表单校验
function checkForm(){
var categoryName = $("#categoryName").val();
if(!categoryName || categoryName == ""){
$("#categoryName").addClass("error");
common.util.__tip("销售类目名称不能为空!");
return false;
}
return true;
}
//添加根分类
$('#add-root-category').on('click', function() {
initDialog();
var data = {};
data.action = '/sale/salesCategory/addSC';
initForm(data);
tabTreeArr=[];
tabTree = new common.tabTree(".sortTree", {
status: 1,
sortLevel: 1
});
tabTree.init();
tabTreeArr.push(tabTree);
checkSortNum();
});
//添加子分类
$(document).on('click', '.add-sub-category', function() {
initDialog();
var categoryId = $(this).parent().data('id');
common.util.__ajax({
url: '/sale/salesCategory/querySCById',
data: {
categoryId: categoryId
}
}, function(res) {
var data = {};
data.action = '/sale/salesCategory/addSC';
data.parentSC = res.data.parentSC;
data.subCategoryName = res.data.categoryName;
data.parentId = res.data.categoryId;
initForm(data);
tabTreeArr = [];
tabTree = new common.tabTree(".sortTree", {
status: 1,
sortLevel: 1
});
tabTree.init();
tabTreeArr.push(tabTree);
checkSortNum();
}, true);
});
//编辑销售类目
$(document).on('click', '.edit-btn', function() {
initDialog();
var categoryId = $(this).parent().data('id');
common.util.__ajax({
url: '/sale/salesCategory/querySCById',
data: {
categoryId: categoryId
}
}, function(res) {
res.data.action = '/sale/salesCategory/updateSC';
initForm(res.data);
console.log(res.data);
if(res.data.relationProductSort && res.data.relationProductSort.length > 0){
//渲染物理类目
var selectedArr = [];
$.each(res.data.relationProductSort, function(i, item) {
var sort = [{id: item.id, sortName: item.sortName}];
addSelected(sort, item);
selectedArr.push(sort);
});
if (selectedArr.length > 0) {
$('.sort-wrap').empty();
tabTreeArr = [];
$.each(selectedArr, function(i, item) {
var $sortGroup = $($('#sortWrap').html());
$sortGroup.appendTo($('.sort-wrap'));
checkSortNum();
tabTree = new common.tabTree(".sortTree:last", {
status: 1,
sortLevel: 1
});
tabTreeArr.push(tabTree);
tabTree.init(item);
});
}
}else {
tabTreeArr = [];
tabTree = new common.tabTree(".sortTree", {
status: 1,
sortLevel: 1
});
tabTree.init();
tabTreeArr.push(tabTree);
checkSortNum();
}
}, true);
});
function addSelected(arr, item) {
if (item && item.parentPS) {
arr.unshift({id: item.parentPS.id, sortName: item.parentPS.sortName});
addSelected(arr, item.parentPS)
}
}
function checkSortNum() {
if ($('.sort-wrap').find('.row').size() > 1) {
$('.sort-wrap').find('.row').find('.add-sort-btn').hide().end().find('.del-sort-btn').show();
$('.sort-wrap').find('.row:last').find('.add-sort-btn').show().end().find('.del-sort-btn').show();
} else {
$('.sort-wrap').find('.row:first').find('.add-sort-btn').show().end().find('.del-sort-btn').hide();
}
}
//添加物理类目
$(document).on('click', '.add-sort-btn',function() {
var $sortGroup = $($('#sortWrap').html());
$sortGroup.appendTo($('.sort-wrap'));
tabTree = new common.tabTree(".sortTree:last", {
status: 1,
sortLevel: 1
});
tabTreeArr.push(tabTree);
tabTree.init();
checkSortNum();
});
//删除物理类目
$(document).on('click', '.del-sort-btn', function() {
var sortIndex = $(this).parents('.row').index();
$(this).parents('.row').remove();
tabTreeArr.splice(sortIndex, 1);
checkSortNum();
});
//保存销售类目
function saveBrand(){
e.submit($('#basicForm').attr('action'), function(option) {
var selected = [];
$.each(tabTreeArr, function(i, item){
if (item.selected.length > 0) {
selected.push(item.selected[item.selected.length - 1].id);
}
});
option.data.relationParameter = selected.join(',');
console.log(option.data.relationParameter);
option.success = function(res) {
res = res.data;
if (res.code == '200') {
console.log(res);
var categoryId = res.data.categoryId;
var parentId = res.data.parentId;
var thisCategory = $("#category_id_" + categoryId);
if(thisCategory.length > 0){ //编辑保存
if(res.data.categoryName) { $("#category_id_" + categoryId).find("span").eq(2).html(res.data.categoryName); }
if(res.data.icon) { $("#category_id_" + categoryId).find("span").eq(4).html("<img src='"+ res.data.icon +"' alt=''>"); }
e.$tip("编辑成功!", function() {}, 'growl-success');
}else if(parentId == "0"){ //添加根目录
initCategory(res.data);
e.$tip("添加根目录成功!", function() {}, 'growl-success');
}else{ //添加子目录
initCategory(res.data, parentId);
e.$tip("添加子目录成功!", function() {}, 'growl-success');
}
} else {
e.$tip(res.message);
}
}
});
}
//销售类目前端局部刷新
function initCategory(data,parentId){
if(parentId){
var html = "<div id='category_id_"+ data.categoryId +"' class='level-wrap clearfix' style='display: none'>";
html += common.util.__template2($('#tableTemp2').html(), data);
html += "</div>";
$("#category_id_" + parentId).append(html);
if($("#category_id_" + parentId).find(".folder").hasClass("open")){
$("#category_id_" + data.categoryId).show();
$("#category_id_" + data.categoryId + " .folder").css("background-position", (12 + 36 * (data.levelNumber - 1)) + "px");
}else{
$("#category_id_" + parentId).find(".folder").click();
}
}else{
var html = "<tr><td><div id='category_id_"+ data.categoryId +"' class='level-wrap clearfix'>";
html += common.util.__template2($('#tableTemp2').html(), data);
html += "</div></td></tr>";
$(".grid table tbody").append(html);
}
newCategorySee(data.categoryId);
}
//新增的销售类目高亮
function newCategorySee(categoryId){
$("#category_id_" + categoryId).addClass("newCategory");
setTimeout(function() {
$("#category_id_" + categoryId).removeClass("newCategory");
}, 3000);
$('html,body').animate({scrollTop:$("#category_id_" + categoryId).offset().top - window.screen.height/2}, 800)
}
//开启销售类目
$(document).on('click', '.open-category', function() {
var categoryId = $(this).parent().data('id'),
categoryName = $(this).parent().data('name');
common.dialog.confirm("温馨提示", "确定  <b><font color='#449d44'>开启 </font>【" + categoryName + "】</b> 分类吗?", function() {
common.util.__ajax({
url: '/sale/salesCategory/updateSCStatus',
data: {
categoryId: categoryId,
state: 1
}
}, function(res){
if (res.code == 200) {
$("#category_id_" + categoryId).find(".status").html("<b style='color: #449d44'>开启</b>");
$("#category_id_" + categoryId).find(".openAndClose").html("<a class='btn btn-danger btn-xs close-category' href='javascript:;'>关闭</a>");
common.util.__tip("开启成功!","success");
} else{
common.util.__tip(res.data.message);
}
},true)
});
})
//关闭销售类目
$(document).on('click', '.close-category', function() {
var categoryId = $(this).parent().data('id'),
categoryName = $(this).parent().data('name');
common.dialog.confirm("温馨提示", "确定  <b><font color='#ff0000'>关闭 </font>【" + categoryName + "】</b> 分类吗?", function() {
common.util.__ajax({
url: '/sale/salesCategory/updateSCStatus',
data: {
categoryId: categoryId,
state: 0
}
}, function(res){
if (res.code == 200) {
$("#category_id_" + categoryId).find(".status").html("<b style='color: #d9534f'>关闭</b>");
$("#category_id_" + categoryId).find(".openAndClose").html("<a class='btn btn-warning btn-xs open-category' href='javascript:;'>开启</a>");
common.util.__tip("关闭成功!","success");
}else{
common.util.__tip(res.data.message);
}
},true)
});
})
//切换状态重载列表
$('input[name="state"]').on('change', function() {
$(this).parent().addClass('current').siblings().removeClass('current');
g.reload();
});
$(document).on('change', '#categoryName', function(){$(this).removeClass("error")})