/*
* Balancebeam Toolkit 1.0
*
* Description : Various tools
* Copyright 2011
* License : MIT
* Author : yangzz
* Mail : balancebeam@163.com
*
* Depends:
* balancebeam/lib/jquery.js
*/
(function($){
//获取应用的上下文路径
$.getContextPath = function(){
/**
* ip address such as :
* http://192.168.1.1/framework/balancbeam/lib/jquery.js
* http://192.168.1.1/balancbeam/lib/jquery.js
* http://www.balancebeam.cc/framework/balancbeam/lib/jquery.js
* http://www.balancebeam.cc/balancbeam/lib/jquery.js
*/
var head = document.getElementsByTagName("head")[0],
scripts = head.getElementsByTagName("script");
for(var i=0,script,src,matcher;script=scripts[i];i++){
if((src = script.src) && (matcher = src.match(/\/jquery[^\/]*\.js$/i))){
var contextPath = "";
//采用的是相对路径
if(!/(^\/)|(^http)/.test(script.getAttribute("src"))){
contextPath = src.substring(0,src.lastIndexOf("/")+1) + "../";
}
else{
src = src.substring(0,src.indexOf("/lib"));
contextPath = src +"/";
}
return ($.getContextPath= function(){
return contextPath;
})();
}
}
throw new Error("miss jQuery lib,can not to analysis application context path.");
};
$.mask = new function(){
var pageMask = null;
//显示页面遮挡层
this.showPageMask = function(zIndex){
pageMask = pageMask || $("
").appendTo('body');
zIndex && pageMask.css("zIndex",zIndex);
pageMask.show();
};
//隐藏页面遮挡层
this.hidePageMask = function(){
pageMask && pageMask.hide();
}
var ajaxMask = null;
//显示ajax请求遮挡层
this.showAjaxMask = function(){
if(!ajaxMask){
var html = [""];
html.push("
");
html.push("
");
html.push("
");
html.push("");
html.push("
");
html.push("
");
html.push("
");
ajaxMask = $(html.join("")).appendTo('body');
}
ajaxMask.show();
}
//隐藏ajax请求遮挡层
this.hideAjaxMask = function(){
ajaxMask && ajaxMask.hide();
}
//显示容器的进度条
var boxLoading = null;
this.showBoxLoading = function(box){
if(!boxLoading){
boxLoading = $("");
}
boxLoading.appendTo(box);
boxLoading.show();
}
this.hideBoxLoading = function(){
if(boxLoading){
boxLoading.remove();
boxLoading.hide();
}
}
}();
//停止冒泡
$.stopEvent = function(e){
e.stopPropagation();
e.preventDefault();
};
//转换模板,返回transformer
$.transformTemplate = function(templateString){
/**
* var template = "#{content}
";
* var transform = $.transformTemplate(template);
* var html = transform({className : "welcome",content:"hello world."});
*
* or
* var template = "#{1}
";
* var transform = $.transformTemplate(template);
* var html = transform([ "welcome","hello world."]);
*/
var str = templateString,
expression = /#\{[<>a-zA-Z0-9]+\}/,
substitute = /(#\{)|(\})/g,
markup = [],
mapping =[]
r = null;
while(r = str.match(expression)){
var index = r.index;
if(index!=0){
markup.push(str.substring(0,index));
}
mapping.push([markup.length,r[0].replace(substitute,"")]);
markup.push(r[0]);
str = str.substring(index+r[0].length);
}
str!="" && markup.push(str);
return function(data){
var html = [];
for(var i=0,map;map =mapping[i];i++){
var index = map[0],
name = map[1],
value = data[name]!=null ? String(data[name]) : " ";
markup[index] = value;
}
html.push(markup.join(""));
return html.join("");
}
};
//获取幂级父节点
$.getPowerParent = function(node,power){
while(power--){
node = node.parentNode;
}
return node;
}
//获取最外层top帧
$.getTop = function(){
var win = window.opener || window;
try{
win.top.navigator;
return win.top;
}catch(e){
try{
do{
var pwin = win.parent;
pwin.navigator;
win = pwin;
}while(win);
}catch(e1){
return win;
}
}
}
//把数组转换成对象
$.array2obj = function(array){
var obj = {};
for(var i = array.length -1; i>=0;i--){
obj[array[i]] = 1;
}
return obj;
}
//获取参数,如果有回调方法则执行
$.getParameters = function(parameters){
var data = {};
for(var name in parameters){
data[name] = jQuery.isFunction(parameters[name]) ? parameters[name]() : parameters[name];
}
return data;
}
$.getBlankIcon = function(){
return "data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
}
/**
* jQuery patch
*/
var opt = Object.prototype.toString;
function isObject(o){
return "[object Object]"==opt.call(o);
}
function extendOptions(o1,o2){
for(var name in o2){
if(name in o1){
if(null!=o1[name]
&& isObject(o1[name])
&& isObject(o2[name])){
extendOptions(o1[name],o2[name]);
continue;
}
}
o1[name] = o2[name];
}
}
$.Widget.prototype._createWidget= function( options, element ) {
// $.widget.bridge stores the plugin instance, but we do it anyway
// so that it's stored even before the _create function runs
$.data( element, this.widgetName, this );
this.element = $( element );
this.options = $.extend( true, {},
this.options,
this._getCreateOptions());
extendOptions(this.options,options);
var self = this;
this.element.bind( "remove." + this.widgetName, function() {
self.destroy();
});
this._create();
this._trigger( "create" );
this._init();
}
})(jQuery);