PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/static/js/lib/util.js

https://gitlab.com/imxieke/XCloud
JavaScript | 929 lines | 834 code | 53 blank | 42 comment | 173 complexity | 556a2b22639ea805face9b4bde873668 MD5 | raw file
  1. /*changed by warlee
  2. * @link http://www.kalcaddle.com/
  3. * @author warlee | e-mail:kalcaddle@qq.com
  4. * @copyright warlee 2014.(Shanghai)Co.,Ltd
  5. * @license http://kalcaddle.com/tools/licenses/license.txt
  6. */
  7. /*
  8. * iframe之间函数调用
  9. *
  10. * main frame中每个frame需要name和id,以便兼容多浏览器
  11. * 如果需要提供给其他frame调用,则需要在body中加入
  12. * <input id="FrameCall" type='hidden' action='' value='' onclick='FrameCall.api()'/>
  13. * 调用例子:Frame.doFunction('main','goUrl','"'+url+'"');该frame调用id为main的兄弟frame的goUrl方法,参数为后面的
  14. * 参数为字符串时需要加引号,否则传过去会被理解成一个未定义变量
  15. */
  16. var FrameCall = (function(){
  17. var idName = "FrameCall";
  18. var idNameAll = "#"+idName;
  19. var ie = !-[1,];//是否ie
  20. return{
  21. apiOpen:function(){
  22. var html = '<input id="FrameCall" type="hidden" action="1" value="1" onclick="FrameCall.api()" />';
  23. $(html).prependTo('body');
  24. },
  25. //其他窗口调用该窗口函数,调用另一个frame的方法
  26. api:function(){
  27. var action = $(idNameAll).attr('action');
  28. var value=$(idNameAll).attr('value');
  29. if (action == 'get') {//获取变量
  30. share.data('create_app_path',eval(value));
  31. return;
  32. }
  33. var fun=action+'('+value+');';//拼装执行语句,字符串转换到代码
  34. try{
  35. eval(fun);
  36. } catch(e) {};
  37. },
  38. //该窗口调用顶层窗口的子窗口api,调用iframe框架的js函数.封装控制器。
  39. top:function(iframe,action,value){
  40. if (!window.parent.frames[iframe]) return;
  41. //var obj = window.top.frames[iframe].document;
  42. var obj = window.parent.frames[iframe].document;
  43. if(!obj) return;
  44. obj=obj.getElementById(idName);
  45. $(obj).attr("action",action);
  46. $(obj).attr("value",value);
  47. obj.click();
  48. },
  49. //该窗口调用父窗口的api
  50. child:function(iframe,action,value){
  51. if (!window.frames[iframe]) return;
  52. var obj = window.frames[iframe].document;
  53. if(!obj) return;
  54. obj=obj.getElementById(idName);
  55. $(obj).attr("action",action);
  56. $(obj).attr("value",value);
  57. obj.click();
  58. },
  59. //该窗口调用父窗口的api
  60. father:function(action,value){
  61. var obj=window.parent.document;
  62. obj=obj.getElementById(idName);
  63. $(obj).attr("action",action);
  64. $(obj).attr("value",value);
  65. obj.click();
  66. },
  67. //___自定义通用方法,可在页面定义更多提供给接口使用的api。
  68. goUrl:function(url){
  69. window.location.href=url;
  70. },
  71. goRefresh:function(){
  72. window.location.reload();
  73. }
  74. }
  75. })();
  76. $(document).ready(function() {
  77. FrameCall.apiOpen();
  78. });
  79. var time = function(){
  80. var time = (new Date()).valueOf();
  81. return time;
  82. }
  83. var urlEncode = encodeURIComponent;
  84. var urlDecode = decodeURIComponent;
  85. var urlEncode2 = function (str){
  86. return urlEncode(urlEncode(str));
  87. };
  88. var UUID = function(){
  89. return 'uuid_'+time()+'_'+Math.ceil(Math.random()*10000)
  90. }
  91. var round = function(val,point){
  92. if (!point) point = 2;
  93. point = Math.pow(10,parseInt(point));
  94. return Math.round(parseFloat(val)*point)/point;
  95. }
  96. //跨框架数据共享
  97. var share = {
  98. data: function (name, value) {
  99. var top = window.top,
  100. cache = top['_CACHE'] || {};
  101. top['_CACHE'] = cache;
  102. return value !== undefined ? cache[name] = value : cache[name];
  103. },
  104. removeData: function (name) {
  105. var cache = window.top['_CACHE'];
  106. if (cache && cache[name]) delete cache[name];
  107. }
  108. };
  109. jQuery.easing.def="easeInOutCubic";//easeOutExpo,easeInOutExpo,easeInOutSine
  110. //cookie操作
  111. var Cookie = (function(){
  112. var cookie = {};
  113. var _init = function(){
  114. cookie = {};//初始化cookie
  115. var cookieArray=document.cookie.split("; ");
  116. for (var i=0;i<cookieArray.length;i++){
  117. var arr=cookieArray[i].split("=");
  118. cookie[arr[0]] = unescape(arr[1]);
  119. }
  120. return cookie;
  121. }
  122. var get = function(key){//没有key代表获取所有
  123. _init();
  124. if (key == undefined) return cookie;
  125. return cookie[key];
  126. };
  127. var set = function(key,value,timeout){
  128. var str = escape(key)+"="+escape(value);//不设置时间代表跟随页面生命周期
  129. if (timeout == undefined){//时间以小时计
  130. timeout = 365;
  131. }
  132. var expDate=new Date();
  133. expDate.setTime(expDate.getTime() + timeout*3600*24*1000);
  134. str += "; expires="+expDate.toGMTString();
  135. document.cookie = str;
  136. };
  137. var del = function(key){
  138. document.cookie = key+"=;expires="+(new Date(0)).toGMTString();
  139. };
  140. var clear = function(){
  141. _init();
  142. for(var key in cookie){
  143. del(key);
  144. }
  145. }
  146. return {
  147. get:get,
  148. set:set,
  149. del:del,
  150. clear:clear
  151. }
  152. })();
  153. //是否在数组中。
  154. var inArray = function(arr,value) {
  155. for (var i=0,l = arr.length ; i <l ; i++) {
  156. if (arr[i] === value) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. var stopPP = function(e){//防止事件冒泡
  163. e = e || window.event;
  164. if(!e) return;
  165. if (e.stopPropagation) {
  166. e.stopPropagation();
  167. }
  168. if (e.preventDefault) {
  169. e.preventDefault();
  170. }
  171. e.cancelBubble = true;
  172. e.keyCode = 0;
  173. e.returnValue = false;
  174. }
  175. //通用提示信息框
  176. var tips = function(msg,code){
  177. Tips.tips(msg,code);
  178. }
  179. var Tips = (function(){
  180. var in_time = 600;
  181. var delay = 800;
  182. var opacity = 0.7;
  183. var _init = function(msg,code){
  184. var tipsIDname = "messageTips";
  185. var tipsID = "#"+tipsIDname;
  186. if ($(tipsID).length ==0) {
  187. var html='<div id="'+tipsIDname+'" class="tips_box"><i></i><span></span>'+
  188. '<a class="tips_close">×</a></div>'
  189. $('body').append(html);
  190. $(tipsID).show().css({'left':($(window).width() - $(tipsID).innerWidth())/2});
  191. $(window).bind('resize',function(){
  192. if ($(tipsID).css('display') =="none") return;
  193. self.stop(true,true)
  194. $(tipsID).css({'left':($(window).width() - $(tipsID).width()) / 2});
  195. });
  196. $(tipsID).find('.tips_close').click(function(){
  197. $(tipsID).animate({opacity:0},
  198. in_time,0,function(){
  199. $(this).hide();
  200. });
  201. });
  202. }
  203. var self = $(tipsID),icon,color;
  204. switch(code){
  205. case 100:delay = 3000;//加长时间 5s
  206. case true:
  207. case undefined:
  208. case 'succcess':color = '#5cb85c';icon = 'icon-ok';break;
  209. case 'info':color = '#519AF6';icon = 'icon-info';break;
  210. case 'warning':color = '#ed9c28';icon = 'icon-exclamation';break;
  211. case false:
  212. case 'error':delay = 1000;color = '#d9534f';icon = 'icon-remove';break;
  213. default:color = '';icon = '';break;
  214. }
  215. if (color != '') {
  216. self.css({'background':color,'color':'#fff'});
  217. self.find('i').removeClass().addClass(icon);
  218. }
  219. if (msg != undefined) self.find('span').html(msg);
  220. $(tipsID).show().css({'left':($(window).width() - $(tipsID).innerWidth())/2});
  221. return self;
  222. };
  223. var tips = function(msg,code,offset_top){
  224. if (typeof(msg) == 'object'){
  225. code=msg.code;msg = msg.data;
  226. }
  227. if (offset_top == undefined) offset_top = 0;
  228. var self = _init(msg,code);
  229. self.stop(true,true)
  230. .css({'opacity':'0','top':offset_top-self.height()})
  231. .show()
  232. .animate({opacity:opacity,top:offset_top},in_time,0)
  233. .delay(delay)
  234. .animate({opacity:0,top:'-='+(offset_top+self.height())},in_time,0,function(){
  235. $(this).hide();
  236. });
  237. };
  238. var loading = function(msg,code,offset_top){
  239. if (typeof(msg) == 'object'){
  240. code=msg.code;msg = msg.data;
  241. }
  242. if (offset_top == undefined) offset_top = 0;
  243. if (msg == undefined) msg = 'loading...'
  244. msg+= "&nbsp;&nbsp; <img src='./static/images/loading.gif'/>";
  245. var self = _init(msg,code);
  246. self.stop(true,true)
  247. .css({'opacity':'0','top':offset_top-self.height()})
  248. .animate({opacity:opacity,top:offset_top},in_time,0);
  249. };
  250. var close = function(msg,code,offset_top){
  251. if (typeof(msg) == 'object'){
  252. try{
  253. code=msg.code;msg = msg.data;
  254. }catch(e){
  255. code=0;msg ='';
  256. };
  257. }
  258. if (offset_top == undefined) offset_top = 0;
  259. var self = _init(msg,code);
  260. self.delay(delay)
  261. .show()
  262. .animate({
  263. opacity:0,
  264. top:'-='+(offset_top+self.height())},
  265. in_time,0,function(){
  266. $(this).hide();
  267. });
  268. };
  269. return{
  270. tips:tips,
  271. loading:loading,
  272. close:close
  273. }
  274. })();
  275. //获取keys
  276. var objectKeys = function(obj){
  277. var keys = [];
  278. for(var p in obj){
  279. if(obj.hasOwnProperty(p)){
  280. keys.push(p);
  281. }
  282. }
  283. return keys;
  284. }
  285. //获取values
  286. var objectValues = function(obj){
  287. var values = [];
  288. for(var p in obj){
  289. keys.push(obj[p]);
  290. }
  291. return values;
  292. }
  293. var $sizeInt = function($obj){
  294. var str = $obj+'';
  295. var theSize = parseInt(str.replace('px',''));
  296. if (isNaN(theSize)) {
  297. return 0;
  298. }else{
  299. return theSize;
  300. }
  301. }
  302. //通用遮罩层
  303. var MaskView = (function(){
  304. var opacity = 0.6;
  305. var animatetime = 250;
  306. var color ='#000';
  307. var maskId = "#windowMaskView";
  308. var maskContent = '#maskViewContent';
  309. var add = function(content,t_opacity,t_color,time){
  310. if (t_opacity != undefined) opacity == t_opacity;
  311. if (t_color != undefined) color == t_color;
  312. if (time != undefined) animatetime == time;
  313. if ($(maskId).length == 0) {
  314. var html ='<div id="windowMaskView" style="position:fixed;top:0;left:0;right:0;bottom:0;background:'+
  315. color+';opacity:'+opacity+';z-index:9998;"></div><div id="maskViewContent" style="position:absolute;z-index:9999"></div>';
  316. $('body').append(html);
  317. $(maskId).bind('click',close);
  318. $(maskContent).bind('click',function(e){
  319. e.stopPropagation();
  320. });
  321. $(window).unbind('resize').bind('resize',_resize);
  322. }
  323. $(maskContent).html(content).fadeIn(animatetime);_resize();
  324. $(maskId).hide().fadeIn(animatetime);
  325. };
  326. var _resize = function(){
  327. var $content = $(maskContent);
  328. $content.css({'width':'auto','height':'auto'}).css({
  329. top:($(window).height()-$content.height())/2,
  330. left:($(window).width()-$content.width())/2});
  331. imageSize();
  332. }
  333. var tips = function(msg){
  334. add("<div style='font-size:50px;color:#fff;opacity:0.6;'>"+msg+"</div>");
  335. }
  336. var image = function(url){
  337. add("<img src='"+url+"' class='image' onload='MaskView.resize();' style='-webkit-box-reflect: below 1px -webkit-gradient(linear,left top,left bottom,from(transparent),color-stop(80%,transparent),color-stop(70%,rgba(255,255,255,0)),to(rgba(255,255,255,0.3)));'/>");
  338. var $content = $(maskContent)
  339. var $dom = $content.find('.image');
  340. var dragFlag = false,E;
  341. var old_left,old_top;
  342. $(document).bind({
  343. mousedown:function(e){
  344. if (!$(e.target).hasClass('image')) return;
  345. dragFlag = true;
  346. $dom.css('cursor','move');
  347. stopPP(e);E = e;
  348. old_top = parseInt($content.css('top').replace('px',''));
  349. old_left = parseInt($content.css('left').replace('px',''));
  350. },
  351. mousemove:function(e){
  352. if (!dragFlag) return;
  353. $content.css({
  354. 'left':old_left+(e.clientX-E.clientX),
  355. 'top':old_top+(e.clientY-E.clientY)
  356. });
  357. },
  358. mouseup:function(){
  359. dragFlag = false;
  360. $dom.css('cursor','default');
  361. },
  362. keydown:function(e){
  363. if ($(maskId).length > 0 && e.keyCode == 27){
  364. MaskView.close();
  365. stopPP(e);
  366. }
  367. }
  368. });
  369. $('#windowMaskView,#maskViewContent img').mousewheel(function(event, delta, deltaX, deltaY){
  370. var offset = delta>0?1:-1;
  371. offset = offset * Math.abs(delta/10);
  372. var o_w = parseInt($dom.width()),
  373. o_h=parseInt($dom.height()),
  374. w = o_w * (1+offset/6),
  375. h = o_h * (1+offset/6);
  376. if(w<=5 || h<=5) return;
  377. if(w>=10000 || h>=10000) return;
  378. // var top = ($(window).height() - h)/2;
  379. // var left = ($(window).width() - w)/2;
  380. var top = parseInt($content.css("top"))-(h-o_h)/2;
  381. var left = parseInt($content.css("left"))-(w-o_w)/2;
  382. $(maskContent+','+maskContent+' .image').stop(true)
  383. .animate({'width':w,'height':h,'top':top,'left':left},400);
  384. });
  385. }
  386. var imageSize = function(){
  387. var $dom = $(maskContent).find('.image');
  388. if ($dom.length == 0) return;
  389. var image=new Image();
  390. image.src = $dom.attr('src');
  391. var percent = 0.7,
  392. w_width = $(window).width(),
  393. w_height= $(window).height(),
  394. m_width = image.width,
  395. m_height= image.height,
  396. width,height;
  397. if (m_width >= w_width*percent){
  398. width = w_width*percent;
  399. height= m_height/m_width * width;
  400. }else{
  401. width = m_width;
  402. height= m_height;
  403. }
  404. $dom.css({'width':width,'height':height});
  405. var $content = $(maskContent);
  406. $content.css({'width':'auto','height':'auto'}).css({
  407. top:($(window).height()-$content.height())/2,
  408. left:($(window).width()-$content.width())/2});
  409. }
  410. var close = function(){
  411. $(maskId).fadeOut(animatetime);
  412. if ($(maskContent).find('.image').length!=0) {
  413. $(maskContent+','+maskContent+' .image').animate({'width':0,'height':0,
  414. 'top':$(window).height()/2,
  415. 'left':$(window).width()/2
  416. },animatetime*1.3,0,function(){
  417. $(maskContent).hide();
  418. _resize();
  419. });
  420. }else{
  421. $(maskContent).fadeOut(animatetime);
  422. }
  423. };
  424. return{
  425. image:image,
  426. resize:_resize,
  427. tips:tips,
  428. add:add,
  429. close:close
  430. }
  431. })();
  432. //textarea自适应高度
  433. (function($){
  434. $.fn.autoTextarea = function(options) {
  435. var defaults={
  436. minHeight:20,
  437. padding:0
  438. };
  439. var opts = $.extend({},defaults,options);
  440. var ie = !!window.attachEvent && !window.opera;
  441. $(this)
  442. .die("paste cut keydown keyup focus blur")
  443. .live("paste cut keydown keyup focus blur",function(){
  444. if(!ie) this.style.height = options.minHeight+"px";
  445. var height = this.scrollHeight-options.padding;
  446. if(height<=options.minHeight){
  447. this.style.height = options.minHeight+"px";
  448. }else{
  449. this.style.height = height+"px";
  450. }
  451. });
  452. };
  453. })(jQuery);
  454. (function($){
  455. $.fn.extend({
  456. //dom绑定enter事件 用于input
  457. keyEnter:function(callback){
  458. $(this).die('keydown').live('keydown',function(e){
  459. if (e.keyCode == 13 && callback){
  460. callback();
  461. }
  462. });
  463. },
  464. //dom绑定鼠标滚轮事件
  465. mousewheel: function(fn){
  466. var mousewheel = jQuery.browser.mozilla ? "DOMMouseScroll" : "mousewheel";
  467. $(this).bind(mousewheel ,function(e){
  468. e= window.event || e;
  469. var delta = e.wheelDelta ? (e.wheelDelta / 120) : (- e.detail / 3);
  470. fn.call(this,delta);
  471. return false;
  472. });
  473. },
  474. //晃动 $('.wrap').shake(4,4,100);
  475. shake: function(times,offset,delay){
  476. this.stop().each(function(){
  477. var Obj = $(this);
  478. var marginLeft = parseInt(Obj.css('margin-left'));
  479. var delay = delay > 50 ? delay : 50;
  480. Obj.animate({'margin-left':marginLeft+offset},delay,function(){
  481. Obj.animate({'margin-left':marginLeft},delay,function(){
  482. times = times - 1;
  483. if(times > 0)
  484. Obj.shake(times,offset,delay);
  485. })
  486. });
  487. });
  488. return this;
  489. }
  490. });
  491. })(jQuery);
  492. (function($){
  493. $.tooltipsy = function (el, options) {
  494. this.options = options;
  495. this.$el = $(el);
  496. this.title = this.$el.attr('title') || '';
  497. this.$el.attr('title', '');
  498. this.random = parseInt(Math.random()*10000);
  499. this.ready = false;
  500. this.shown = false;
  501. this.width = 0;
  502. this.height = 0;
  503. this.delaytimer = null;
  504. this.$el.data("tooltipsy", this);
  505. this.init();
  506. };
  507. $.tooltipsy.prototype = {
  508. init: function () {
  509. var base = this,
  510. settings,
  511. $el = base.$el,
  512. el = $el[0];
  513. base.settings = settings = $.extend({}, base.defaults, base.options);
  514. settings.delay = +settings.delay;
  515. if (typeof settings.content === 'function') {
  516. base.readify();
  517. }
  518. if (settings.showEvent === settings.hideEvent && settings.showEvent === 'click') {
  519. $el.toggle(function (e) {
  520. if (settings.showEvent === 'click' && el.tagName == 'A') {
  521. e.preventDefault();
  522. }
  523. if (settings.delay > 0) {
  524. base.delaytimer = window.setTimeout(function () {
  525. base.show(e);
  526. }, settings.delay);
  527. }
  528. else {
  529. base.show(e);
  530. }
  531. }, function (e) {
  532. if (settings.showEvent === 'click' && el.tagName == 'A') {
  533. e.preventDefault();
  534. }
  535. window.clearTimeout(base.delaytimer);
  536. base.delaytimer = null;
  537. base.hide(e);
  538. });
  539. }
  540. else {
  541. $el.bind(settings.showEvent, function (e) {
  542. if (settings.showEvent === 'click' && el.tagName == 'A') {
  543. e.preventDefault();
  544. }
  545. base.delaytimer = window.setTimeout(function () {
  546. base.show(e);
  547. }, settings.delay || 0);
  548. }).bind(settings.hideEvent, function (e) {
  549. if (settings.showEvent === 'click' && el.tagName == 'A') {
  550. e.preventDefault();
  551. }
  552. window.clearTimeout(base.delaytimer);
  553. base.delaytimer = null;
  554. base.hide(e);
  555. });
  556. }
  557. },
  558. show: function (e) {
  559. if (this.ready === false) {
  560. this.readify();
  561. }
  562. var base = this,
  563. settings = base.settings,
  564. $tipsy = base.$tipsy,
  565. $el = base.$el,
  566. el = $el[0],
  567. offset = base.offset(el);
  568. if (base.shown === false) {
  569. if ((function (o) {
  570. var s = 0, k;
  571. for (k in o) {
  572. if (o.hasOwnProperty(k)) {
  573. s++;
  574. }
  575. }
  576. return s;
  577. })(settings.css) > 0) {
  578. base.$tip.css(settings.css);
  579. }
  580. base.width = $tipsy.outerWidth();
  581. base.height = $tipsy.outerHeight();
  582. }
  583. if (settings.alignTo === 'cursor' && e) {
  584. var tip_position = [e.clientX + settings.offset[0], e.clientY + settings.offset[1]];
  585. if (tip_position[0] + base.width > $(window).width()) {
  586. var tip_css = {top: tip_position[1] + 'px', right: tip_position[0] + 'px', left: 'auto'};
  587. }
  588. else {
  589. var tip_css = {top: tip_position[1] + 'px', left: tip_position[0] + 'px', right: 'auto'};
  590. }
  591. }
  592. else {
  593. var tip_position = [
  594. (function () {
  595. if (settings.offset[0] < 0) {
  596. return offset.left - Math.abs(settings.offset[0]) - base.width;
  597. }
  598. else if (settings.offset[0] === 0) {
  599. return offset.left - ((base.width - $el.outerWidth()) / 2);
  600. }
  601. else {
  602. return offset.left + $el.outerWidth() + settings.offset[0];
  603. }
  604. })(),
  605. (function () {
  606. if (settings.offset[1] < 0) {
  607. return offset.top - Math.abs(settings.offset[1]) - base.height;
  608. }
  609. else if (settings.offset[1] === 0) {
  610. return offset.top - ((base.height - base.$el.outerHeight()) / 2);
  611. }
  612. else {
  613. return offset.top + base.$el.outerHeight() + settings.offset[1];
  614. }
  615. })()
  616. ];
  617. }
  618. $tipsy.css({top: tip_position[1] + 'px', left: tip_position[0] + 'px'});
  619. base.settings.show(e, $tipsy.stop(true, true));
  620. },
  621. hide: function (e) {
  622. var base = this;
  623. if (base.ready === false) {
  624. return;
  625. }
  626. if (e && e.relatedTarget === base.$tip[0]) {
  627. base.$tip.bind('mouseleave', function (e) {
  628. if (e.relatedTarget === base.$el[0]) {
  629. return;
  630. }
  631. base.settings.hide(e, base.$tipsy.stop(true, true));
  632. });
  633. return;
  634. }
  635. base.settings.hide(e, base.$tipsy.stop(true, true));
  636. },
  637. readify: function () {
  638. this.ready = true;
  639. this.$tipsy = $('<div id="tooltipsy' + this.random + '" style="position:fixed;z-index:2147483647;display:none">').appendTo('body');
  640. this.$tip = $('<div class="' + this.settings.className + '">').appendTo(this.$tipsy);
  641. this.$tip.data('rootel', this.$el);
  642. var e = this.$el;
  643. var t = this.$tip;
  644. this.$tip.html(this.settings.content != '' ? (typeof this.settings.content == 'string' ? this.settings.content : this.settings.content(e, t)) : this.title);
  645. },
  646. offset: function (el) {
  647. return this.$el[0].getBoundingClientRect();
  648. },
  649. destroy: function () {
  650. if (this.$tipsy) {
  651. this.$tipsy.remove();
  652. $.removeData(this.$el, 'tooltipsy');
  653. }
  654. },
  655. defaults: {
  656. alignTo: 'element',
  657. offset: [0, -1],
  658. content: '',
  659. show: function (e, $el) {
  660. $el.fadeIn(100);
  661. },
  662. hide: function (e, $el) {
  663. $el.fadeOut(100);
  664. },
  665. css: {},
  666. className: 'tooltipsy',
  667. delay: 200,
  668. showEvent: 'mouseenter',
  669. hideEvent: 'mouseleave'
  670. }
  671. };
  672. $.fn.tooltipsy = function(options) {
  673. return this.each(function() {
  674. new $.tooltipsy(this, options);
  675. });
  676. };
  677. })(jQuery);
  678. var date = function(format, timestamp){
  679. timestamp = parseInt(timestamp);
  680. var a, jsdate=((timestamp) ? new Date(timestamp*1000) : new Date());
  681. var pad = function(n, c){
  682. if((n = n + "").length < c){
  683. return new Array(++c - n.length).join("0") + n;
  684. } else {
  685. return n;
  686. }
  687. };
  688. var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  689. var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"};
  690. var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  691. var f = {
  692. // Day
  693. d: function(){return pad(f.j(), 2)},
  694. D: function(){return f.l().substr(0,3)},
  695. j: function(){return jsdate.getDate()},
  696. l: function(){return txt_weekdays[f.w()]},
  697. N: function(){return f.w() + 1},
  698. S: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th'},
  699. w: function(){return jsdate.getDay()},
  700. z: function(){return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0},
  701. // Week
  702. W: function(){
  703. var a = f.z(), b = 364 + f.L() - a;
  704. var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;
  705. if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){
  706. return 1;
  707. } else{
  708. if(a <= 2 && nd >= 4 && a >= (6 - nd)){
  709. nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");
  710. return date("W", Math.round(nd2.getTime()/1000));
  711. } else{
  712. return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
  713. }
  714. }
  715. },
  716. // Month
  717. F: function(){return txt_months[f.n()]},
  718. m: function(){return pad(f.n(), 2)},
  719. M: function(){return f.F().substr(0,3)},
  720. n: function(){return jsdate.getMonth() + 1},
  721. t: function(){
  722. var n;
  723. if( (n = jsdate.getMonth() + 1) == 2 ){
  724. return 28 + f.L();
  725. } else{
  726. if( n & 1 && n < 8 || !(n & 1) && n > 7 ){
  727. return 31;
  728. } else{
  729. return 30;
  730. }
  731. }
  732. },
  733. // Year
  734. L: function(){var y = f.Y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},
  735. Y: function(){return jsdate.getFullYear()},
  736. y: function(){return (jsdate.getFullYear() + "").slice(2)},
  737. // Time
  738. a: function(){return jsdate.getHours() > 11 ? "pm" : "am"},
  739. A: function(){return f.a().toUpperCase()},
  740. B: function(){
  741. var off = (jsdate.getTimezoneOffset() + 60)*60;
  742. var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off;
  743. var beat = Math.floor(theSeconds/86.4);
  744. if (beat > 1000) beat -= 1000;
  745. if (beat < 0) beat += 1000;
  746. if ((String(beat)).length == 1) beat = "00"+beat;
  747. if ((String(beat)).length == 2) beat = "0"+beat;
  748. return beat;
  749. },
  750. g: function(){return jsdate.getHours() % 12 || 12},
  751. G: function(){return jsdate.getHours()},
  752. h: function(){return pad(f.g(), 2)},
  753. H: function(){return pad(jsdate.getHours(), 2)},
  754. i: function(){return pad(jsdate.getMinutes(), 2)},
  755. s: function(){return pad(jsdate.getSeconds(), 2)},
  756. O: function(){
  757. var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4);
  758. if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;
  759. return t;
  760. },
  761. P: function(){var O = f.O();return (O.substr(0, 3) + ":" + O.substr(3, 2))},
  762. c: function(){return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P()},
  763. U: function(){return Math.round(jsdate.getTime()/1000)}
  764. };
  765. return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){
  766. if( t!=s ){
  767. ret = s;
  768. } else if( f[s] ){
  769. ret = f[s]();
  770. } else{
  771. ret = s;
  772. }
  773. return ret;
  774. });
  775. }
  776. var Base64 = (function(){
  777. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  778. var encode = function (input) {
  779. var output = "";
  780. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  781. var i = 0;
  782. input = _utf8_encode(input);
  783. while (i < input.length) {
  784. chr1 = input.charCodeAt(i++);
  785. chr2 = input.charCodeAt(i++);
  786. chr3 = input.charCodeAt(i++);
  787. enc1 = chr1 >> 2;
  788. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  789. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  790. enc4 = chr3 & 63;
  791. if (isNaN(chr2)) {
  792. enc3 = enc4 = 64;
  793. } else if (isNaN(chr3)) {
  794. enc4 = 64;
  795. }
  796. output = output +
  797. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  798. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  799. }
  800. return output;
  801. }
  802. // public method for decoding
  803. var decode = function (input) {
  804. var output = "";
  805. var chr1, chr2, chr3;
  806. var enc1, enc2, enc3, enc4;
  807. var i = 0;
  808. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  809. while (i < input.length) {
  810. enc1 = _keyStr.indexOf(input.charAt(i++));
  811. enc2 = _keyStr.indexOf(input.charAt(i++));
  812. enc3 = _keyStr.indexOf(input.charAt(i++));
  813. enc4 = _keyStr.indexOf(input.charAt(i++));
  814. chr1 = (enc1 << 2) | (enc2 >> 4);
  815. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  816. chr3 = ((enc3 & 3) << 6) | enc4;
  817. output = output + String.fromCharCode(chr1);
  818. if (enc3 != 64) {
  819. output = output + String.fromCharCode(chr2);
  820. }
  821. if (enc4 != 64) {
  822. output = output + String.fromCharCode(chr3);
  823. }
  824. }
  825. output = _utf8_decode(output);
  826. return output;
  827. }
  828. // private method for UTF-8 encoding
  829. _utf8_encode = function (string) {
  830. string = string.replace(/\r\n/g,"\n");
  831. var utftext = "";
  832. for (var n = 0; n < string.length; n++) {
  833. var c = string.charCodeAt(n);
  834. if (c < 128) {
  835. utftext += String.fromCharCode(c);
  836. } else if((c > 127) && (c < 2048)) {
  837. utftext += String.fromCharCode((c >> 6) | 192);
  838. utftext += String.fromCharCode((c & 63) | 128);
  839. } else {
  840. utftext += String.fromCharCode((c >> 12) | 224);
  841. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  842. utftext += String.fromCharCode((c & 63) | 128);
  843. }
  844. }
  845. return utftext;
  846. }
  847. // private method for UTF-8 decoding
  848. _utf8_decode = function (utftext) {
  849. var string = "";
  850. var i = 0;
  851. var c = c1 = c2 = 0;
  852. while ( i < utftext.length ) {
  853. c = utftext.charCodeAt(i);
  854. if (c < 128) {
  855. string += String.fromCharCode(c);
  856. i++;
  857. } else if((c > 191) && (c < 224)) {
  858. c2 = utftext.charCodeAt(i+1);
  859. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  860. i += 2;
  861. } else {
  862. c2 = utftext.charCodeAt(i+1);
  863. c3 = utftext.charCodeAt(i+2);
  864. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  865. i += 3;
  866. }
  867. }
  868. return string;
  869. };
  870. return {
  871. encode:encode,
  872. decode:decode
  873. }
  874. })();
  875. var base64_encode = Base64.encode;
  876. var base64_decode = Base64.decode;