var Feng = {
|
ctxPath: "",
|
addCtx: function (ctx) {
|
if (this.ctxPath == "") {
|
this.ctxPath = ctx;
|
}
|
},
|
confirm: function (tip, ensure) {//询问框
|
parent.layer.confirm(tip, {
|
btn: ['确定', '取消']
|
}, function (index) {
|
ensure();
|
parent.layer.close(index);
|
}, function (index) {
|
parent.layer.close(index);
|
});
|
},
|
log: function (info) {
|
console.log(info);
|
},
|
alert: function (info, iconIndex) {
|
parent.layer.msg(info, {
|
icon: iconIndex
|
});
|
},
|
info: function (info) {
|
Feng.alert(info, 0);
|
},
|
success: function (info) {
|
Feng.alert(info, 1);
|
},
|
error: function (info) {
|
Feng.alert(info, 2);
|
},
|
infoDetail: function (title, info) {
|
var display = "";
|
if (typeof info == "string") {
|
display = info;
|
} else {
|
if (info instanceof Array) {
|
for (var x in info) {
|
display = display + info[x] + "<br/>";
|
}
|
} else {
|
display = info;
|
}
|
}
|
parent.layer.open({
|
title: title,
|
type: 1,
|
skin: 'layui-layer-rim', //加上边框
|
area: ['950px', '600px'], //宽高
|
content: '<div style="padding: 20px;">' + display + '</div>'
|
});
|
},
|
writeObj: function (obj) {
|
var description = "";
|
for (var i in obj) {
|
var property = obj[i];
|
description += i + " = " + property + ",";
|
}
|
layer.alert(description, {
|
skin: 'layui-layer-molv',
|
closeBtn: 0
|
});
|
},
|
showInputTree: function (inputId, inputTreeContentId, leftOffset, rightOffset) {
|
var onBodyDown = function (event) {
|
if (!(event.target.id == "menuBtn" || event.target.id == inputTreeContentId || $(event.target).parents("#" + inputTreeContentId).length > 0)) {
|
$("#" + inputTreeContentId).fadeOut("fast");
|
$("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发,不用弹起
|
}
|
};
|
|
if (leftOffset == undefined && rightOffset == undefined) {
|
var inputDiv = $("#" + inputId);
|
var inputDivOffset = $("#" + inputId).offset();
|
$("#" + inputTreeContentId).css({
|
left: inputDivOffset.left + "px",
|
top: inputDivOffset.top + inputDiv.outerHeight() + "px"
|
}).slideDown("fast");
|
} else {
|
$("#" + inputTreeContentId).css({
|
left: leftOffset + "px",
|
top: rightOffset + "px"
|
}).slideDown("fast");
|
}
|
|
$("body").bind("mousedown", onBodyDown);
|
},
|
baseAjax: function (url, tip) {
|
var ajax = new $ax(Feng.ctxPath + url, function (data) {
|
Feng.success(tip + "成功!");
|
}, function (data) {
|
Feng.error(tip + "失败!" + data.responseJSON.message + "!");
|
});
|
return ajax;
|
},
|
base_ajax: function (url, data, success, error) {
|
// 加载动画层,0代表加载的风格,支持0-2
|
var index = parent.layer.load(0, {shade: [ 0.1, "#2a2b3c"]}), close_layer = function() { parent.layer.close(index); };
|
error = error || function( data) { Feng.error("操作失败!" + data.responseJSON.message + "!");}
|
setTimeout(function () {
|
new $ax(Feng.ctxPath + url,
|
function ( data) {
|
close_layer();
|
if ( data.code != 200) { data.responseJSON = data; return error ? error( data) : Feng.error( "操作失败!" + data.message); }
|
success && success( data)
|
},
|
function ( data) { close_layer(); error( data) })
|
.setData( data).start();
|
}, 100);
|
},
|
base_ajax_data: function (url, data, success, error) {
|
// 加载动画层,0代表加载的风格,支持0-2
|
var index = parent.layer.load(0, {shade: [ 0.1, "#2a2b3c"]}), close_layer = function() { parent.layer.close(index); };
|
error = error || function( data) { Feng.error("操作失败!" + data.responseJSON.message + "!");}
|
setTimeout(function () {
|
new $ax(Feng.ctxPath + url,
|
function ( data) {
|
close_layer();
|
success && success( data)
|
},
|
function ( data) { close_layer(); error( data) })
|
.setData( data).start();
|
}, 100);
|
},
|
changeAjax: function (url) {
|
return Feng.baseAjax(url, "修改");
|
},
|
zTreeCheckedNodes: function (zTreeId) {
|
var zTree = $.fn.zTree.getZTreeObj(zTreeId);
|
var nodes = zTree.getCheckedNodes();
|
var ids = "";
|
for (var i = 0, l = nodes.length; i < l; i++) {
|
ids += "," + nodes[i].id;
|
}
|
return ids.substring(1);
|
},
|
eventParseObject: function (event) {//获取点击事件的源对象
|
event = event ? event : window.event;
|
var obj = event.srcElement ? event.srcElement : event.target;
|
return $(obj);
|
},
|
sessionTimeoutRegistry: function () {
|
$.ajaxSetup({
|
contentType: "application/x-www-form-urlencoded;charset=utf-8",
|
complete: function (XMLHttpRequest, textStatus) {
|
//通过XMLHttpRequest取得响应头,sessionstatus,
|
var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
|
if (sessionstatus == "timeout") {
|
//如果超时就处理 ,指定要跳转的页面
|
window.location = Feng.ctxPath + "/global/sessionError";
|
}
|
}
|
});
|
},
|
initValidator: function (formId, fields) {
|
$('#' + formId).bootstrapValidator({
|
feedbackIcons: {
|
valid: 'glyphicon glyphicon-ok',
|
invalid: 'glyphicon glyphicon-remove',
|
validating: 'glyphicon glyphicon-refresh'
|
},
|
fields: fields,
|
live: 'enabled',
|
message: '该字段不能为空'
|
});
|
},
|
underLineToCamel: function (str) {
|
var strArr = str.split('_');
|
for (var i = 1; i < strArr.length; i++) {
|
strArr[i] = strArr[i].charAt(0).toUpperCase() + strArr[i].substring(1);
|
}
|
var result = strArr.join('');
|
return result.charAt(0).toUpperCase() + result.substring(1);
|
},
|
randomNum: function (minNum, maxNum) {
|
switch (arguments.length) {
|
case 1:
|
return parseInt(Math.random() * minNum + 1, 10);
|
break;
|
case 2:
|
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
|
break;
|
default:
|
return 0;
|
break;
|
}
|
},
|
newCrontab: function (href, menuName) {
|
var dataUrl = href;
|
var needCreateCrontab = true;
|
|
// 轮询已有的标签,判断是否已经存在标签
|
parent.$('.J_menuTab').each(function () {
|
if ($(this).data('id') == dataUrl) {
|
if (!$(this).hasClass('active')) {
|
$(this).addClass('active').siblings('.J_menuTab').removeClass('active');
|
parent.MyCrontab.scrollToTab(this);
|
parent.MyCrontab.$('.J_mainContent .J_iframe').each(function () {
|
if ($(this).data('id') == dataUrl) {
|
$(this).show().siblings('.J_iframe').hide();
|
$(this).attr('src', $(this).attr('src'));
|
return false;
|
}
|
});
|
}
|
needCreateCrontab = false;
|
return false;
|
}
|
});
|
|
//创建标签
|
if (needCreateCrontab) {
|
var tabLink = '<a href="javascript:;" class="active J_menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
|
parent.$('.J_menuTab').removeClass('active');
|
parent.$('.J_menuTabs .page-tabs-content').append(tabLink);
|
|
var iframeContent = '<iframe class="J_iframe" name="iframe' + Feng.randomNum(100,999) + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
|
parent.$('.J_mainContent').find('iframe.J_iframe').hide().parents('.J_mainContent').append(iframeContent);
|
parent.MyCrontab.scrollToTab($('.J_menuTab.active'));
|
}
|
},
|
/**
|
* 获取复选框选中值
|
*/
|
checkBoxIds: function (tableId) {
|
var selected = $("#" + tableId).bootstrapTable('getSelections');
|
if (selected.length == 0) {
|
Feng.info("请选择要操作的数据!!");
|
return '';
|
}
|
var ids = '';
|
if (selected.length > 0) {
|
for (var i = 0; i < selected.length; i++) {
|
ids += selected[i]['id'] + ",";
|
}
|
ids = ids.substring(0, ids.length - 1);
|
}
|
return ids;
|
},
|
/**
|
* 设置操作按钮
|
* @returns <a href="javascript:void(0);" onclick=update('1');>编辑</a>
|
*
|
Opts: function() {
|
var opts = '';
|
return {
|
add: function (funcName, label) {
|
if (opts != "") {
|
opts += " | ";
|
}
|
opts += '<a href="javascript:void(0);" onclick=' + funcName + '>' + label + '</a>';
|
},
|
getOpts: function() {
|
return opts;
|
}
|
}
|
},
|
* @returns <button type="button" class="btn btn-outline btn-success btn-sm" style="margin-right: 3px;">编辑</button>
|
*/
|
Opts: function() {
|
var opts = '';
|
return {
|
add: function (funcName, label) {
|
opts += '<button type="button" class="btn btn-outline btn-success btn-xs" style="margin-right: 3px;" ' +
|
'onclick=' + funcName + '>' + label + '</button>';
|
},
|
addDanger: function (funcName, label) {
|
opts += '<button type="button" class="btn btn-outline btn-danger btn-xs" style="margin-right: 3px;" ' +
|
'onclick=' + funcName + '>' + label + '</button>';
|
},
|
addBtn: function (funcName, label, btn) {
|
// 按钮样式:btn-default / btn-primary / btn-success / btn-info / btn-warning / btn-danger / btn-link
|
opts += '<button type="button" class="btn btn-outline ' + btn + ' btn-xs" style="margin-right: 3px;" ' +
|
'onclick=' + funcName + '>' + label + '</button>';
|
},
|
getOpts: function() {
|
return opts;
|
}
|
}
|
},
|
/**
|
* 获取状态转换说明
|
*/
|
getStateFormat: function(value) {
|
return {0 : "待处理", 1 : "通过", 2 : "拒绝"}[value];
|
},
|
/**
|
* 格式化处理图片路径
|
*/
|
getImageUrlFormat: function(imgUrl) {
|
if (imgUrl == null || imgUrl == '' || imgUrl == undefined) {
|
return Feng.ctxPath + '/static/img/NoPIC.png';
|
} else {
|
if(imgUrl.indexOf("http") == -1) {
|
// 不是全路径,拼接项目路径
|
imgUrl = Feng.ctxPath + '/kaptcha/' + imgUrl;
|
}
|
return imgUrl;
|
}
|
},
|
/**
|
* 获取图片Dom对象
|
*/
|
getImageDom: function(imgUrl) {
|
return '<a class = "view" href="javascript:void(0)"><img style="height:40px;" src="' + Feng.getImageUrlFormat(imgUrl) + '" onclick="Feng.openImageLayer(this);" /></a>';
|
},
|
/**
|
* 点击图片,弹出层显示大图
|
*/
|
openImageLayer: function(image) {
|
layer.open({
|
type: 1,
|
title: false,
|
closeBtn: 0,
|
area: 'auto',
|
skin: 'layui-layer-nobg', //没有背景色
|
shadeClose: true,
|
content: '<img src="' + $(image).attr("src") + '" height="100%" width="100%" />'
|
});
|
},
|
lookBigImg: function (obj) {
|
console.log("进入")
|
console.log(obj)
|
// 设置图片路径
|
var imgUrl = obj;
|
if (imgUrl != "") {
|
imgUrl = imgUrl;// 设置图片路径
|
} else {
|
imgUrl = Feng.ctxPath + '/static/img/NoPIC.png';// 默认无图
|
}
|
layer.open({
|
type: 1,
|
title: false,
|
closeBtn: 0,
|
area: ['80%', '100%'],
|
skin: 'layui-layer-nobg', //没有背景色
|
shadeClose: true, //点击空白区域关闭弹出层
|
content: '<div id="zoomableImageContainer" style="height: 100%; width: 100%; overflow: hidden; display: flex; justify-content: center; align-items: center; position: relative;"><img id="zoomableImage" src="'
|
+ imgUrl
|
+ '" style="max-width: none; max-height: none; position: absolute;"></div>',
|
success: function (layero, index) {
|
// 获取图片容器和图片元素
|
var zoomableImageContainer = document.getElementById(
|
'zoomableImageContainer');
|
var zoomableImage = document.getElementById('zoomableImage');
|
var imageWidth = zoomableImage.naturalWidth;
|
var imageHeight = zoomableImage.naturalHeight;
|
var containerWidth = zoomableImageContainer.offsetWidth;
|
var containerHeight = zoomableImageContainer.offsetHeight;
|
var isDragging = false; // 标记是否正在拖动
|
var startX, startY; // 记录拖动起始位置
|
zoomableImageContainer.addEventListener('wheel',
|
function (event) {
|
event.preventDefault();
|
var delta = Math.sign(-event.deltaY); // 获取滚动方向,1表示向上滚动,-1表示向下滚动
|
var currentWidth = zoomableImage.offsetWidth;
|
var currentHeight = zoomableImage.offsetHeight;
|
var step = 0.1; // 缩放步长
|
var newWidth = currentWidth + delta * step
|
* currentWidth;
|
var aspectRatio = zoomableImage.naturalWidth
|
/ zoomableImage.naturalHeight;
|
var newHeight = newWidth / aspectRatio;
|
zoomableImage.style.width = newWidth + 'px';
|
zoomableImage.style.height = newHeight + 'px';
|
});
|
zoomableImageContainer.addEventListener('mousedown',
|
function (event) {
|
if (event.button === 0) { // 判断是否为鼠标左键点击
|
event.preventDefault();
|
isDragging = true;
|
startX = event.clientX - zoomableImage.offsetLeft;
|
startY = event.clientY - zoomableImage.offsetTop;
|
}
|
});
|
zoomableImageContainer.addEventListener('mousemove',
|
function (event) {
|
if (isDragging) {
|
event.preventDefault();
|
var offsetX = event.clientX - startX;
|
var offsetY = event.clientY - startY;
|
var maxX = zoomableImage.offsetWidth
|
- containerWidth;
|
var maxY = zoomableImage.offsetHeight
|
- containerHeight;
|
offsetX = Math.min(Math.max(offsetX, -maxX), 0);
|
offsetY = Math.min(Math.max(offsetY, -maxY), 0);
|
zoomableImage.style.left = offsetX + 'px';
|
zoomableImage.style.top = offsetY + 'px';
|
}
|
});
|
zoomableImageContainer.addEventListener('mousemove',
|
function (event) {
|
if (isDragging) {
|
var mouseX = event.clientX;
|
var mouseY = event.clientY;
|
var newLeft = mouseX - startX;
|
var newTop = mouseY - startY;
|
// 确保图片不会移出容器范围
|
if (newLeft >= 0 && newLeft
|
+ zoomableImage.offsetWidth <= containerWidth) {
|
zoomableImage.style.left = newLeft + 'px';
|
}
|
if (newTop >= 0 && newTop
|
+ zoomableImage.offsetHeight
|
<= containerHeight) {
|
zoomableImage.style.top = newTop + 'px';
|
}
|
}
|
});
|
zoomableImageContainer.addEventListener('mouseup',
|
function (event) {
|
if (event.button === 0) {
|
isDragging = false;
|
}
|
});
|
}
|
});
|
},
|
/**
|
* 单选框-取值
|
*/
|
getRadioVal: function(key) {
|
return $("input:radio[name='" + key + "']:checked").val();
|
},
|
/**
|
* 复选框-取值
|
*/
|
getCheckboxVal: function(key) {
|
var checkIds = [];
|
$("input:checkbox[name='" + key + "']:checked").each(function(i){
|
checkIds[i] = $(this).val();
|
});
|
return checkIds.join();
|
},
|
/**
|
* 复选框-填充赋值
|
*/
|
fillCheckboxVal: function(key, fillVal) {
|
// 遍历复选框
|
$("input:checkbox[name='" + key + "']").each(function(i, item) {
|
var itemVal = $(item).val();
|
// 判断是否被选中
|
$(fillVal.split(",")).each(function(ii, iItem) {
|
if(itemVal == iItem) {
|
$(item).attr('checked', 'checked');
|
}
|
});
|
});
|
},
|
/**
|
* 多选下拉框-取值
|
*/
|
getMultipleSelectVal: function(key) {
|
var selected = [];
|
$("#" + key).find("option:selected").each(function() {
|
selected.push(this.value);
|
});
|
return selected.join(",");
|
},
|
/**
|
* 下拉搜索框-取值
|
*/
|
getSelectPickerVal: function(key) {
|
if(document.getElementById(key) != null && $("#" + key).selectpicker('val') != null) {
|
return $("#" + key).selectpicker('val').join();
|
}
|
return '';
|
},
|
/**
|
* 富文本编辑器-取值
|
*/
|
getEditorContent: function(obj) {
|
if(obj != null && obj != undefined){
|
return obj.getContent();
|
}
|
return '';
|
},
|
/**
|
* 时间格式化
|
*/
|
getDateFormat: function (value) {
|
if (value != "" && value != undefined) {
|
return new Date(value).format("Y-m-d"); // Y-m-d H:i:s
|
}
|
return "";
|
},
|
/**
|
* 文本缩略格式化
|
*/
|
getTextFormat: function (value, size) {
|
if(value != undefined && value.length > size){
|
return "<span title='"+value+"'>"+value.substring(0,size)+"...</span>";
|
}
|
return value;
|
},
|
/**
|
* 是/否判断格式化
|
*/
|
getIsObjFormat: function (value) {
|
if(value != undefined && value == 1){
|
return "是";
|
}
|
return "否";
|
},
|
/**
|
* 格式化:小于10进行补0显示。1 -> 01
|
*/
|
addPreZero: function (value) {
|
var format = '';
|
if (value <= 9) {
|
format = "0";
|
}
|
return format + value;
|
},
|
selectCity1: function() {
|
var code1 = $("#cityCode1").val();
|
$.ajax({
|
type: "GET",
|
url: Feng.ctxPath + "/cityRegion/getListByParentId",
|
data: {code1: code1},
|
success: function(data) {
|
var options = '<option value="-1">请选择</option>';
|
$(data).each(function (i, item) {
|
options += "<option value='" + item.code + "'>" + item.name + "</option>"
|
});
|
$("#cityCode2").html(options);
|
$("#cityCode").html('<option value="-1">请选择</option>');
|
}
|
});
|
},
|
selectCity2: function() {
|
var code2 = $("#cityCode2").val();
|
$.ajax({
|
type: "GET",
|
url: Feng.ctxPath + "/cityRegion/getListByParentId",
|
data: {code2: code2},
|
success: function(data) {
|
var options = '<option value="-1">请选择</option>';
|
$(data).each(function (i, item) {
|
options += "<option value='" + item.code + "'>" + item.name + "</option>"
|
});
|
$("#cityCode").html(options);
|
}
|
});
|
},
|
// 单选列表
|
get_radio_checkbox: function ( $el) { return !!$el[0]['checked']; },
|
i_checks_radio_init: function ( $elem, name, array, callback) {
|
$elem.append( (array||[]).map(function (val) {
|
var other = (val['others'] && val['others']())||"";
|
var $div = $('<div class="radio i-checks '+ (other ? "" : "inline") +'">' +
|
' <label>' +
|
' <input '+ (val.checked ? 'checked=""':'') +
|
' '+ (val.disabled ? 'disabled="disabled"':'') +' name="'+ name +'" type="radio" value="'+ val.value +'" />' +
|
' '+ val.name + '' +
|
' </label>' +
|
' <div style="display: inline; margin-left: 2em;">' + (other) + '</div>' +
|
'</div>');
|
$div.find( "input").on("ifChecked", function () { callback && callback( val); });
|
return $div;
|
}));
|
return {
|
getValue: function () {
|
var select;
|
$elem.find( "input[type='radio']").each(function () {
|
var checkbox = Feng.get_radio_checkbox( $( this));
|
if ( checkbox) { select = $( this).attr( "value"); }
|
});
|
return select;
|
}
|
}
|
},
|
|
/**
|
* 简化 表单验证方法
|
* @param isEmpty
|
* @param regexp
|
* @param regexp_msg
|
* @param callback
|
* @param map
|
* @returns {{validators: {}}}
|
*/
|
def_valid_info: function ( isEmpty, regexp, regexp_msg, callback, map) {
|
var vail = {};
|
if ( isEmpty) { vail.notEmpty = { message: "不能为空" } }
|
if ( regexp ) { vail.regexp = { regexp: regexp,message: regexp_msg} }
|
if ( callback) { vail.callback = callback;}
|
if ( map) {for (var key in map) {vail[ key] = map[ key];}}
|
return { validators: vail };
|
},
|
def_valid_money: function() {
|
return Feng.def_valid_info( true, /^((\d{1,10})|(0))(\.\d{1,2})?$/, "只能是数字,且最多保留两位小数")
|
},
|
def_valid_num: function() {
|
return Feng.def_valid_info( true, /^((\d{1,10})|(0))$/, "只能是整数")
|
},
|
check_form: function ( form_id) {
|
$('#' + form_id).data("bootstrapValidator").resetForm();
|
$('#' + form_id).bootstrapValidator('validate');
|
return $('#' + form_id).data('bootstrapValidator').isValid();
|
},
|
|
|
|
/**
|
* 获取select option 值 : 一般用于 有码表转换 然后查询条件有码表 直接在页面上获取值 不用后台再转一次
|
*/
|
getOptionItems: function( $id, isMap) {
|
var result = [], resMap = {};
|
$($id).find( "option").each(function (){
|
result.push( { value: this.value, name: this.text});
|
resMap[ this.value] = this.text;
|
});
|
return isMap ? resMap : result;
|
},
|
getOptionFormatter: function( $id) {
|
var optionItems = Feng.getOptionItems( $id, true);
|
return function( value) { return optionItems[ value] || ""}
|
},
|
getOrderTypeFormatter: function( key) {
|
//订单类型,0普通商品,1黄金套餐,2钻石套餐
|
var value = "普通商品";
|
if(key ==1){
|
value = "黄金套餐";
|
}else if(key ==2){
|
value = "钻石套餐";
|
}
|
return value;
|
},
|
getWorkDate: function( key) {
|
var value = "周一";
|
if(key ==2){
|
value = "周二";
|
}else if(key ==3){
|
value = "周三";
|
}else if(key ==4){
|
value = "周四";
|
}else if(key ==5){
|
value = "周五";
|
}else if(key ==6){
|
value = "周六";
|
}else if(key ==7){
|
value = "周日";
|
}
|
return value;
|
},
|
/**
|
* 下载导出
|
* @param url 请求URL
|
* @param data 请求参数
|
*/
|
down_export: function ( url, data) {
|
var params = "";
|
if ( data) {
|
params = Object.keys( data).map(function( k) { return k + "=" + data[ k]}).join( "&");
|
}
|
window.open( Feng.ctxPath + Feng.url_params( url, data));
|
},
|
url_params: function ( url, data) {
|
var params = "";
|
if ( data) {
|
params = Object.keys( data).map(function( k) { return k + "=" + data[ k]}).join( "&");
|
}
|
if ( url.indexOf("?") != -1) { return url + "&" + params; }
|
return url + "?" + params;
|
},
|
|
tableUserInfo: function( v, row) {
|
return [ "id:" + row['userId_showId'], "手机号:" + row[ 'userId_phone'], "昵称:" + row['userId_nickName']].join( "<br>")
|
},
|
|
get_select_option_values: function ( $id) {
|
var res = [];
|
$($id).find( "option").each(function () {
|
res.push( $( this).attr( "value"));
|
}); return res;
|
},
|
init_chosen: function( $id, options, onChange) {
|
var $elem = $( $id);
|
var default_setting = {no_results_text:"没有找到", search_contains:true, allow_single_deselect:true}
|
$elem.on( "change", onChange);
|
return $elem.chosen( $.extend( default_setting, options||{}));
|
},
|
get_chosen_selected: function( $id, callback) {
|
var selected = [];
|
$( $id).find("option:selected").each(function() {
|
selected.push( this.value);
|
callback && callback( this.value, this);
|
}); return selected.join( ",");
|
},
|
chose_change: function( $id, value) {
|
return $($id).val( value).trigger("chosen:updated");
|
},
|
|
to_br_formatter: function( sp, max) {
|
max = max||4;
|
return function (value) {
|
var strings = (value||"").split( (sp||/\,/g));
|
return strings.filter( function (val, index) {
|
return index < max
|
}).concat( strings.length > max ? ["..."]:[]).join( "<br>");
|
}
|
},
|
|
/**
|
* 商品 动态table 处理
|
* @param $id table dom 或 table id
|
*/
|
move_table: function( $id) {
|
var $elem = $( $id);
|
function get_sort_id ( array) {
|
if ( !array) { return array; }
|
if ( typeof array == "string") { array = array.split( ",") }
|
return array.sort( function (a, b) { return a - b; }).join( ",")
|
}
|
var Node = function( id, name, tId, parent) {
|
this.id = id;
|
this.name = name;
|
this.tId = tId;
|
this.parent = parent;
|
this.children = [];
|
this.$map = {};
|
parent && parent.add_child( this);
|
|
return this.init_$map();
|
}
|
Node.prototype = {
|
add_child:function( node) { this.children.push( node); },
|
is_last:function() { return this.children.length <= 0; },
|
get_name: function( tId) {
|
if ( this.tId == tId) { return this.name; }
|
return (this.$map[ tId]||{}).name;
|
},
|
init_$map: function () {
|
this.$item = {ids: [ this.id], names: [ this.name], $pIds: [ this.$pId]};
|
var parent = this.parent;
|
while ( parent) {
|
this.$map[ parent.tId] = parent;
|
this.$item.ids.push( parent.id);
|
this.$item.$pIds.push( parent.$pId);
|
this.$item.names.push( parent.name);
|
parent = parent.parent;
|
}
|
this.$mId = get_sort_id( this.$item.ids);
|
return this;
|
}
|
}
|
|
var Operation = function() {
|
this.$thead = this._read_$el( "thead");
|
this.$tbody = this._read_$el( "tbody");
|
this.items = [];
|
}
|
|
Operation.prototype = {
|
_read_$el: function( key) {
|
if ($elem.find(key).length <= 0) {
|
$elem.append( $( "<"+ key +">"));
|
} return $elem.find( key);
|
},
|
init: function( $aThead, tr_fun) {
|
this.$aThead = $aThead;
|
this.tr_fun = tr_fun;
|
|
return this._init_table();
|
},
|
append: function( item) {
|
if ( !item) { return this; }
|
var isFlag = this.items.filter( function( val) {
|
if ( val.id == item.id) {
|
val.values = item.values;
|
return true;
|
} return false;
|
}).length > 0;
|
isFlag || this.items.push( item);
|
|
return this._init_table();
|
},
|
|
|
_init_table: function() {
|
var self = this, values = this._get_values();
|
this.$thead.empty().append( $('<tr>')
|
.append( self._item_map( function( val) {
|
return $('<th>').text( val.name); }).concat( self.$aThead)));
|
this.$tbody.empty().append( values.map(function (node) {
|
return $( "<tr>").data( node).append(self._item_map(function( val) {
|
return $('<td>').data( val).text( node.get_name( val.id));
|
}).concat( self.tr_fun( node)));
|
}));
|
if ( values.length <= 0) {
|
this.$tbody.append( "<tr><td colspan='10000000'>暂无规格数据</td></tr>")
|
}
|
|
$elem.find( "td,th").addClass( "text-center");
|
return this;
|
},
|
_item_map: function( callback) {
|
return this.items.map(function (item) { return callback( item);});
|
},
|
_get_values: function() {
|
var result = [];
|
this._item_map(function( val) {
|
var is_move = result.length <= 0,
|
c_result = [].concat( result);
|
(val.values||[]).forEach(function( v) {
|
if ( is_move) {
|
result.push( new Node( v.id, v.name, val.id, null))
|
} else {
|
c_result.forEach( function( node) {
|
result.push( new Node( v.id, v.name, val.id, node));
|
});
|
}
|
});
|
})
|
return result.filter( function( node){ return node.is_last() });
|
},
|
|
clear: function() {
|
this.items = [];
|
return this._init_table();
|
},
|
|
get_trs: function() {
|
return this.$tbody.find( 'tr');
|
}
|
}
|
|
return new Operation();
|
},
|
};
|