tabTree.js 10.9 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
var $=require('jquery');

function tabTree(ele,options){
	var defaults = {
		innerHtml: ' <a href="javascript:void(0);"  class="cityboxbtn" ></a>'
		            + '<div class="citybox">'
				       + '<div class="btn btn-info">重置</div> '
		               + '<div class="chooseArea">' + "</div>"
		               + '<div  class="arriveBox">'
		                  + '<div class="cityshow"></div>'
		               + "</div>"
		               + '<div class="closeSelector"></div>'
		            + "</div>",
		level: 1,
		columns: [{
			state: "prov",
			text: "一级分类",
			hide: false,
			addclass: ""
		}, {
			state: "city",
			text: "二级分类",
			hide: false,
			addclass: ""
		}, {
			state: "area",
			text: "三级分类",
			hide: false,
			addclass: ""
		}, {
			state: "town",
			text: "四级分类",
			hide: false,
			addclass: ""
		}],
		datas: [{
			sortName: "",
			code: "",
			id: ""
		}, {
			sortName: "",
			code: "",
			id: ""
		}, {
			sortName: "",
			code: "",
			id: ""
		}, {
			sortName: "",
			code: "",
			id: ""
		}],
		otherParam: null,
		stepMet: null,
		complete: null
	};
	this.options = $.extend({}, defaults, options);
	this.element = $(ele);
}

