salesdata.js
17.8 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* founder : zzf
* Creation time : 2014/11/11
* note :
* role : 商品属性自动创建
*/
define(function( require, exports, module ) {
var SalesData = function (arges) {
//临时存储空间
var array = [];
var len = 0;
var indexs = {};
var config = {
odata : {},//对象存储
sdata : [],//数据存储
trview : arges.rootView && $(arges.rootView).find("tr").length > 0 ? $(arges.rootView).find("tr") : $(arges.rootView).find( '.attribute' ),//遍历对象
radioview : arges.radioNmae ? $(arges.radioNmae) : null,//设置区域操作
currentview : arges.showView ? $(arges.showView) : null,//显示区域视图
titles : [],//title数据存储
data : [],//当前未整理数据存储
isdata : {},//当前显示数据存储
setview : null,//显示区域回调
res : [
{"class" : "price", "dilipattern" : "/^[0-9]+$/,可售量有误!", "value" : "", "name":""},
{"class" : "stockNum", "dilipattern" : "/^[0-9]+$/,可售量有误!","dilimethod":"checkProductStockNum", "value" : "", "name":""},
{"class" : "minNum", "dilipattern" : "/^[0-9]+$/,可售量有误!","dilimethod":"checkProductStockNum", "value" : "", "name":""}
],//参数
httpdata : {},//网络数据存储
sku : {}//sku本地存储
};
var events = {
change : null//设置操作回调
};
var methods = {
updata : function(){//数据更新整理
//if(events.change){events.change()};
config.sdata = [];
config.data = [];
array = [];
for (i in config.odata) {
config.sdata.push(config.odata[i].data);
};
for (var i = 0; i < config.sdata.length; i++) {//二维数组转化 数据筛选
var list = [];
for (var k = 0; k < config.sdata[i].length; k++) {
if (config.sdata[i][k].bl) {
var ack = config.sdata[i][k];
list.push({txt: ack.name, val: ack.val, data:ack.data ? ack.data : null});
};
};
if(list.length > 0){
array.push(list);
};
};
len = array.length;
methods.specialSort(-1);
methods.setdata();
},
getdata : function(){//获取数据
var texts = [];
var skus = {};
for (var i = 0; i < config.setdata.length; i++) {
if(config.setdata[i].sku){
var txt = '';
var attribute = methods.skuString(config.setdata[i].sku);
var keys = config.setdata[i].sku.split(":");
for (var k = 0; k < attribute.length; k++) {
if(!skus[keys[k]]){
texts.push({
txt : attribute[k],
sku : keys[k]
});
};
txt += attribute[k];
};
config.setdata[i].attribute = attribute;
config.httpdata[txt] = config.setdata[i];
};
}
methods.upview(texts);
var w = 0;
for(i in config.odata){//更新数据
config.odata[i].changeData();
w ++;
};
},
skuString : function(sku){
var sku = sku.split(":");
var attribute = [];
for (var i = 0; i < sku.length; i++) {
attribute.push(config.sku[sku[i]]);
};
return attribute;
},
setdata : function(){//设置数据显示
var unit = config.radioview.find("option:selected").length > 0 ? config.radioview.find("option:selected").text() : $("#unit").text();
var results = config.data;
var current = config.currentview.find("tbody");
current.html("");//清楚数据
methods.showdata(results);
if (unit == "" || results.length <= 0 || results[0].length < config.sdata.length) {return false;};
if(config.setview){
config.setview(unit);
}else{
methods.updateview(unit);
};
},
showdata : function(results){//数据存储结构整理
config.isdata = {};
for (var i = 0; i < results.length; i++) {
var text="", sku="", lis=[], abj = {}, attribute=[], adata = results[i];
for(var k = 0; k < adata.length; k++){
sku += adata[k].val;
if(k != (adata.length-1)){
sku += ":";
};
text += adata[k].txt;
attribute.push(adata[k].txt);
};
if(! config.httpdata[text]){
abj.sku = sku;
abj.data = methods.copy();
abj.attribute = attribute;
config.isdata[text] = abj;
}else{
config.isdata[text] = config.httpdata[text];
};
};
},
oevent : function(cals){//事件监听
this.data = [];
var self = this;
$('.' + cals).bind("change", function () {
self.data = [];
self.changeData();
//return false;
});
this.changeData = function(data){
$('.' + cals).each(function () {
var name = $(this).attr('alt');
if ($(this).attr('checked') == "checked") {
self.data.push({ bl: true, name: name, val: $(this).attr('value'), data:data ? data : null});
} else {
self.data.push({ bl: false, name: name, val: '', data:data ? data : null});
};
});
methods.updata();
};
return this;
},
upview : function(data){//更新视图
config.trview.find(':checkbox').prop("checked", false);
config.trview.each(function () {
if ($(this).hasClass('attribute')) {
$(this).find('input').each(function () {
var val = $(this).val();
var key = $(this).attr('alt');
for (var i = 0; i < data.length; i++) {
if(data[i].txt){
if(val == data[i].sku){
$(this).prop("checked", true);
}
}else{
if(key == data[i]){
$(this).prop("checked", true);
}
}
}
});
};
});
},
start : function (){//初始化
$.extend(config, arges);//数据合并
config.trview.each(function () {
if ($(this).hasClass('attribute')) {
var title = $(this).find('input').attr('class');
var checkbox = new methods.oevent(title);
config.odata[title] = checkbox;
config.sdata.push(checkbox.data);
$(this).find('input').each(function(){
config.sku[$(this).val()] = $(this).attr("alt");
});
};
});
if(config.setdata){methods.getdata()};
methods.eventinput();
},
specialSort : function(start){//二分查找算法
start++;
if (start > len - 1) {
return;
}
if (!indexs[start]) {
indexs[start] = 0;
}
if (!(array[start] instanceof Array)) {
array[start] = [array[start]];
}
for (indexs[start] = 0; indexs[start] < array[start].length; indexs[start]++) {
methods.specialSort(start);
if (start == len - 1) {
var temp = [];
for (var i = len - 1; i >= 0; i--) {
if (!(array[start - i] instanceof Array)) {
array[start - i] = [array[start - i]];
}
temp.push(array[start - i][indexs[start - i]]);
}
config.data.push(temp);
}
}
},
unique : function(list){//数组去重
var res = [];
var json = {};
for(var i = 0; i < list.length; i++){
if(!json[list[i]]){
res.push(list[i]);
json[list[i]] = 1;
}
}
return res;
},
updateview : function(unit){//默认更新视图
var current = config.currentview.find("tbody");
current.html("");//清楚数据
var data = config.isdata;
for (k in data) {
var view = '<tr>', alist = data[k].attribute, moredata = data[k].data;
for (var i = 0; i < alist.length; i++) {
view += '<td class="txt" val="' + data[k].sku + '">' + alist[i] + '</td>';
};
if(moredata && moredata[0]){
view += methods.issku(moredata, unit, data[k].sku);
}else{
view += methods.onsku(moredata, unit, data[k].sku);
};
current.append(view);
};
},
issku : function(moredata, unit, sku){//已有数据对象处理
var view = "";
for (var j = 0; j < moredata.length; j++) {
view += '<td><input index="'+j+'" type="text" sku='+sku;
view += methods.automatic(moredata[j], sku);
view += ' />'+(moredata[j]["class"] && (methods.hasClass(moredata[j]["class"], "stockNum") || methods.hasClass(moredata[j]["class"], "minNum")) ? "": "元/")
+'<span class="t-unit">'+unit+'</span></td>';
};
return view;
},
onsku : function(moredata, unit, sku){//新增对象处理
var view = '';
for (var k = 0; k < moredata.length; k++) {
view += '<td><input index="'+k+'" type="text" sku='+sku
+ methods.automatic(config.res[k], sku)+' />'
+(config.res[k]["class"] && (methods.hasClass(config.res[k]["class"], "stockNum") || methods.hasClass(config.res[k]["class"], "minNum")) ? "": "元/")
+'<span class="t-unit">'+unit+'</span></td>';
};
return view;
},
automatic : function(data, sku){//添加其他属性
var view = ' ';
for(f in data){
if(f == "name" && ! data[f]){
view += ' '+f+'="'+(sku+":"+data["class"])+'"';
}else{
view += ' '+f+'="'+data[f]+'"';
};
};
return view;
},
eventinput : function(){//检测数值变化
var current = config.currentview;
current.find("input").live("change", function(){
var thisview = $(this), pview = thisview.parent().parent(), text="", index= 0, attribute=[], sku="";
pview.find(".txt").each(function(){
text += $(this).html();
attribute.push($(this).html());
});
sku = thisview.attr("sku");
index = parseInt(thisview.attr("index"));
if(config.httpdata[text]){
config.httpdata[text].data[index]["value"] = thisview.val();
}else{
var aobj = {
attribute : attribute,
sku : sku,
data : methods.copy()
};
aobj.data[index]["value"] = thisview.val();
config.httpdata[text] = aobj;
};
});
config.radioview.change(function () {
if(events.change){events.change()};
});
},
cloneObj : function(obj){//对象深拷贝
var newO = {};
if (obj instanceof Array) {
newO = [];
}
for (var key in obj) {
var val = obj[key];
newO[key] = typeof val === 'object' ? arguments.callee(val) : val;
}
return newO;
},
copy : function(){//数组复制
var res = new Array();
for(i in config.res){
res.push(methods.cloneObj(config.res[i]));
};
return res;
},
hasClass : function(text, txt){
text = text.split(" ");
for(i in text){
if(text[i] == txt){
return true;
}
};
return false;
}
};
//开放接口
this.event = function(key, callback){
if(typeof(callback) == "function"){
events[key] = callback;
};
};
//设置数据
this.setData = function(data){
config.setdata = data;
methods.getdata();
};
//设置改变
this.changeData = function(){
var texts = [];
var data = [];
for(i in config.isdata){
var txt = i;
data = config.isdata[i].attribute;
for(var k=0; k < data.length; k++){
texts.push(data[k]);
}
};
methods.upview(methods.unique(texts));
var w = 0;
for(i in config.odata){//更新数据
config.odata[i].changeData();
w ++;
};
};
//临时解决接口
this.eachData = function(){
//数据遍历
var data = [];
var leng = config.odata.length;
config.currentview.find("tbody tr").each(function(){
var abj = {
data : []
};
abj.sku = $(this).find("td").eq(0).attr("val").split(":").slice(1);
var key = "";
for(var k=0; k < abj.sku.length; k++){
key += abj.sku[k];
if(k != (abj.sku.length - 1)){
key += ":";
}
};
abj.sku = key;
$(this).find("input").each(function(){
var elProp= {};
if($(this).attr("dilipattern")){
elProp.dilipattern = $(this).attr("dilipattern");
}
if($(this).attr("dilimethod")){
elProp.dilimethod = $(this).attr("dilimethod");
}
if($(this).attr("money")){
elProp.money = $(this).attr("money");
}
if($(this).attr("digits")){
elProp.digits = $(this).attr("digits");
}
if($(this).attr("maxlength")){
elProp.maxlength = $(this).attr("maxlength");
}
if($(this).attr("range")){
elProp.range = $(this).attr("range");
}
if($(this).attr("min")){
elProp.min = $(this).attr("min");
}
if($(this).attr("max")){
elProp.max = $(this).attr("max");
}
elProp.value= $(this).val();
elProp["class"] = $(this).attr("class");
elProp.sku=$(this).attr("sku");
elProp.vsku= $(this).attr("vsku");
elProp.name= $(this).attr("name");
abj.data.push(elProp);
});
data.push(abj);
});
if(data.length <= 0){
return false;
};
config.setdata = data;
methods.getdata();
var w = 0;
for(i in config.odata){//更新数据
config.odata[i].changeData();
w ++;
};
};
methods.start();
return this;
};
return SalesData;
});