/** * thinksns核心js对象 * @author jason * @version ts3.0 */ var _core = function() { // 核心通用的加载源函数 var obj = this; // 加载文件方法 this._coreloadfile = function() { var temp = new array(); var tempmethod = function(url, callback) { // 第二次调用的时候就不=0了 var flag = 0; for(i in temp) { if(temp[i] == url) { flag = 1; } } if(flag == 0) { // 未载入过 temp[temp.length] = url; // jquery的ajax载入文件方式,如果有样式文件,同理在此引入相关样式文件 $.getscript(url, function() { if("undefined" != typeof(callback)) { if("function" == typeof(callback)) { callback(); } else { eval(callback); } } }); } else { if("undefined" != typeof(callback)) { // 利用settimeout 避免未定义错误 settimeout(callback, 200); } } }; // 返回内部包函数,供外部调用并可以更改temp的值 return tempmethod; }; // 加载css文件 this._loadcss = function() { var temp = new array(); var tempmethod = function(url) { // 第二次调用的时候就不=0了 var flag = 0; for(i in temp) { if(temp[i] == url) { flag = 1; } } if(flag == 0) { // 未载入过 temp[temp.length] = url; var css = ''; $('head').append(css); } }; // 返回内部包函数,供外部调用并可以更改temp的值 return tempmethod; }; /** * 时间插件源函数 * 利用必包原理只载入一次js文件,其他类似功能都可以参照此方法 * 需要提前引入jquery.js文件 * @author yangjs */ this._rcalendar = function(text, mode, refunc) { // 标记值 var temp = 0; var tempmethod = function(t, m, r) { // 第二次调用的时候就不=0了 if(temp == 0) { // jquery的ajax载入文件方式,如果有样式文件,同理在此引入相关样式文件 $.getscript(theme_url+'/js/rcalendar.js', function() { rcalendar(t, m, r); }); } else { rcalendar(t, m, r); } temp++; }; // 返回内部包函数,供外部调用并可以更改temp的值 return tempmethod; }; /** * 生成img的html块 */ this._createimagehtml = function() { var _imghtml = ''; var _c = function() { if(_imghtml == '') { $.post(u('public/feed/getsmile'), {}, function(data) { for(var k in data) { _imghtml += ''; } _imghtml += '
'; $('#emot_content').html(_imghtml); }, 'json'); } else { $('#emot_content').html(_imghtml); } }; return _c; }; } // 核心对象 var core = new _core(); /** * 核心的插件列表 */ //微博加载文件,支持回调函数 调用方式core.loadfile(url,callback) core.loadfile = core._coreloadfile(); core.loadcss = core._loadcss(); /** * 核心插件自动生成的工厂函数 * 这里用到了js的工厂模式等设计模式 * * 使用方法:将js插件写到plugins/下的对应文件下,文件名必须与插件对象同名,如core.at.js * js 插件里面需要有一个_init 函数,根据传入参数真正调用 init函数 * * 如:core.pluginit('searchuser',$(this)); * 其中searchuser表示插件的名称是core.searchuser.js * $(this) 为 init的第一个参数 * * @author yangjs */ core.pluginit = function() { if(arguments.length > 0) { var arg = arguments; var back = function() { eval("var func = core." + arg[0] + ";"); if("undefined" != typeof(func)) { func._init(arg); } }; var file = theme_url + '/js/plugins/core.' + arguments[0] + '.js'; core.loadfile(file, back); } }; //与上面方法类似 只不过可以自己写回调函数(不主动执行init) core.plugfunc = function(plugname,callback){ var file = theme_url+'/js/plugins/core.'+plugname+'.js'; core.loadfile(file,callback); }; /** * 优化settimeout函数 * @param func * @param time */ core.settimeout = function(func,time){ // var type = typeof(func); // if("undefined" == type){ settimeout(func, time); // }else{ // if("string" == type){ // eval(func); // }else{ // func(); // } // } }; // 获取对象编辑框内的可输入数字 core.getleftnums = function(obj) { var str = obj.innerhtml; // 替换br标签 var imgnums = $(obj).find('img').size(); // 判断是否为空 var _str = str.replace(new regexp("
","gm"),""); _str = _str.replace(/[ ]|( )*/g, ""); // 判断字数是否超过,一个空格算一个字 _str = str.replace(/<[^>]*>/g, ""); // 去掉所有html标签 _str = trim(_str,' '); if(imgnums <1 ) { var emptystr = _str.replace(/ /g,"").replace(/\s+/g,""); if(emptystr.length == 0) { return initnums; } } _str = _str.replace(/ /g,"a"); // 由于可编辑div的空格都是nbsp 所以这么算 return initnums - getlength(_str) - imgnums; }; core.getlength = function(str, shorturl) { str = str + ''; if (true == shorturl) { // 一个url当作十个字长度计算 return math.ceil(str.replace(/((news|telnet|nttp|file|http|ftp|https):\/\/){1}(([-a-za-z0-9]+(\.[-a-za-z0-9]+)*(\.[-a-za-z]{2,5}))|([0-9]{1,3}(\.[0-9]{1,3}){3}))(:[0-9]*)?(\/[-a-za-z0-9_\$\.\+\!\*\(\),;:@&=\?\/~\#\%]*)*/ig, 'xxxxxxxxxxxxxxxxxxxx') .replace(/^\s+|\s+$/ig,'').replace(/[^\x00-\xff]/ig,'xx').length/2); } else { return math.ceil(str.replace(/^\s+|\s+$/ig,'').replace(/[^\x00-\xff]/ig,'xx').length/2); } }; // 一些自定义的方法 // 生成表情图片 core.createimagehtml = core._createimagehtml(); //日期控件,调用方式 core.rcalendar(this,'full') //this 也可以替换为具体id,full表示时间显示模式,也可以参考rcalendar.js内的其他模式 core.rcalendar = core._rcalendar(); //临时存储机制 适用于分割开存储的内容 core.stringdb = function(obj,inputname,tags){ this.inputname = inputname; this.obj = obj; if(tags != ''){ this.tags = tags.split(','); }else{ this.tags = new array(); } this.taglist = $(obj).find('ul'); this.inputhide = $(obj).find("input[name='"+inputname+"']"); }; core.stringdb.prototype = { init:function(){ if(this.tags.length > 0){ var html = ''; for(var i in this.tags){ if(this.tags[i] != ''){ html +='
  • x
  • '; } } this.taglist.html(html); this.bindul(); } }, bindul:function(){ var _this = this; this.taglist.find('li').click(function(){ _this.remove($(this).find('label').html()); }); }, add:function(tag){ var _tag = tag.split(','); var _this = this; var add = function(t){ for(var i in _this.tags){ if(_this.tags[i] == t){ return false; } } var html = '
  • x
  • '; _this.tags[_this.tags.length] = t; _this.taglist.append(html); }; for(var ii in _tag){ if(_tag[ii] != ''){ add(_tag[ii]); } } this.inputhide.val(this.tags.join(',')); this.bindul(); }, remove:function(tag){ var del = function(arr,n){ if(n<0){ return arr; }else{ return arr.slice(0,n).concat(arr.slice(n+1,arr.length)) } } for(var i in this.tags){ if(this.tags[i] == tag){ this.tags = del(this.tags,parseint(i)); this.taglist.find('li').eq(i).remove(); this.inputhide.val(this.tags.join(',')); } } } }; /*** 核心js函数库 ***/ /** * 模拟ts的u函数,需要预先定义js全局变量site_url和appname * @param string url 链接地址 * @param array params 链接参数 * @return string 组装后的链接地址 */ var u = function(url, params) { var website = site_url+'/index.php'; url = url.split('/'); if(url[0]=='' || url[0]=='@') url[0] = appname; if (!url[1]) url[1] = 'index'; if (!url[2]) url[2] = 'index'; website = website+'?app='+url[0]+'&mod='+url[1]+'&act='+url[2]; if(params) { params = params.join('&'); website = website + '&' + params; } return website; }; /** * 窗体对象,全站使用,统一窗体接口 */ var ui = { /** * 浮屏显示消息,提示信息框 * @param string message 信息内容 * @param integer error 是否是错误样式,1表示错误样式、0表示成功样式 * @param integer lazytime 提示时间 * @return void */ showmessage: function(message, error, lazytime) { var style = (error=="1") ? "html_clew_box clew_error" : "html_clew_box"; var ico = (error == "1") ? 'ico-error' : 'ico-ok'; var html = ''; var _u = function() { for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] != 'undefined') return false; } return true; }; // 显示提示弹窗 ui.showblackout(); // 将弹窗加载到body后 $(html).appendto(document.body); // 获取高宽 var _h = $('#ui_messagebox').height(); var _w = $('#ui_messagebox').width(); // 获取定位值 var left = ($('body').width() - _w)/2 ; var top = $(window).scrolltop() + ($(window).height()-_h)/2; // 添加弹窗样式与动画效果(出现) $('#ui_messagebox').css({ left:left + "px", top:top + "px" }).fadein("slow",function(){ $('#ui_messagebox').prepend(''); }); // 添加弹窗动画效果(消失) settimeout(function() { $('#ui_messagebox').find('iframe').remove(); $('#ui_messagebox').fadeout("fast", function() { ui.removeblackout(); $('#ui_messagebox').remove(); }); } , lazytime*1000); }, /** * 添加弹窗 * @return void */ showblackout: function() { if($('.boxy-modal-blackout').length > 0) { // todo:??? } else { var height = $('body').height() > $(window).height() ? $('body').height() : $(window).height(); $('
    ') .css({ height:height+'px',width:$('body').width()+'px',zindex: 999, opacity: 0.5 }).appendto(document.body); } }, /** * 移除弹窗 * @return void */ removeblackout: function() { if($('#tsbox').length > 0) { if(document.getelementbyid('tsbox').style.display == 'none'){ $('.boxy-modal-blackout').remove(); } } else { $('.boxy-modal-blackout').remove(); } }, /** * 操作成功显示api * @param string message 信息内容 * @param integer time 展示时间 * @return void */ success: function(message, time) { var t = "undefined" == typeof(time) ? 1 : time; ui.showmessage(message, 0, t); }, /** * 操作出错显示api * @param string message 信息内容 * @param integer time 展示时间 * @return void */ error: function(message, time) { var t = "undefined" == typeof(time) ? 2 : time; ui.showmessage(message, 1, t); }, /** * 确认弹框显示api - 浮窗型 * @example * 可以加入callback,回调函数 * @param object o 定位对象 * @param string text 提示语言 * @param string|function _callback 回调函数名称 * @return void */ confirm: function(o, text, _callback) { // 判断弹窗是否存在 document.getelementbyid('ts_ui_confirm') !== null && $('#ts_ui_confirm').remove(); var callback = "undefined" == typeof(_callback) ? $(o).attr('callback') : _callback; text = text || l('public_accont_tipes'); text = ""+text; this.html = '
    '; $('body').append(this.html); var position = $(o).offset(); $('#ts_ui_confirm').css({"top":position.top+"px","left":position.left+"px","display":"none"}); $("#ts_ui_confirm .txt").html(text); $('#ts_ui_confirm').fadein("fast"); $("#ts_ui_confirm .btn-cancel").one('click',function(){ $('#ts_ui_confirm').fadeout("fast"); // 修改原因: ts_ui_confirm .btn_b按钮会重复提交 $('#ts_ui_confirm').remove(); return false; }); $("#ts_ui_confirm .btn_ok").one('click',function(){ $('#ts_ui_confirm').fadeout("fast"); // 修改原因: ts_ui_confirm .btn_b按钮会重复提交 $('#ts_ui_confirm').remove(); if("undefined" == typeof(callback)){ return true; }else{ if("function"==typeof(callback)){ callback(); }else{ eval(callback); } } }); $('body').bind('keyup', function(event) { $("#ts_ui_confirm .btn_ok").click(); }); return false; }, /** * 确认框显示api - 弹窗型 * @param string title 弹窗标题 * @param string text 提示语言 * @param string|function _callback 回调函数名称 * @return void */ confirmbox: function(title, text, _callback) { this.box.init(title); text = text || l('public_accont_tipes'); text = ""+text; var content = '
    '+ text + '
    '+l('public_queding')+''+l('public_quxiao')+'
    '; this.box.setcontent(content); this.box.center(); var callback = "undefined" == typeof(_callback) ? $(o).attr('callback') : _callback; var _this = this; $("#tsbox .btn-cancel").one('click',function(){ $('#tsbox').fadeout("fast",function(){ $('#tsbox').remove(); }); _this.box.close(); return false; }); $("#tsbox .btn-green-small").one('click',function(){ $('#tsbox').fadeout("fast",function(){ $('#tsbox').remove(); }); _this.box.close(); if("undefined" == typeof(callback)){ return true; }else{ if("function"==typeof(callback)){ callback(); }else{ eval(callback); } } }); return false; }, /** * 私信弹窗api * @param string touid 收件人id * @return void */ sendmessage: function(touid, editable) { if(typeof(editable) == "undefined" ) { editable = 1; } touid = touid || ''; this.box.load(u('public/message/post')+'&touid='+touid+'&editable='+editable, l('public_setprivate_mail')); }, /** * @me弹窗api * @param string touid @人id * @return void */ sendat: function(touid) { touid = touid || ''; this.box.load(u('public/mention/at')+'&touid='+touid, '@ta'); }, /** * 弹窗发布微博 * @param string title 弹窗标题 * @param string inithtml 插入内容 * @return void */ sendbox: function(title, inithtml,channelid) { if($.browser.msie) { inithtml = encodeuri(inithtml); } inithtml = inithtml.replace(/\#/g, "%23"); this.box.load(u('public/index/sendfeedbox')+'&inithtml='+inithtml+'&channelid='+channelid, title,function(){ $('#at-view').hide(); }); }, /** * 回复弹窗api * @param integer comment_id 评论id * @return void */ reply: function(comment_id) { this.box.load(u('public/comment/reply')+'&comment_id='+comment_id,l('public_resave'),function (){$('#at-view').hide();}); }, groupreply: function(comment_id,gid) { this.box.load(u('group/group/reply')+'&gid='+gid+'&comment_id='+comment_id,l('public_resave'),function (){$('#at-view').hide();}); }, /** * 选择部门弹窗api - 暂不使用 */ changedepartment: function(hid,showname,sid,nosid,notop) { this.box.load(u('widget/department/change')+'&hid='+hid+'&showname='+showname+'&sid='+sid+'&nosid='+nosid+'¬op='+notop,l('public_depatement_select')); }, /** * 自定弹窗api接口 */ box: { wrapper: '', inited: false, ie6: (jquery.browser.msie && jquery.browser.version < 7), init: function(title, callback) { this.callback = callback; // 弹窗中隐藏小名片 if("undefined" != typeof(core.facecard)){ core.facecard.dohide(); } if($('#tsbox').length >0) { return false; } else { $('body').prepend(this.wrapper); } var url = theme_url+'/js/tbox/box.css'; core.loadcss(url); // 添加头部 if("undefined" != typeof(title)) { $("
    "+title+"
    ").insertbefore($('#tsbox .layer-content')); } ui.showblackout(); $('#tsbox').stop().css({width: '', height: ''}); // 添加键盘事件 jquery(document.body).bind('keypress.tsbox', function(event) { var key = event.keycode ? event.keycode : event.which ? event.which : event.charcode; if (key == 27) { jquery(document.body).unbind('keypress.tsbox'); ui.box.close(callback); return false; } }); // 关闭弹窗,回调函数 $('#tsbox').find('.ico-close').click(function() { ui.box.close(callback); return false; }); this.center(); var show = function(){ $('#tsbox').show(); } settimeout(show, 200); $('#tsbox').draggable({ handle: '.hd' }); $('.hd').mousedown(function(){ $('.mod-at-wrap').remove(); }); }, /** * 设置弹窗中的内容 * @param string content 内容信息 * @return void */ setcontent: function(content) { $('#layer-content').html(content); }, /** * 关闭窗口 * @param function fn 回调函数名称 * @return void */ close: function(fn) { $('#ui-fs .ui-fs-all .ui-fs-allinner div.list').find("a").die("click"); $('.talkpop').remove(); $('#tsbox').remove(); $('.mod-at-wrap').remove(); jquery('.boxy-modal-blackout').remove(); var back =''; if("undefined" != typeof(fn)){ back = fn; }else if("undefined" != typeof(this.callback)){ back = this.callback; } if("function" == typeof(back)){ back(); }else{ eval(back); } }, /** * 提示弹窗 * @param string data 信息数据 * @param string title 标题信息 * @param function callback 回调函数 * @return void */ alert:function(data,title,callback){ this.init(title,callback); this.setcontent('
    '+data+'
    '); this.center(); }, /** * 显示弹窗 * @param string content 信息数据 * @param string title 标题信息 * @param function callback 回调函数 * @return void */ show:function(content,title,callback){ this.init(title,callback); this.setcontent(content); this.center(); }, /** * 载入弹窗api * @param string requesturl 请求地址 * @param string title 弹窗标题 * @param string callback 窗口关闭后的回调事件 * @param object requestdata requestdata * @param string type ajax请求协议,默认为get * @return void */ load:function(requesturl,title,callback,requestdata,type) { this.init(title,callback); if("undefined" != typeof(type)) { var ajaxtype = type; }else{ var ajaxtype = "get"; } this.setcontent('
     
    '); var obj = this; if("undefined" == requestdata) { var requestdata = {}; } jquery.ajax({url:requesturl, type:ajaxtype, data:requestdata, cache:false, datatype:'html', success:function(html){ obj.setcontent(html); obj.center(); } }); }, /** * 弹窗定位 * @return void */ _viewport: function() { var d = document.documentelement, b = document.body, w = window; return jquery.extend( jquery.browser.msie ? { left: b.scrollleft || d.scrollleft, top: b.scrolltop || d.scrolltop } : { left: w.pagexoffset, top: w.pageyoffset }, !ui.box._u(w.innerwidth) ? { width: w.innerwidth, height: w.innerheight } : (!ui.box._u(d) && !ui.box._u(d.clientwidth) && d.clientwidth != 0 ? { width: d.clientwidth, height: d.clientheight } : { width: b.clientwidth, height: b.clientheight }) ); }, /** * 验证参数 * @return void */ _u: function() { for(var i = 0; i < arguments.length; i++) { if(typeof arguments[i] != 'undefined') return false; } return true; }, /** * 样式覆盖 * @return void */ _cssforoverlay: function() { if (ui.box.ie6) { return ui.box._viewport(); } else { return {width: '100%', height: jquery(document).height()}; } }, /** * 中间定位 * @param string axis 横向,纵向 * @return void */ center: function(axis) { var v = ui.box._viewport(); var o = [v.left, v.top]; if (!axis || axis == 'x') this.centerat(o[0] + v.width / 2 , null); if (!axis || axis == 'y') this.centerat(null, o[1] + v.height / 2); return this; }, /** * 横向移动 * @param integer x 数值 * @return void */ movetox: function(x) { if (typeof x == 'number') $('#tsbox').css({left: x}); else this.centerx(); return this; }, /** * 纵向移动 * @param integer y 数值 * @return void */ movetoy: function(y) { if (typeof y == 'number') $('#tsbox').css({top: y}); else this.centery(); return this; }, centerat: function(x, y) { var s = this.getsize(); if (typeof x == 'number') this.movetox(x - s[0]/2 ); if (typeof y == 'number') this.movetoy(y - s[1]/2 ); return this; }, centeratx: function(x) { return this.centerat(x, null); }, centeraty: function(y) { return this.centerat(null, y); }, getsize: function() { return [$('#tsbox').width(), $('#tsbox').height()]; }, getcontent: function() { return $('#tsbox').find('.boxy-content'); }, getposition: function() { var b = $('#tsbox'); return [b.offsetleft, b.offsettop]; }, getcontentsize: function() { var c = this.getcontent(); return [c.width(), c.height()]; }, _getboundsforresize: function(width, height) { var csize = this.getcontentsize(); var delta = [width - csize[0], height - csize[1]]; var p = this.getposition(); return [math.max(p[0] - delta[0] / 2, 0), math.max(p[1] - delta[1] / 2, 0), width, height]; } } };