tabTree.prototype= {
		init: function(datas) {
			var g = this,
				p = this.options;
			g.element.addClass("citySelect");
			g.dom = g.dom || g._dom();
			g.selected = [], g.records = [], g.rows = [];
			p.datas = datas || p.datas;
			if (p.columns.length < p.datas.length) {
				return
			}
			g.isfeeze = g.isfeeze || false;
			g.renderHeader();
			g.setEvent();

			// add by xueyin: 初始时隐藏重置按钮
			g.dom["btn btn-info"].hide();
		},
		setEvent: function() {
			var g = this,
				p = this.options;
			g.dom["cityboxbtn"].off();
			g.dom["cityboxbtn"].click(function(event) {
				if(g.isfeeze){
					return false;
				}
				g.dom["cityboxbtn"].addClass("select");
				g.dom["citybox"].show().css("top", (g.inputHeight-10) + "px");
				g.dom["chooseArea"].children("p").each(function() {
					if ($(this).hasClass("disable") && $(this).hasClass("cur")) {
						g.active_count = 0;
						g.getData(g._getParam(0))
					}
				});
				return event.stopPropagation()
			});

			// mod by xueyin: 增加重置功能
			g.dom["btn btn-info"].off();
			g.dom["btn btn-info"].click(function(event) {
				g.selected = [];
				g.setHeadVal(true);

				g.active_count = 0;
				g.getData(g._getParam(0));
				g.changeVolume();

				g.renderHeader();
				g.reset();
				return event.stopPropagation()
			});
			g.dom["chooseArea"].off();
			g.dom["chooseArea"].children("p").click(function(event) {
				if ($(this).hasClass("disable")) {
					return event.stopPropagation()
				} else {
					g.active_count = $(this).index();
					g.getData(g._getParam(g.active_count));
					return event.stopPropagation()
				}
			});
			g.dom["cityshow"].off();
			g.dom["cityshow"].on("click", "span>a", function(event) {
				var rowid = $(this).attr("data").split("_")[1];
				g.select(g.records[rowid]);
				g.changeVolume();
				if(!p.url){
					g.setHeadVal();
				}else{
					/*既可以选择三级也可以选择四级 hack*/
					if(g.active_count>2){
						g.setHeadVal();
					}
				}

				return event.stopPropagation()
			});
			$(document).click(function() {
				if (g.dom["citybox"].css("display") == "block") {
					g.reset()
				}
			})
		},
		changeVolume: function() {
			var g = this,
				p = this.options;
			var _data = g.selected[g.active_count];
			// del by xueyin: 无需修改tab头
			// $("span", g.dom["chooseArea"]).eq(g.active_count).html(_data.sortName);
			g.selected[g.active_count].isdirect = _data.item ? true : false;
			var _cur = g.active_count;
			// del by xueyin: 无需修改tab头
			// for (var i = g.active_count + 1; i < p.datas.length; i++) {
			// 	$("span", g.dom["chooseArea"]).eq(i).html(p.columns[i].text)
			// }
			if (g.active_count + 1 <= p.columns.length - 1) {
				g.active_count++;
				var _next = g.active_count;
				if (_data.item) {
					_cur = g.active_count;
					$("p", g.dom["chooseArea"]).eq(g.active_count).addClass("disable");
					$("span", g.dom["chooseArea"]).eq(g.active_count).html(_data.item[0].sortName);
					g.selected[g.selected.length] = _data.item[0];
					g.active_count++;
					_next = g.active_count;
				}
				if (g.active_count <= p.columns.length - 1) {
					g.getData(g._getParam(_next))
				} else {
					g.active_count = 0;
					g.setHeadVal(true);
					g.reset()
				}
			} else {
				g.setHeadVal(true);
				g.reset()
			}
		},
		getData: function(_param) {
			var g = this,
				p = this.options;
			$("p", g.dom["chooseArea"]).eq(g.active_count).removeClass("disable").addClass("cur").siblings("p").removeClass("cur");
			for (var i = g.active_count + 1; i < p.datas.length; i++) {
				if ($("span", g.dom["chooseArea"]).eq(i).html() == p.columns[i].text) {
					$("p", g.dom["chooseArea"]).eq(i).addClass("disable")
				}
			}
			_param.param=_param.sortId;
			
			g.getAjax(_param)
		},
		getAjax: function(param) {
			var g = this,
				p = this.options;

			var url = '';

			if (param.state == 'prov') {
				if (p.status) {
					url = '/sale/product/queryPSNotTreeWhenLevelAndStatus';
				} else {
					url = '/product/class/queryAllProductSortList';
				}
			} else {
				if (p.status) {
					url = '/sale/product/queryPSListByParentIdWhenStatus';
				} else {
					url = '/product/class/queryProductSortList';
				}
				
			}

			url=p.url||url;

			param.status = p.status;
			param.sortLevel = p.sortLevel;
			var ajaxOptions = {
				type: "POST",
				url:url+"?_datetime_="+new Date(),
				data: param,
				dataType: 'json',
				beforeSend: function(msg) {
					g.dom["cityshow"].html("加载中...")
				},
				success: function(items) {
					//console.log("返回值",items);
					items=items;



					g.dom["cityshow"].html("");
					g.records.length = 0;
					g.rows.length = 0;
					if (items.data.length == 0) {
						g.active_count = 0;
						g.setHeadVal(true);
						g.reset();
					}else{
					$.each(items.data, function(count, item) {
							var b = $("<span/>");
							var a = $("<a/>");
							if (item.ishot == "true") {
								a.addClass("imp")
							}
							a.attr("data", "Sns_" + count).attr("title",item.sortName).html(item.sortName);
							b.append(a);
							g.dom["cityshow"].append(b);
							g.records.push(count);
							g.rows.push(item)
						});
					}
				},
				error: function(e1, e2) {}
			};
			$.ajax(ajaxOptions)
		},
		setHeadVal: function(bool) {
			var g = this,
				p = this.options;
			var arr=[];
			$.each(g.selected, function(count, item) {
				arr.push(item.sortName);
			});
			g.dom["cityboxbtn"].html(arr.join(' > ')||'请选择类目');

			// add by xueyin: 重置按钮可以根据需求隐藏
			if (arr.length == 0) {
				g.dom["btn btn-info"].hide();
			} else {
				g.dom["btn btn-info"].show();
			}
			p.datas.length = 0;
			$.each(g.selected, function(count, item) {
				p.datas.push(item)
			});
			if (p.complete != null) {
				p.complete(p.datas, bool)
			}
		},
		renderHeader: function() {
			var g = this,
				p = this.options;

			function _buildHtml(_count, items, isdisable) {
				var _tempfun2 = function(count, item, isdisable) {
						var _txt = $.trim(item.sortName) == "" ? p.columns[_count].text : item.sortName;

						if(item.disabled){
							var _html = '<p class="disable" eq="' + count + '"><span>' + _txt + "</span><b></b></p>";
						}else{
							var _html = '<p eq="' + count + '"><span>' + _txt + "</span><b></b></p>";
						}

						if (isdisable || item.id == "") {
							_html = '<p class="disable"><span>' + _txt + "</span><b></b></p>"
						}
						return _html
					};
					// g.dom["chooseArea"].html('');
				//g.dom["cityboxbtn"].append(_tempfun1(_count, items, isdisable));
				g.dom["chooseArea"].append(_tempfun2(_count, items, isdisable))
			}
			g.dom["chooseArea"].html("");
			if (p.datas && p.columns && p.datas.length > 0) {
				var _isdirect = false;
				$.each(p.datas, function(count, item) {
					if (item.id) {
						g.selected[count] = item
					}
					_buildHtml(count, item, _isdirect);
					_isdirect = p.datas[count].isdirect
				});
				var arr=[];
				$.each(g.selected, function(count, item) {
					arr.push(item.sortName);
				});
				g.dom["cityboxbtn"].html(arr.join(' > ')||'请选择类目');

				if (g.selected.length > 0 && !g.selected[g.selected.length - 1].isdirect) {
					if (g.selected.length != p.datas.length) {
						g.active_count = g.selected.length;
						g.getData(g._getParam(g.active_count))
					} else {
						g.active_count = g.selected.length - 1;
						g.getData(g._getParam(g.active_count))
					}
				} else {
					g.active_count = 0;
					g.getData(g._getParam(g.active_count))
				}
				_isdirect = false;
				if (p.complete != null) {
					p.complete(p.datas, true)
				}
			}
			g.dom.chooseArea["append"]('<div class="clear"></div>');
			g.dom.cityboxbtn["append"]("<b></b>");
			g.inputHeight = g.dom.cityboxbtn["height"]();
			g.dom["chooseArea"].addClass("fix");
		},
		reset: function() {
			var g = this,
				p = this.options;
			g.dom["cityboxbtn"].removeClass("select");
			g.dom["citybox"].hide();
		},
		select: function(rp) {
			var g = this,
				p = this.options;
			var row = g.rows[parseInt(rp)];
			g.selected.length = g.active_count;
			g.selected[g.selected.length] = row
		},
		_getParam: function(rp) {
			var g = this,
				p = this.options;
			var defaults = {
				state: p.columns[rp].state,
				// brandId:928,
				sortId: rp - 1 > -1 ? g.selected[rp - 1].id : ""
			};
			var setting = {};
			if (p.otherParam) {
				var obj = p.otherParam(rp, p.columns, g.selected);
				if (obj && typeof(obj) == "object") {
					setting = obj
				}
			}
			var params = $.extend(defaults, setting);
			return params
		},
		_dom: function() {
			var g = this,
				p = this.options;
			g.element.html(p.innerHtml);
			var wrap = g.element;
			var name, DOM = {
				wrap: $(wrap)
			},
				els = wrap[0].getElementsByTagName("*"),
				elsLen = els.length;
			for (var i = 0; i < elsLen; i++) {
				name = els[i].className;
				if (name) {
					DOM[name] = $(els[i], wrap)
				}
			}
			return DOM
		},
		getAddress: function() {
			var g = this,
				p = this.options;
			return p.datas
		},
		getRows: function() {
			var g = this,
				p = this.options;
			return g.selected
		},
		setCurrent: function(rp) {
			var g = this,
				p = this.options;
			g.dom["cityboxbtn"].addClass("select");
			g.dom["citybox"].show().css("top", (g.inputHeight-10) + "px");
			if (g.selected.length - parseInt(rp) >= 0) {
				g.active_count = parseInt(rp);
				g.getData(g._getParam(g.active_count))
			} else {
				g.active_count = 0;
				g.getData(g._getParam(g.active_count))
			}
			return event.stopPropagation()
		},
	    getDisplayName: function () {
			var g = this;
			var name = g.dom["cityboxbtn"].text();
			if (name == "请选择类目") {
				return "";
			}

			return  name.split(' > ').join('|');
		},
	    setDisplayName: function (name) {
			var g = this;
			if (name == "") {
				return;
			}

			var dis_name = name.split('|');
			g.dom["cityboxbtn"].html(dis_name.join(' > ')||'请选择类目');
		}
	};
module.exports = tabTree;