/*!
* 应用通用功能
* 米拓企业建站系统 copyright (c) 长沙米拓信息技术有限公司 (https://www.metinfo.cn). all rights reserved.
*/
// 判断地址栏是否有lang参数,没有则跳转到带lang参数的地址
if(typeof met !='undefined' && met['url']['basepath'] !='undefined'){
try {
if(window.parent){
var str=window.parent.document.url,
s=str.indexof("lang="+m['lang']),
z=str.indexof("lang");
if (s=='-1' && z!='-1') {
var s1=str.indexof('#');
if (s1=='-1') {
str=str.replace(/(lang=[^#]*)/g, "lang="+m['lang']+"#");
}
str=str.replace(/(lang=[^#]*#)/g, "lang="+lang+"#");
parent.location.href=str;
}
}
} catch (err) {}
}
// 获取地址栏参数
function getquerystring(name) {
var reg=new regexp("(^|&)"+name+"=([^&]*)(&|$)", "i");
var r=window.location.search.substr(1).match(reg);
if (r!=null) return unescape(decodeuricomponent(r[2]));
return null;
}
// 修改、添加、删除地址栏参数
function replaceparamval(param,value) {
var url=location.href,
match_url=window.location.search.substr(1) || window.location.hash.split('?')[1]||'',
param=$.isarray(param)?param:[param],
value=$.isarray(value)?value:[value];
$.each(param, function(index, val) {
var param1='&' + val + '=',
param2='?' + val + '=',
re = match_url.match(new regexp("(^|&)"+val+"=([^&]*)(&|$)", "i"));
if(!re && (url.indexof(param1)>0 || url.indexof(param2)>0)) re=[val + '='];
re && (re[0]=re[0].replace(/&/g,''));
value[index]=string(value[index]);
if(value[index]){
if(re){
if(url.indexof(param1)>0){
url = url.replace('&'+re[0], param1 + value[index]);
}else if(url.indexof(param2)>0){
url = url.replace('?'+re[0], param2 + value[index]);
}
}else{
if(url.indexof('?')>0){
var laststr=url.substr(-1),
urls=url.split('?');
if(urls[urls.length-1].indexof('#')>0 && laststr!='/'){
url+='/?';
laststr=url.substr(-1);
}
url = url+((laststr=='?'||laststr=='&')?(val + '='):param1) + value[index];
}else{
if((url+'/')==m.weburl) url+='/';
var laststr=url.substr(url.lastindexof('/')+1);
if(laststr.length?laststr.indexof('.')>0:1){
url=url+param2 + value[index];
}else{
url=url+'/'+param2 + value[index];
}
}
}
}else if(re){
url = url.replace('&'+re[0], '').replace(re[0], '');
}
})
history.pushstate('','',url);
}
try {
if(window.parent){
// 可视化弹框中页面隐藏头部
if (parent.window.location.search.indexof('pageset=1') >= 0) $('.metadmin-head').hide();
// 操作成功、失败提示信息
if(top.location!=location) $("html",parent.document).find('.turnovertext').remove();
}
} catch (err) {}
// 弹出提示信息
function metalert(text,delay,bg_ok,type){
delay=typeof delay != 'undefined'?delay:2000;
bg_ok=bg_ok?'bgshow':'';
if(bg_ok){
$('.metalert-text').remove();
}else{
$('.metalert-wrapper').remove();
}
if(text!=' '){
text=text||metlang.jsok;
text='
暂无可选择图片,请上传图片
';
$loader.hide();
$list.html(html).find('[data-original]').metlazyload({container:'.img-library-body'});
$('.img-library-modal').removeattr('data-update');
}
}
});
}else{
$('.img-library-modal .img-library-list li a').removeclass('active');
}
});
// 图片库-选择图片
$(document).on('click', '.img-library-modal .img-library-list li a', function(event) {
var multiple=$('.file-input #'+$('.img-library-modal button[type="submit"]').attr('data-id')).attr('multiple')?true:false;
$(this).toggleclass('active');
if(!multiple) $(this).parents('li').siblings('li').find('a').removeclass('active');
})
// 图片库-提交
$(document).on('click', '.img-library-modal button[type="submit"]', function(event) {
var $self=$(this),
$img_library_modal=$('.img-library-modal'),
img_url='';
$img_library_modal.find('.img-library-list li a.active img').each(function(index, el) {
img_url+=(index?',':'')+$(this).attr('src');
});
metalertifyloadfun(function(){
if(img_url){
$('.file-input #'+$self.attr('data-id')).metfileinputchange(img_url);
$img_library_modal.modal('hide');
alertify.success('图片选择成功');
}else{
alertify.error('请选择图片');
}
});
});
// 外部图片-弹框
$(document).on('click', '.file-input .fileinput-file-other', function(event) {
if(!$('.img-other-modal').length){
var html='
';
$('body').append(html);
$('.img-other-modal').modal();
}
$('.img-other-modal [name="img_url"]').val('');
$('.img-other-modal button[type="submit"]').attr({'data-id':$(this).parents('.file-input').find('input[type="file"]').attr('id')});
});
// 外部图片-提交
$(document).on('click', '.img-other-modal button[type="submit"]', function(event) {
var $self=$(this),
$img_other_modal=$('.img-other-modal'),
img_url=$img_other_modal.find('[name="img_url"]').val();
metalertifyloadfun(function(){
if(img_url){
$('.file-input #'+$self.attr('data-id')).metfileinputchange(img_url);
$img_other_modal.modal('hide');
alertify.success('图片添加成功');
}else{
alertify.error('请输入外部图片链接');
$img_other_modal.find('[name="img_url"]').focus();
}
});
});
}
},
// 上传图片组件改变值
metfileinputchange:function(img_url){
var $file_input=$(this).parents('.file-input'),
name=$(this).attr('name'),
html='',
img_urls=img_url.indexof(',')>=0?img_url.split(','):[img_url];
if($(this).attr('multiple')){
var old_val=$file_input.find('input[type="hidden"][name="'+name+'"]').val();
if(old_val){
old_val=old_val.indexof(',')>=0?old_val.split(','):[old_val];
img_url=old_val+','+img_url;
}else{
old_val=[];
}
if(old_val!=img_urls) img_urls=old_val.concat(img_urls);
}
$.each(img_urls, function(index, val) {
html+='
'
+'
'
+''
+'
';
});
if(html && $file_input.hasclass('file-input-new')) $file_input.removeclass('file-input-new');
$file_input.find('.file-drop-zone .file-drop-zone-title').remove();
$file_input.find('.file-preview-thumbnails').html(html);
$file_input.find('.input-group .file-caption-name').html('
'+img_url).attr({title:img_url});
$file_input.find('input[type="hidden"][name="'+name+'"]').val(img_url).trigger('change');
},
// 单选、多选默认选中
metradiocheckboxchecked:function(){
if(!$(this).length) return;
$(this).each(function(index, el) {
var checked=string($(this).attr('data-checked')),
delimiter=$(this).data('delimiter')||'#@met@#';
if(checked !='undefined'){
checked=checked.indexof(delimiter)>=0?checked.split(delimiter):[checked];
var name=$(this).attr('name');
$(this).parents('form').find('input[name="'+name+'"]').removeattr('checked');
for (var i=0; i < checked.length; i++) {
$(this).parents('form').find('input[name="'+name+'"][value="'+checked[i]+'"]').attr('checked', true).prop({checked:true});
}
}
});
},
// 下拉菜单默认选中
metselectchecked:function(){
if(!$(this).length) return;
$(this).each(function(index, el) {
$('option[value="'+$(this).attr('data-checked')+'"]',this).attr({selected:true});
});
},
// 图片延迟加载
metlazyload:function(options){
if(!$(this).length) return;
var $self=$(this);
metfileloadfun(m['plugin']['lazyload'],function(){
return typeof $.fn.lazyload=='function';
},function(){
$self.lazyload(options);
});
},
// 表单删除按钮ajax提交
metformajaxdel:function(url){
var $form=$(this).parents('form'),
del_id=$form.find('[name="all_id"]')?$form.find('[name="all_id"]').val():'';
if(del_id!=''){
$.ajax({
url: $(this).data('url')||$form.attr('action'),
type: "post",
datatype:'json',
data:{del_id:del_id},
success: function(result){
metajaxfun({result:result});
}
});
}else{
metalert(metlang.jslang3,'','bgshow',0);
}
},
// 表单两种类型提交前的处理(保存、删除)
metsubmit:function(type){
// 插入submit_type字段
var type=typeof type!='undefined'?type:1,
type_str=type?'save':'delet';
if($(this).find('[name="submit_type"]').length){
$(this).find('[name="submit_type"]').val(type_str);
}else $(this).append('
');
// 插入表格的all_id字段
if($(this).find('table').length){
var $table=$(this).find('table'),
checked_str=type?'':':checked',
$checkbox=$table.find('tbody input[type="checkbox"][name="id"]'+checked_str),
all_id='';
$checkbox.each(function(index, el) {
all_id+=all_id?','+$(this).val():$(this).val();
})
if(!$(this).find('[name="all_id"]').length) $(this).append('
');
$(this).find('[name="all_id"]').val(all_id);
}
},
// 表单更新验证
metformaddfield:function(name){
var $form=$(this)[0].tagname=='form'?$(this):$(this).parents('form');
if($form.length){
if(name){
if(!$.isarray(name)){
if(name.indexof(',')>=0){
name=name.split(',');
}else name=[name];
}
$.each(name, function(index, val) {
$form.data('formvalidation').addfield(val);
})
}else{
var name_array=[];
$('[name]',this).each(function(index, el) {
var name=$(this).attr('name');
if($.inarray(name, name_array)<=0){
name_array.push(name);
if(typeof $(this).attr('required') !='undefined'){
$form.data('formvalidation').addfield(name);
}else{
$.each($(this).data(), function(index, val) {
var third_str=index.substr(2,1);
if(index.substr(0,2)=='fv' && index.length>2 && third_str >= 'a' && third_str <= 'z'){
$form.data('formvalidation').addfield(name);
return false;
}
});
}
}
});
}
}
},
// 点击ajax请求弹出确认框后以及返回结果通用处理
metclickconfirmajax:function(default_options){
var default_options = $.extend({
ajax_data:'',
true_text:metlang.confirm,
false_text:metlang.cancel,
confirm_text:metlang.delete_information,
true_fun:function(){
var url=typeof this.url=='function'?this.url():this.url,
ajax_data=typeof this.ajax_data=='function'?this.ajax_data():this.ajax_data,
options_this=this;
$.ajax({
url: url,
type: ajax_data?'post':'get',
datatype: 'json',
data:ajax_data,
success:function(result){
options_this.ajax_fun(result);
}
});
},
false_fun:'',
ajax_fun:function(result){
metajaxfun({result:result});
}
},default_options);
$(document).on('click', this.selector, function(event) {
var options = $.extend({
el:$(this),
url:$(this).data('url')
},default_options);
metalertifyloadfun(function(){
var confirm_text=typeof options.confirm_text=='function'?options.confirm_text():options.confirm_text;
alertify.okbtn(options.true_text).cancelbtn(options.false_text).confirm(confirm_text, function (ev) {
options.true_fun();
},function(){
if(typeof options.false_fun=='function') options.false_fun();
});
})
});
},
// 通用功能开启
metcommon:function(){
var dom=this;
// 表单验证
if($('form',dom).length){
if(typeof validate =='undefined'){
$.include(m['plugin']['formvalidation']);
}else{
$(dom).metvalidate();
}
}
// ajax表格
if($('.datatable',dom).length){
if(typeof datatable =='undefined'){
$.include(m['plugin']['datatables']);
}else{
$(dom).metdatatable();
}
}
// 编辑器
if($('textarea[data-plugin="editor"]',dom).length && typeof met['url']['basepath']!='undefined') $('textarea[data-plugin="editor"]',dom).meteditor();
// 颜色选择器
if($('input[data-plugin="minicolors"]',dom).length) $.include(m['plugin']['minicolors'],function(){
$('input[data-plugin="minicolors"]',dom).minicolors();
});
// 标签
if($('input[data-plugin="tokenfield"]',dom).length) $.include(m['plugin']['tokenfield'],'','siterun');
// 滑块
if($('input[type="text"][data-plugin="ionrangeslider"]',dom).length) $.include(m['plugin']['ionrangeslider'],'','siterun');
// 日期选择器
if($('input[data-plugin="datetimepicker"]',dom).length) $.include(m['plugin']['datetimepicker'],function(){
$('input[data-plugin="datetimepicker"]',dom).metdatetimepicker();
});
// 联动菜单
if($('[data-plugin="select-linkage"]',dom).length) $.include(m['plugin']['select-linkage'],function(){
$('[data-plugin="select-linkage"]',dom).metcityselect();
});
// 模态对话框
if($('[data-plugin="alertify"]',dom).length) $.include(m['plugin']['alertify'],'','siterun');
// 全选、全不选
if($('[data-plugin="selectable"]',dom).length) $.include(m['plugin']['selectable'],'','siterun');
// 上传文件
if($('input[type="file"][data-plugin="fileinput"]',dom).length) $.include(m['plugin']['fileinput'],function(){
$('input[type="file"][data-plugin="fileinput"]',dom).metfileinput();
})
// 滚动条
if($('[data-plugin="scrollable"]',dom).length) $.include(m['plugin']['asscrollable'],'','siterun');
// 单选、多选默认选中
if($('input[data-checked]',dom).length) $('input[data-checked]',dom).metradiocheckboxchecked();
// 下拉菜单默认选中
if($('select[data-checked]',dom).length) $('select[data-checked]',dom).metselectchecked();
// 数量改变
if($('[data-plugin="touchspin"]',dom).length && typeof $.fn.touchspin =='undefined') $.include(m['plugin']['touchspin'],function(){
$('[data-plugin="touchspin"]',dom).touchspin();
});
// 图片延迟加载
if($('[data-original]',dom).length && dom!=document) $('[data-original]',dom).metlazyload();
}
});
// 通用功能开启
$(document).metcommon();
// 勾选开关
$(document).on('change', 'input[type="checkbox"][data-plugin="switchery"]', function(event) {
var val=$(this).is(':checked')?1:0;
$(this).val(val);
});
// tokenfield插件输入框值更新后
$(document).on('change', '.tokenfield .token-input', function(event) {
$(this).parents('.tokenfield').find('[name][data-fv-field]').trigger('change');
});
$(function(){
// 非前台模板页面-兼容老模板
if(m['url']['basepath'] || $('script[src*="js/basic_web.js"]').length){
// 返回顶部
$(".met-scroll-top").click(function(){
$('html,body').animate({scrolltop:0},300);
});
// 返回顶部按钮显示/隐藏
var wh=$(window).height();
$(window).scroll(function(){
if($(this).scrolltop()>wh){
$(".met-scroll-top").removeattr('hidden').addclass("animation-slide-bottom");
}else{
$(".met-scroll-top").attr({hidden:''}).removeclass('animation-slide-bottom');
}
});
}
// 会员侧栏手机端当前页面标题显示在导航徒步
breakpoints.on('xs sm',{
enter:function(){
if($('.met-sidebar-nav-active-name').length) $('.met-sidebar-nav-active-name').html($('.met-sidebar-nav-mobile .dropdown-menu .dropdown-item.active').text());
}
})
// 在&pageset=1弹窗中时,页面的表单提交地址添加参数pageset=1
if(getquerystring('pageset')) $('form').each(function(index, el) {
if($(this).attr('action')) $(this).attr({action:$(this).attr('action')+'&pageset=1'});
});
// 下拉展开时下拉图标旋转
$(document).on('click', '[data-toggle="collapse"]', function(event) {
var $icon=$('.icon[class*="fa-angle-"]',this);
if($icon.length){
if(!$icon.hasclass('transition500')) $icon.addclass('transition500');
if($($(this).data('target')).height()){
$icon.removeclass('fa-rotate-90');
}else{
$icon.addclass('fa-rotate-90');
}
}
});
// 表单功能
// 添加项
$(document).on('click', '[table-addlist]', function(event) {
var $self=$(this),
$table=$(this).parents('table').length?$(this).parents('table'):$($(this).data('table-id')),
addlist=function(data){
$table.find('tbody').append(data);
var $new_tr=$table.find('tbody tr:last-child');
if(!$new_tr.find('[table-cancel]').length && typeof $self.data('nocancel')=='undefined') $new_tr.find('td:last-child').append('
');
// 添加表单验证
$new_tr.metformaddfield();
};
if($table.find('[table-addlist-data]').length){
var html=$table.find('[table-addlist-data]').val();
addlist(html);
}else{
if(typeof datatable_option=='undefined') window.datatable_option=[];
var datatable_index=$table.index('.datatable');
if(typeof datatable_option[datatable_index]=='undefined') datatable_option[datatable_index]=[];
if(typeof datatable_option[datatable_index]['new_id']=='undefined'){
datatable_option[datatable_index]['new_id']=0;
}else{
datatable_option[datatable_index]['new_id']++;
}
$.ajax({
url: $(this).data('url'),
type: 'post',
data:{new_id:datatable_option[datatable_index]['new_id']},
success:function(result){
addlist(result);
}
});
}
});
// 撤销项
$(document).on('click', '[table-cancel]', function(event) {
$(this).parents('tr').remove();
})
// 删除项-不提交
$(document).on('click', '[table-del]', function(event) {
var $self=$(this),
remove=function(){
alertify.theme('bootstrap').okbtn(metlang.confirm).cancelbtn(metlang.cancel).confirm(metlang.delete_information, function (ev) {
$self.parents('tr').remove();
})
};
metalertifyloadfun(function(){
remove();
});
})
// 点击保存按钮
$(document).on('click', 'form .btn[type="submit"]', function(event) {
if($(this).data('plugin')=='alertify' && $(this).data('type')=='confirm') event.preventdefault();
$(this).parents('form').metsubmit();
})
// 删除多项提交
$(document).on('click', '[table-delet]', function(event) {
event.preventdefault();
var $form=$(this).parents('form');
$form.metsubmit(0);
if($(this).data('plugin')!='alertify'){
if($(this).data('url')){
$(this).metformajaxdel();
}else $form.submit();
}
})
// 表单输入框回车时,触发提交按钮
$(document).on('keydown', 'form input[type="text"]', function(event) {
if(event.keycode==13){
event.preventdefault();
$(this).parents('form').find('[type="submit"]:not(.fv-hidden-submit)').click();
}
});
// 表单提交
$(document).on('submit', 'form', function(event) {
// 提交删除时没有勾选时提示
if($(this).find('[name="submit_type"]').length && $(this).find('[name="submit_type"]').val()=='delet' && $(this).find('[name="all_id"]').val()==''){
event.preventdefault();
metalert(metlang.jslang3,'','bgshow',0);
}
});
});
// 判断是否加载了formvalidation后回调
function metformvalidationloadfun(fun){
metfileloadfun(m['plugin']['formvalidation'],function(){
return typeof $.fn.metvalidate=='function';
},function(){
if(typeof fun=='function') fun();
});
}
// 判断是否加载了alertify后回调
function metalertifyloadfun(fun){
metfileloadfun(m['plugin']['alertify'],function(){
return typeof alertify!='undefined';
},function(){
if(typeof fun=='function') fun();
});
}
// ajax请求返回后通用处理
function metajaxfun(options){
options = $.extend({
result:'',
false_fun:'',
true_fun:'',
status_key:'status',
msg_key:'msg',
true_val:function(){
return parseint(options.result[options.status_key]);
}
},options);
metalertifyloadfun(function(){
if(options.true_val()){
if(typeof options.result[options.msg_key]!='undefined' && options.result[options.msg_key]!='') alertify.success(options.result[options.msg_key]);
if(typeof options.true_fun=='function'){
options.true_fun();
}else{
settimeout(function(){
location.reload();
},1000);
}
}else{
if(typeof options.result[options.msg_key]!='undefined' && options.result[options.msg_key]!='') alertify.error(options.result[options.msg_key]);
if(typeof options.false_fun=='function') options.false_fun();
}
});
}
// 设置cookie
function setcookie(name,value,path,term){
var exp = new date(),
terms =term||30,
paths =path||'/';
exp.settime(exp.gettime() + terms*24*60*60*1000);
document.cookie = name + "="+ value + ";path="+paths+";expires=" + exp.togmtstring();
}
// 获取指定名称的cookie的值
function getcookie(name) {
var cookie_str = document.cookie.split(";");
for (var i = 0; i < cookie_str.length; i++) {
cookie_str[i]=$.trim(cookie_str[i]);
var index = cookie_str[i].indexof("="),
cookie_name = cookie_str[i].substring(0, index);
if (cookie_name == name) {
var temp = cookie_str[i].substring(index + 1);
return decodeuricomponent(temp);
}
}
}