/**
*<p>Copyright 2009-2014 Hikvision Digital Technology Co., Ltd.</p>
*<p>Copyright 2012 系统公司/金融事业部/业务平台组</p>
*@Author: wenliang (wenliang@hikvision.com.cn)
*@Date: 2014-04-12下午15:22:53
*@Version: 1.0
*@Description:对前台工具函数的统一封装,工具函数包括字符串处理、日期处理、普通对话框、模式对话框、
*/
var FinanceUtil = FinanceUtil || {} ;
/**
* 弹出框
* @param {Object} option
* @param {Object} frames
* @memberOf {TypeName}
* @return {TypeName}
*/
FinanceUtil.dialog = function(option, frames) {
option = option || {};
var el = null;
if (option.iframeSrc) {
if (option.iframeSrc.indexOf('/') != 0 && option.iframeSrc.indexOf('http://') != 0) {
var baseEl = $('base');
var baseHref = baseEl.length ? baseEl.attr('href') : '';
option.iframeSrc = baseHref + option.iframeSrc;
}
el = $([
'<div class="dialog" style="padding:0;overflow:hidden;"><div class="loading-overlay"><div class="loading-m"><i></i><span>'+lan.text('loading')+'</span></div><div class="shadow"></div></div><iframe src="',
option.iframeSrc, '" dialogIframe="true" frameborder="0" scrolling="auto" class="autoIframe"></iframe></div>' ].join(''));
if (option.id) {
el.find('iframe').attr({
id : option.id,
name : option.id
});
}
el.appendTo('body');
el.find('iframe').on('load', function() {
el.find('.loading-overlay').hide();
option.load && option.load(window.frames[option.id].$.callback);
});
} else if (option.ajax) {
el = $(['<div class="dialog" style="overflow:hidden;"><div class="loading-overlay"><div class="loading-m"><i></i><span>'+lan.text('loading')+'</span></div><div class="shadow"></div></div></div>'].join('')).appendTo('body');
if (option.id) {
el.attr('id', option.id);
}
if (option.ajax.url.indexOf('/') != 0 && option.ajax.url.indexOf('http://') != 0) {
var baseEl = $('base');
var baseHref = baseEl.length ? baseEl.attr('href') : '';
option.ajax.url = baseHref + option.ajax.url;
}
option.load && option.load();
var success = option.ajax.success;
option.ajax.success = function() {
var result = success && success.apply(this, arguments);
el.find('.loading-overlay').hide();
return result;
};
el.load(option.ajax.url,option.ajax.param || {}, option.ajax.success);
} else if (option.el) {
el = $(option.el).clone();
el.appendTo('body');
} else {
return;
}
var close = option.close;
option.close = null;
delete option.close;
option = $.extend({
autoOpen : true,
modal : true,
frames : frames,
resizable : false,
draggable: true,
width : 500,
height : 500,
closeDestroy:true,
close : function() {
el.find('iframe').attr('src', /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank');
if (close && option.maybeRefresh) {
var frames = option.frames;
var frame = window;
for ( var i = frames.length; i > 0; i--) {
frame = frame.frames[frames[i - 1]];
}
var callback = window.frames[option.id];
if (callback.$) {
callback = callback.$;
if (callback.callback) {
callback = callback.callback;
}
}
(function(frame, callback) {
var f = new frame.Function('var t = ' + close.toString() + ';return t.apply(this,arguments);');
f.call(this, callback);
}(frame, callback));
} else {
close && close.apply(this, arguments);
}
el.remove();
el = null;
}
}, option);
return el.dialog(option);
};
// Loader 动态加载组件
(function($) {
FinanceUtil.loader = {
show : function(title, callback) {
var text = title || lan.text('loader.waiting');
var html = $('<div id="loaderTop" class="loading-overlay"><div class="loading-m"><i></i><span>' + text + '</span></div><div class="shadow"></div></div>');
if ($('#loaderTop').length > 0)
$('#loaderTop').remove();
$('body').append(html);
if ($.fn.bgiframe) {
$("#loaderTop").bgiframe();
}
var w = $('.loading-m', html).outerWidth(), ml = parseInt(w) / 2, h = $('.loading-m', html).outerHeight(), mt = parseInt(h) / 2;
$('.loading-m', html).css({
marginTop : '-' + mt + 'px',
marginLeft : '-' + ml + 'px'
});
try {
if (callback)
callback();
} catch (e) {
}
},
hide : function(callback) {
$('#loaderTop').fadeOut('slow', function() {
$(this).remove();
try {
if (callback)
callback();
} catch (e) {
}
});
}
}
})(jQuery);
// Progress 动态加载组件
(function($) {
FinanceUtil.progress = {
show : function(percent, elapsed, callback) {
var elapsed = elapsed || lan.text('loader.waiting');
var percent = percent || 0;
var html = $('<div id="progressTop" class="progress-overlay"><div class="progress-m"><div class="progressbar"><div class="progress progress-small progress-info"><div class="filler"><div class="bar" style="width:'
+ percent
+ '%"> </div></div></div><div class="percent"><b>'
+ percent
+ '%</b></div><div class="elapsed">'
+ elapsed
+ '</div><div class="actions clear" style="display:none;"><a id="btn_progress_hide" href="javascript:void(0);" class="buttonS bBlue">'+lan.text('loader.background')+'</a></div></div></div><div class="shadow"></div></div>');
if ($('#progressTop').length > 0)
$('#progressTop').remove();
$('body').append(html);
if ($.fn.bgiframe) {
$("#progressTop").bgiframe();
}
if (callback)
callback();
},
update : function(percent, elapsed, callback) {
$('#progressTop .bar').css('width', percent + '%');
$('#progressTop .percent b').text(percent + '%');
$('#progressTop .elapsed').text(elapsed);
if (callback)
setTimeout(function() {
callback();
}, 1000);
},
showButton : function(callback) {
$('#progressTop .actions').show('fade');
$('#btn_progress_hide').on('click', function() {
progress.hide(callback);
});
},
hide : function(callback) {
$('#progressTop').fadeOut('slow', function() {
$(this).remove();
if (callback)
callback();
});
}
}
})(jQuery);
/*
* @Author chenliujie
* 初始化指定DOM元素内的表单元素样式
* @param {Object} selector 父元素选择器
*/
FinanceUtil.initFormUI = function(selector){
if(!selector || selector == ''){
return;
}
//初始化select、file
$(selector).find('select, .file :file, .check :checkbox, .check :radio').uniform({
selectAutoWidth: false
});
//初始化日期控件
$(selector).find('input[hikui="calendar"]').each(function(){
$(this).hik().calendar();
})
//初始化iButton
$(selector).find('.on_off :checkbox, .on_off :radio').iButton({
labelOn: lan.text('ibutton.open'),
labelOff: lan.text('ibutton.close')
});
//初始ip地址输入控件
/* $(selector).find('.ipv4').ipAddress();*/
//初始化tips
$(selector).find('.tipS').tipsy({
gravity : 's',
html : true
});
//初始化timepicker
$(selector).find('input.timepicker').timeEntry();
//初始化form
if($(selector).is('form')){
$(selector).ajaxForm({ajaxSubmit:false});
}else{
$('form', selector).ajaxForm({ajaxSubmit:false});
}
};
/*
* @Author chenliujie
* 扩展validate组件的validate方法,支持form表单的自动验证
* @param {Object} targetEl 需要验证的对象,若为form,自动验证form内所有有auto-input类的表单元素(可用元素,没有disabled)
* @param {String} msg 若msg不为空,直接标记验证对象为输入错误,且显示msg为提示信息
*/
FinanceUtil.validate = function(targetEl, msg){
var result = true;
if(!targetEl.is('.auto-input')){
var autoinputs = targetEl.find('.auto-input').not(':disabled');
$.each(autoinputs, function(i, input){
if(!$(input).validate(msg)){
result = false;
}
})
}else{
result = targetEl.validate(msg);
}
return result;
};
/*
* @Author chenliujie
* 设置select组件的选择项
* @param {Object} selectEl select的jquery对象,需要已经uniform初始化
* @param {Array} data 数据
* @param {String | false} 添加默认第一项的值,若false,表示不添加
* @param {String} displayName 显示属性, 默认name
* @param {String} valueName 值属性, 默认value
*/
FinanceUtil.setSelectOptions = function(selectEl, data, head ,displayName, valueName){
selectEl.html('');
if(head !== false){
$('<option></option>').text(head[0]).attr('value', head[1]).appendTo(selectEl);
}
$.each(data, function(i, e){
$('<option></option>').text(e[displayName]).attr({
'value': e[valueName],
'title': e[displayName]
}).appendTo(selectEl);
})
selectEl.uniform.update();
};
/*
* @Author chenliujie
* 格式化字符串、数字,不足位数补0
* @param {String || Number} str
* @param {Number} lenght 格式化的位数
* @return {String} 格式化后的字符串
*/
FinanceUtil.padLeft = function(str, lenght) {
var str = str.toString();
if (str.length >= lenght)
return str;
else
return FinanceUtil.padLeft("0" + str, lenght);
}
/*
* @Author chenliujie
* 重新加载标签内容信息
* @param {Object} tabs对象
*/
FinanceUtil.reloadTabsInfo = function(tabs){
var src = tabs.children('ul.tabs').children('li[aria-selected="true"]').children('a').attr('src');
delete top.pt.historyMap[src];
window.frames['tabs-info'].document.location = src;
}
/*
* @Author wenliang
* 重新解析加载grid的内容
* 适用于左侧手风琴、右侧数据列表的左右布局
*/
FinanceUtil.reloadAccordionInfo = function(){
var src = $('#content').find('div.col-sub').find('dl.open').children('dt').attr('url');
delete top.pt.historyMap[src];
window.frames['grid-iframe'].document.location = src;
}
/*
* @Author chenliujie
* 刷新当前数据列表,保留过滤条件和分页信息
*/
FinanceUtil.refreshIframe = function(){
$('#content').find('iframe.autoIframe').contents().find('.filterForm form').submitHistory();
}
/*
* @Author chenliujie
* 刷新当前数据列表,保留过滤条件和分页信息
*/
FinanceUtil.refreshIframetwo = function(){
$('#content').find('iframe.autoIframeTwo').contents().find('.filterForm form').submitHistory();
}
/*
* @Author wenliang
* DOM元素动态加载界面,重新封装了$.load方法,增加了加载进度提示
* @param {Object} selector jquery 选择器表达式
* @param {Object} url 请求地址
* @param {Object} data 请求数据
* @param {Object} callback 回调函数
*/
FinanceUtil.load = function(selector,url,data,callback){
FinanceUtil.loader.show();
$(selector).load(url,data,function(){
FinanceUtil.loader.hide();
if(callback!=null){callback();}
});
}
/*
* 创建xml文档
* @return {TypeName}
*/
FinanceUtil.createXML = function(){
var xmlObj = new ActiveXObject("Microsoft.XMLDOM");
var p = xmlObj.createProcessingInstruction('xml', 'version="1.0" encoding="utf-8"');
xmlObj.appendChild(p);
return xmlObj;
};
/*
* 创建xml节点
* @param {Object} xmlObj
* @param {Object} nodeName
* @param {Object} text
* @param {Object} arr
* @return {TypeName}
*/
FinanceUtil.createElement = function(xmlObj,nodeName,text,arr){
var Properties = xmlObj.createElement(nodeName);
if(arr!=null&&arr!=""){
for(var i=0;i<arr.length;i++){
if(arr[i][1]==null){
Properties.setAttribute(arr[i][0],0);
continue;
}
Properties.setAttribute(arr[i][0],arr[i][1]);
}
}
text = text+"";
if(text!=null&&text!=""){
Properties.appendChild(xmlObj.createTextNode(text));
}
return Properties;
};
/*
* 加载xml文件
* @param {Object} str
* @return {TypeName}
*/
FinanceUtil.loadxml = function(str){
if (window.ActiveXObject!==undefined) //ie内核的浏览器
{
var doc = new ActiveXObject( "Msxml2.DOMDocument" );
doc.loadXML(str);
return doc;
}
else if(window.DOMParser)//firefox内核的浏览器
{
var p = new DOMParser();
return p.parseFromString( str, "text/xml" );
}
return false;
};
/*
* @Author chenliujie
* 公共删除方法
* @param {Object} options 配置项,url-请求路径,data-传递参数,content-提示内容
* @param {function} callback 回调函数
*/
FinanceUtil.deleteSth = function(options, callback){
jConfirm(options.content || lan.text('dialog.confirm.delete'), lan.text('dialog.title.confirm'), function(r){
if(r){
FinanceUtil.loader.show(lan.text('dialog.content.waiting'));
$.ajax({
url: options.url,
data: options.data,
success: function(data){
FinanceUtil.loader.hide();
if(data && data.success){
if(typeof callback === 'function'){
callback(data);
}
}else if(data && data.msg){
jAlert(data.msg, lan.text('ajax.error_title'), 'error');
}
}
});
}
})
}
/**
* 获取其余的高度值
* _self,元素自己
* _centerEl 父容器元素,不传则为body
**/
FinanceUtil.getCenterHeight = function(_self,_centerEl){
if(_self==null||_self.length==0){
return 0;
}
if(_centerEl==null||(_centerEl.length==0)){
_centerEl = $('body');
}
var _list,i,_tmpEl = _self.get(0),_height=_centerEl.innerHeight(),_p=_centerEl.get(0);
/* _height -=parseInt(_centerEl.css('padding-top'))-1;
_height -=parseInt(_centerEl.css('padding-bottom'))-1;
_height -=parseInt(_centerEl.css('margin-top'))-1;
_height -=parseInt(_centerEl.css('margin-bottom'))-1;
_height -=parseInt(_centerEl.css('border-top'))-1;
_height -=parseInt(_centerEl.css('border-bottom'))-1;*/
while(_tmpEl!=$('body').get(0)&&_tmpEl!=_p){
_list = $(_tmpEl).parent().children();
for(i=0;i<_list.length;i++){
if(_list.get(i)==_tmpEl||
$(_list.get(i)).css('position')=='absolute'||
$(_list.get(i)).is(':hidden')||
$(_list.get(i)).css('display')=='none'
)continue;
_height -= $(_list[i]).outerHeight(true);
}
_tmpEl = $(_tmpEl).parent().get(0);
if($(_tmpEl).css('position')=='absolute'||
$(_tmpEl).is(':hidden')||
$(_tmpEl).css('display')=='none')
break;
}
delete _tmpEl;
delete _list;
delete _self;
delete _p;
return _height;
}
/**
* 获取相对今天的日期
* @param {Object} num
* @return {TypeName}
*/
FinanceUtil.getNewDate = function(num){
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
return new Date(y, m, d + num, 0, 0);
}
[HK]常用工具类
最新推荐文章于 2023-02-26 12:07:16 发布