sg.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /****************************************************************
  2. * *
  3. * 代码库 *
  4. * www.dmaku.com *
  5. * 努力创建完善、持续更新插件以及模板 *
  6. * *
  7. ****************************************************************/
  8. var zindex = 100;
  9. var timerInterval = null;
  10. var isMouseDownDoing = false;
  11. var ajaxTimeout = null;
  12. (function($){
  13. $.tmUtil = {
  14. dialogHtml : function(opts){
  15. if(opts.single){
  16. $(".tmui-modal").remove();
  17. $(".tmui-overlay").remove();
  18. };
  19. var dialogHtml = "<div class='b_l w460'><div class='bcom_ti'>"+
  20. "<a href='javascript:void(0)' class='bide layer_icon fr tmui-modal-close'></a>"+
  21. "<span>"+opts.title+"</span></div>"+
  22. "<div class='bcom_cent'>"+
  23. "<p class='bcomti'>"+opts.content+"</p>"+
  24. "<p class='bcoma'><a href='javascript:void(0)' class='bc_abut1 tmui-modal-sure'>"+opts.sureText+"</a>"+
  25. "<a href='javascript:void(0)' class='bc_abut2 tmui-modal-cancle'>"+opts.cancleText+"</a></p>"+
  26. "</div>";
  27. return $(dialogHtml);
  28. },
  29. _position : function($obj,opts){
  30. var windowHeight = $(window).height();
  31. var windowWidth = $(window).width();
  32. var left =opts.left || (windowWidth - $obj.width())/2;
  33. var top = opts.top || (windowHeight - $obj.height())/2;
  34. if(opts.open=="top"){
  35. $obj.css("left",left).stop().animate({top:top});
  36. }else if(opts.open=="left"){
  37. $obj.css("top",top).stop().animate({left:left});
  38. }else if(opts.open=="fade"){
  39. $obj.hide().css({left:left,top:top}).stop().fadeIn("slow");
  40. }else if(opts.open=="slide"){
  41. $obj.hide().css({left:left,top:top}).stop().slideDown("slow");
  42. }else if(opts.open=="message"){
  43. var left = $.tmUtil._getClientWidth()-opts.width-3;
  44. var top = $.tmUtil._getClientHeight()-opts.height-3;
  45. $obj.css({left:left,top:$.tmUtil._getClientHeight()}).stop().animate({top:"+"+top+"px"});
  46. }else{
  47. $obj.css({"left":left,"top":top});
  48. }
  49. },
  50. _resize : function($obj,opts){
  51. $(window).on("resize",function(){
  52. $.tmUtil._position($obj,opts);
  53. $obj.next(".tmui-overlay").height($.tmUtil._getScrollHeight());
  54. });
  55. },
  56. _overlay:function($dialog){
  57. var height = this._getScrollHeight();
  58. var zindexc = $dialog.css("z-index");
  59. zindexc--;
  60. var $overLayObj = $('<div class="tmui-overlay" style="height:'+height+'px;z-index:'+zindexc+'"></div>');
  61. $dialog.after($overLayObj);
  62. },
  63. _getClientHeight : function() {
  64. var clientHeight = 0;
  65. if (document.body.clientHeight && document.documentElement.clientHeight) {
  66. clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
  67. } else {
  68. clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
  69. }
  70. return clientHeight;
  71. },
  72. _getClientWidth : function() {
  73. var clientWidth = 0;
  74. if (document.body.clientWidth && document.documentElement.clientWidth) {
  75. clientWidth = (document.body.clientWidth < document.documentElement.clientWidth) ? document.body.clientWidth: document.documentElement.clientWidth;
  76. } else {
  77. clientWidth = (document.body.clientWidth > document.documentElement.clientWidth) ? document.body.clientWidth: document.documentElement.clientWidth;
  78. }
  79. return clientWidth;
  80. },
  81. _getScrollHeight : function (){
  82. return Math.max(this._getClientHeight(),document.body.scrollHeight,document.documentElement.scrollHeight);
  83. },
  84. _getHeight: function() {
  85. return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
  86. },
  87. _getWidth: function() {
  88. return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
  89. },
  90. _showOpen : function($dialog,opts){
  91. zindex++;
  92. $dialog.css("zIndex",zindex);
  93. if(opts.open !="other"){
  94. $dialog.show().css({width:opts.width,height:opts.height,"margin":"none"});
  95. $("body").append($dialog);
  96. $.tmUtil._position($dialog,opts);
  97. $.tmUtil._resize($dialog,opts);
  98. }
  99. if(opts.open=="other"){
  100. if(opts.animateBefore=="before"){
  101. $dialog.css("margin","auto").show().stop().animate({width:opts.width,height:opts.height});
  102. $("body").append($dialog);
  103. }
  104. if(opts.animateBefore=="after"){
  105. $("body").append($dialog);
  106. $dialog.css("margin","auto").show().stop().animate({width:opts.width,height:opts.height});
  107. }
  108. }
  109. },
  110. _animateClose : function($obj,opts){
  111. var height = $obj.offset().top+$obj.height()*2;
  112. var width = $obj.offset().left+$obj.width()*2;
  113. if(opts.open=="top"){
  114. $obj.stop().animate({top:"-"+height+"px"},function(){
  115. if(opts.showOverlay)$(this).next().remove();
  116. $(this).remove();
  117. });
  118. }else if(opts.open=="left"){
  119. $obj.stop().animate({left:"-"+width+"px"},function(){
  120. if(opts.showOverlay)$(this).next().remove();
  121. $(this).remove();
  122. });
  123. }else if(opts.open =="fade"){
  124. $obj.stop().fadeOut("slow",function(){
  125. if(opts.showOverlay)$(this).next().remove();
  126. $(this).remove();
  127. });
  128. }else if(opts.open=="slide"){
  129. $obj.stop().slideUp("slow",function(){
  130. if(opts.showOverlay)$(this).next().remove();
  131. $(this).remove();
  132. });
  133. }else if(opts.open=="other"){
  134. $obj.stop().animate({width:0,height:0},function(){
  135. if(opts.showOverlay)$(this).next().remove();
  136. $(this).remove();
  137. });
  138. }else if(opts.open=="message"){
  139. $obj.stop().animate({top:$.tmUtil._getClientHeight()},function(){
  140. if(opts.showOverlay)$(this).next().remove();
  141. $(this).remove();
  142. });
  143. }else{
  144. if(opts.showOverlay)$obj.next().remove();
  145. $obj.remove();
  146. }
  147. },
  148. _shake : function (obj){
  149. var style = obj.style,
  150. p = [14, 18, 14, 10, -14, -18, -14, 10,12,0,-12,11],
  151. fx = function () {
  152. style.marginLeft = p.shift() + 'px';
  153. if (p.length <= 0) {
  154. style.marginLeft = 0;
  155. clearInterval(timerId);
  156. };
  157. };
  158. p = p.concat(p.concat(p));
  159. timerId = setInterval(fx, 13);
  160. },
  161. _overlayClick:function($dialog,opts){
  162. $dialog.next().click(function(e){
  163. $.tmUtil._animateClose($dialog,opts);
  164. stopBubble(e);
  165. });
  166. }
  167. };
  168. $.tmDialog = {
  169. confirm :function(options){
  170. if(isEmpty(options.icon))options.icon = "warn";
  171. this._init(options);
  172. },
  173. _init:function(options){
  174. var opts = $.extend({},$.tmDialog,$.tmDialog.defaults,options);
  175. var $dialog = $.tmUtil.dialogHtml(opts);
  176. if(isNotEmpty(opts.id))$dialog.attr("id",opts.id);
  177. $dialog.data("options",opts);
  178. $.tmUtil._showOpen($dialog,opts);//打开特效
  179. this._bindEvent($dialog,opts);//绑定事件
  180. if(!eval(opts.showIcon))$dialog.find(".tmui-modal-icon").remove();
  181. if(opts.drag)$dialog.tmDrag({handle:$dialog.find(".tmui-modal-header")});//是否允许拖动
  182. if(opts.width<=360)opts.width = 360;
  183. if(opts.height<=200)opts.height = 260;
  184. if(opts.showOverlay)$.tmUtil._overlay($dialog);//是否有阴影层
  185. if(opts.showHeader){$dialog.find(".tmui-modal-header").hide();}
  186. if(eval(opts.showIcon)){
  187. $dialog.find(".tmui-modal-content").width(opts.width-148);
  188. }else{
  189. $dialog.find(".tmui-modal-content").css({"width":(opts.width-100),"textAlign":opts.arrow||"center","padding":0,"padding":50});
  190. }
  191. if(!eval(opts.showBottom)){
  192. $dialog.find(".tmui-modal-bottom").remove();
  193. var h = opts.height-55;
  194. $dialog.height(h).find(".tmui-modal-body").height(h);
  195. }else{
  196. $dialog.find(".tmui-modal-body").height(opts.height-125);
  197. }
  198. if(!eval(opts.showClose)){
  199. $dialog.find(".tmui-modal-close").remove();
  200. $dialog.find(".tmui-modal-cancle").remove();
  201. }
  202. if(eval(opts.overlayHide))$.tmUtil._overlayClick($dialog,opts);
  203. if(eval(opts.colors).length>0){
  204. var colorx = eval(opts.colors);
  205. $dialog.find(".tmui-modal-header").css({"background":colorx[0]});
  206. $dialog.find(".tmui-modal-sure").css({"background":colorx[1]||colorx[0]});
  207. $dialog.find(".tmui-modal-cancle").css({"background":colorx[2]||colorx[0]});
  208. }
  209. if(isNotEmpty(opts.timer))this._timer($dialog,opts);
  210. if(eval(opts.shake))$.tmUtil._shake($dialog.get(0));
  211. if(isNotEmpty(opts.left))$dialog.css("left",opts.left);
  212. if(isNotEmpty(opts.top)){$dialog.css("top",opts.top);}
  213. if(isNotEmpty(opts.zindex)){
  214. $dialog.css("zIndex",opts.zindex).next().css("zIndex",opts.zindex-1);
  215. }
  216. return $dialog;
  217. },
  218. _limitRandom : function (begin,end){
  219. return Math.floor(Math.random()*(end-begin))+begin;
  220. },
  221. _bindEvent :function($dialog,opts){
  222. $dialog.find(".tmui-modal-cancle,.tmui-modal-close").off().on("click",function(e){
  223. if(opts.callback)opts.callback(false);
  224. $.tmDialog._remove($dialog,opts);
  225. if(opts.content instanceof jQuery){
  226. $("body").append(opts.content.hide());
  227. }
  228. });
  229. $dialog.find(".tmui-modal-sure").off().on("click",function(){
  230. if(opts.callback)opts.callback(true);
  231. $.tmDialog._remove($dialog,opts);
  232. });
  233. },
  234. _timer : function($dialog,opts){
  235. var timecount = opts.timer*1000 / 1000;
  236. clearInterval(timerInterval);
  237. timerInterval = setInterval(function(){
  238. $dialog.find(".tmui-modal-timer").html("("+timecount+")");
  239. if(timecount<=1){
  240. $.tmUtil._animateClose($dialog,opts);
  241. clearInterval(timerInterval);
  242. }
  243. timecount--;
  244. },1000);
  245. },
  246. _remove : function($dialog,opts){
  247. if(timerInterval)clearInterval(timerInterval);
  248. $.tmUtil._animateClose($dialog,opts);
  249. },
  250. _zindex:function(){
  251. var arr = [];
  252. $(".tmui-modal").each(function(){
  253. arr.push($(this).css("z-index"));
  254. });
  255. var max = Math.max.apply({},arr)*1 || 100;
  256. max++;
  257. return max;
  258. },
  259. window : function(options){
  260. options = $.extend({},$.tmDialog.defaults,options);
  261. var $wp = $("#tm_window_"+options.id);
  262. if(isNotEmpty($wp.html())){
  263. var max = $.tmDialog._zindex();
  264. $wp.show().css({"zIndex":max});
  265. $wp.next().css({"zIndex":(max-1)});
  266. return;
  267. }
  268. options.id = "tm_window_"+options.id;/*给id重命名*/
  269. options.showBottom = false;
  270. options.showIcon = false;
  271. if(!options.showCenter){//是否居中显示
  272. options.top = this._limitRandom(30,60);
  273. options.left = this._limitRandom(300,400);
  274. }
  275. var $window = this._init(options);
  276. $window.find(".tmui-modal-body").html("<div id='tmDialog_loading' style='position:absolute;top:50%;left:45%;'><img src='images/loading.gif'><label style='font-size:12px;'>数据马上就来...</label></div>");
  277. if(!options.ajax){
  278. var iframe=document.createElement("iframe");
  279. iframe.id = "tmiframe_"+options.id ;
  280. iframe.width= options.width;
  281. iframe.height = options.height;
  282. iframe.scrolling = "auto";
  283. iframe.frameborder = "no";
  284. iframe.src = options.content;
  285. iframe.style.display ="none";
  286. iframe.style.border ="0";
  287. $(iframe).attr("frameborder","no");
  288. $window.find(".tmui-modal-body").css({"overflowX":"hidden","overflowY":"auto","height":options.height}).append(iframe);
  289. $(iframe).load(function(){
  290. $window.find("#tmDialog_loading").remove();
  291. iframe.style.display ="block";
  292. });
  293. }else{
  294. $window.find(".tmui-modal-body").css({"height":options.height}).load(options.content,function(){
  295. $window.find(".tmui-modal-close-proxy").on("click",function(){
  296. $window.fadeOut("slow",function(){
  297. $window.next().remove();
  298. $window.remove();
  299. });
  300. });
  301. $window.tmDrag({handle:$window.find(".tmui-drag-header")});//是否允许拖动
  302. });
  303. }
  304. $window.find(".tmui-modal-header").css("paddingLeft",3).prepend("<div class='fl' style='padding-top:4px;padding-right:5px;'><img src='"+options.wicon+"' width='22' height='23'/></div>&nbsp;");
  305. $window.find(".tmui-modal-toolbars").append("<button type='button' title='最大化' class='tmui-modal-max'><span class='tmui-modal-span'>□</span></button><button type='button' title='最大小化' class='tmui-modal-min'><span class='tmui-modal-span'>-</span></button>");
  306. $window.find(".tmui-modal-min").on("click",function(){
  307. $window.tmDrag({handle:$window.find(".tmui-modal-header")});//是否允许拖动
  308. });
  309. var max = $.tmDialog._zindex();
  310. $window.show().css("zIndex",max);
  311. $window.next().css({"zIndex":(max-1)});
  312. /*附加代理层*/
  313. $("#tmui_resizable").remove();
  314. $("body").append("<div id='tmui_resizable'></div>");
  315. /*添加resize边角*/
  316. $window.append("<div class='tmui-resize tmui-resize-ll'></div>" +
  317. "<div class='tmui-resize tmui-resize-tt'></div>" +
  318. "<div class='tmui-resize tmui-resize-rr'></div>" +
  319. "<div class='tmui-resize tmui-resize-bb'></div>" +
  320. "<div class='tmui-resize tmui-resize-tr'></div>" +
  321. "<div class='tmui-resize tmui-resize-tl'></div>" +
  322. "<div class='tmui-resize tmui-resize-br'></div>" +
  323. "<div class='tmui-resize tmui-resize-bl'></div>");
  324. /*最大化*/
  325. $window.find(".tmui-modal-max").off().on("click",function(){
  326. var open = $(this).data("open");
  327. var max = $.tmDialog._zindex();
  328. $window.css("zIndex",max);
  329. var width = $.tmUtil._getClientWidth();
  330. var height = $.tmUtil._getClientHeight();
  331. if(isEmpty(open)){
  332. var styleData = $window.attr("style");
  333. $(this).data({"open":"open","style":styleData});
  334. $window.css({left:0,top:1,width:"100%",height:height-70});
  335. $(this).find("span").text("■");
  336. $window.find(".tmui-modal-header").css("cursor","default").off();
  337. $window.find("iframe").height(height).width(width);
  338. $window.find(".tmui-modal-body").height(height).width(width);
  339. }else{
  340. $window.attr("style",$(this).data("style"));
  341. $window.find("iframe").height($window.height()).width($window.width());
  342. $window.find(".tmui-modal-body").height(height).width(width);
  343. $window.tmDrag({handle:$window.find(".tmui-modal-header")});//是否允许拖动
  344. $(this).find("span").text("□");
  345. $(this).removeData("open");
  346. $(this).removeData("style");
  347. }
  348. if(options.maxcallback)options.maxcallback($window,options);
  349. });
  350. $window.find(".tmui-modal-min").off().on("click",function(){
  351. if(options.mincallback)options.mincallback($window,options);
  352. var max = $.tmDialog._zindex();
  353. $window.css("zIndex",max);
  354. });
  355. if(!options.showMax){
  356. $window.find(".tmui-modal-min").css("right",45);
  357. $window.find(".tmui-modal-max").remove();
  358. }
  359. if(!options.showMin)$window.find(".tmui-modal-min").remove();
  360. if(!options.showHeader){
  361. $window.find(".tmui-modal-header").css({"position":"absolute","width":"100%","height":47,"zIndex":2});
  362. $window.hover(function(){
  363. $(this).find(".tmui-modal-header").show();
  364. },function(){
  365. $(this).find(".tmui-modal-header").hide();
  366. });
  367. }
  368. if(!options.removeHeader){
  369. $window.find(".tmui-modal-header").remove();
  370. }
  371. if(options.showResize){
  372. var windowDom = $window.get(0);
  373. var oL = $window.find(".tmui-resize-ll").get(0);
  374. var oT = $window.find(".tmui-resize-tt").get(0);
  375. var oR = $window.find(".tmui-resize-rr").get(0);
  376. var oB = $window.find(".tmui-resize-bb").get(0);
  377. var oLT = $window.find(".tmui-resize-tr").get(0);
  378. var oTR = $window.find(".tmui-resize-tl").get(0);
  379. var oBR = $window.find(".tmui-resize-br").get(0);
  380. var oLB = $window.find(".tmui-resize-bl").get(0);
  381. /*四角*/
  382. this._windowResize(windowDom, oLT, true, true, false, false,options);
  383. this._windowResize(windowDom, oTR, false, true, false, false,options);
  384. this._windowResize(windowDom, oBR, false, false, false, false,options);
  385. this._windowResize(windowDom, oLB, true, false, false, false,options);
  386. /*四边*/
  387. this._windowResize(windowDom, oL, true, false, false, true,options);
  388. this._windowResize(windowDom, oT, false, true, true, false,options);
  389. this._windowResize(windowDom, oR, false, false, false, true,options);
  390. this._windowResize(windowDom, oB, false, false, true, false,options);
  391. }else{
  392. $window.find(".tmui-resize").remove();
  393. }
  394. },
  395. _windowResize : function(oParent, handle, isLeft, isTop, lockX, lockY,opts){
  396. var dragMinWidth = opts.limitWidth;
  397. var dragMinHeight = opts.limitHeight;
  398. handle.onmousedown = function (event){
  399. var max = $.tmDialog._zindex();
  400. $(oParent).css("zIndex",max);
  401. var e = event || window.event;
  402. var disX = e.clientX - handle.offsetLeft;
  403. var disY = e.clientY - handle.offsetTop;
  404. var iParentTop = oParent.offsetTop;
  405. var iParentLeft = oParent.offsetLeft;
  406. var iParentWidth = oParent.offsetWidth;
  407. var iParentHeight = oParent.offsetHeight;
  408. document.onmousemove = function (event){
  409. var e = event || window.event;
  410. var iL = e.clientX - disX;
  411. var iT = e.clientY - disY;
  412. var maxW = document.documentElement.clientWidth - oParent.offsetLeft - 2;
  413. var maxH = document.documentElement.clientHeight - oParent.offsetTop - 2;
  414. var iW = isLeft ? iParentWidth - iL : handle.offsetWidth + iL;
  415. var iH = isTop ? iParentHeight - iT : handle.offsetHeight + iT;
  416. isLeft && (oParent.style.left = iParentLeft + iL + "px");
  417. isTop && (oParent.style.top = iParentTop + iT + "px");
  418. iW < dragMinWidth && (iW = dragMinWidth);
  419. iW > maxW && (iW = maxW);
  420. lockX || (oParent.style.width = iW + "px");
  421. iH < dragMinHeight && (iH = dragMinHeight);
  422. iH > maxH && (iH = maxH);
  423. lockY || (oParent.style.height = iH + "px");
  424. if((isLeft && iW == dragMinWidth) || (isTop && iH == dragMinHeight)) document.onmousemove = null;
  425. $(oParent).find("iframe").height(iH).width(iW);
  426. $(oParent).find(".tmui-modal-body").height(iH).width(iW);
  427. return false;
  428. };
  429. document.onmouseup = function (){
  430. document.onmousemove = null;
  431. document.onmouseup = null;
  432. };
  433. return false;
  434. };
  435. }
  436. };
  437. $.fn.tmDrag = function (settings) {
  438. return this.each(function () {
  439. var $drag = $(this),options = $.extend({},$.fn.tmDrag.defaults,$.fn.tmDrag.defaults.parseOptions(this),settings),
  440. // 拖动对象
  441. $handle = options.handle ? $drag.find(options.handle) : $drag,
  442. // 拖动区域对象
  443. $zoom = $(options.zoom),
  444. // 拖动开始的位置
  445. startPos = {},
  446. dragFix = {},
  447. // body 默认 cursor
  448. cursor = $("body").css("cursor"),
  449. // 默认的 zIndex 值
  450. zIndex = $drag.css("z-index"),
  451. // 是否正在拖动
  452. isDraging = 0,ghsotDiv;
  453. $handle.css("cursor", "move");
  454. _checkPosition($drag);
  455. if(!options.isDrag)return;
  456. // html4 拖拽
  457. $handle.mousedown(function (e) {
  458. var evt = e || event;
  459. if(evt.which==3 || evt.button==2)return;
  460. var left = $drag.offset().left;
  461. var top = $drag.offset().top;
  462. $drag.css({top:top,left:left,"margin":0,"position":"absolute"});
  463. if(options.ghsot){ghsotDiv = options.ghsotEvent($drag);}
  464. if (!isDraging) {
  465. _ondragstrart(e);
  466. return false;
  467. }
  468. });
  469. $(document).mousemove(function (e) {
  470. if (isDraging) {
  471. _ondragpos(e);
  472. return false;
  473. }
  474. }).mouseup(function (e) {
  475. if (isDraging) {
  476. _ondragpos(e, true);
  477. return false;
  478. }
  479. });
  480. /**
  481. * 检查拖动对象的position
  482. * @return {undefined}
  483. */
  484. function _checkPosition() {
  485. if (!$drag.css("position")) {
  486. $drag.css({
  487. position: "absolute",
  488. left: 0,
  489. top: 0
  490. });
  491. }
  492. }
  493. /**
  494. * 开始拖动
  495. * @param {Object} event对象
  496. * @return {undefined}
  497. */
  498. function _ondragstrart(e) {
  499. isDraging = 1;
  500. startPos.screenX = e.screenX;
  501. startPos.screenY = e.screenY;
  502. startPos.left = $drag.offset().left;
  503. startPos.top = $drag.offset().top;
  504. $parentbox = options.parent;
  505. boxHeight = $(window).height()+$(window).scrollTop()-($drag.height()/5);
  506. boxWidth = $.tmUtil._getClientWidth()-($drag.width()/5);
  507. if($parentbox){
  508. limitLeft =$parentbox.offset().left;
  509. limitTop = $parentbox.offset().top;
  510. //limitWidth = $parentbox.width();
  511. //limitHeight = $parentbox.height();
  512. }
  513. var max = $.tmDialog._zindex();
  514. $drag.css("zIndex",max);
  515. options.ondragstart.call($drag, e);
  516. $("body").css("cursor", "move");
  517. }
  518. /**
  519. * 改变拖拽位置
  520. * @param {Object} event对象
  521. * @return {undefined}
  522. */
  523. function _ondragpos(e, isStop) {
  524. // 正在拖动并且不支持html5
  525. if (isDraging) {
  526. var _left = e.screenX - startPos.screenX + startPos.left;
  527. var _top = e.screenY - startPos.screenY + startPos.top;
  528. if($parentbox){
  529. if(_left<limitLeft)_left=limitLeft+3;
  530. if(_top<limitTop)_top=limitTop+3;
  531. if(_top>boxHeight)_top = boxHeight+8;
  532. if(_left>boxWidth)_left=boxWidth+8;
  533. }else{
  534. if(_left<(boxWidth*-1))_left= boxWidth*-1;
  535. if(_top<2)_top=2;
  536. if(_left>boxWidth)_left = boxWidth;
  537. if(_top>boxHeight)_top=boxHeight-options.arrowTop;
  538. }
  539. if(isNotEmpty(options.arrow) && options.arrow=="left"){
  540. _top = $drag.offset().top;
  541. }
  542. if(isNotEmpty(options.arrow) && options.arrow=="top"){
  543. _left = $drag.offset().left;
  544. }
  545. dragFix.left = _left;
  546. dragFix.top = _top;
  547. /*镜像处理*/
  548. if(options.ghsot){
  549. ghsotDiv.css({left:_left+"px",top:_top+"px"});
  550. }else{
  551. /*普通处理*/
  552. $drag.offset({left: _left,top: _top});
  553. }
  554. }
  555. // 停止
  556. if (isStop && isDraging) {
  557. if(options.ghsot){
  558. $drag.css({left:dragFix.left,top:dragFix.top});
  559. ghsotDiv.remove();
  560. }
  561. //$drag.css("z-index", zIndex);
  562. isDraging = 0;
  563. options.ondragend.call($drag, e);
  564. $("body").css("cursor", cursor);
  565. } else {
  566. options.ondrag.call($drag, e);
  567. }
  568. }
  569. });
  570. };
  571. $.fn.tmDrag.defaults = {
  572. // 鼠标操作区域
  573. handle: "",
  574. parent:"",
  575. arrow:"",
  576. arrowTop:30,
  577. isDrag:true,
  578. // 拖动的时候层级的高度
  579. zIndex: 999,
  580. // 拖动开始回调
  581. ondragstart: function () {},
  582. // 拖动中回调
  583. ondrag: function () {},
  584. // 拖动结束回调
  585. ondragend: function () {},
  586. ghsot:true,
  587. ghsotEvent:function($this){
  588. var ghsotDiv = $("<div class='ghsot'><div>");
  589. var selfHeight = $this.outerHeight(true);//容器自身的高度加border
  590. var selfWidth = $this.outerWidth(true);//容器自身的宽度加border
  591. var $offset = $this.offset();
  592. $("body").append(ghsotDiv);
  593. ghsotDiv.css({zIndex:999,border:"2px dotted #f9f9f9","boxShadow":"0px 0px 1.5em #111",opacity:0.35,position:"absolute",width:selfWidth,height:selfHeight});
  594. return ghsotDiv;
  595. }
  596. };
  597. $.fn.tmDrag.defaults.parseOptions = function(target){
  598. var $this = $(target);
  599. var ghsot = true;
  600. var arrow = "";
  601. var parent = "";
  602. var handle = "";
  603. var ghsotp = $this.attr("ghsot");
  604. var arrowp = $this.attr("arrow");
  605. var parentp = $this.attr("parent");
  606. var handlep = $this.attr("handle");
  607. if(isNotEmpty(ghsotp) && ghsotp=="false")ghsot=false;
  608. if(isNotEmpty(arrowp))arrow = arrowp;
  609. if(isNotEmpty(parentp))parent = $("#"+parentp);
  610. if(isNotEmpty(handlep))handle = $("#"+handlep);
  611. return {
  612. ghsot:ghsot,
  613. arrow:arrow,
  614. parent:parent,
  615. handle:handle
  616. };
  617. };
  618. $.tmDialog.defaults = {
  619. id:"",//标示符
  620. ajax:false,
  621. open:"top",//打开方式。如果是default的话没有动画效果 //关闭动画的效果 fade left top default slide,other
  622. animateBefore:"before",//如果open不是默认的动画效果,如果为true打开为前置动画,false为后置动画
  623. position:"fixed",//定位方式
  624. single:false,//是否采用单例模式
  625. width:358,//宽度
  626. height:228,//高度
  627. colors:[],//换肤
  628. drag:true,//是否允许拖拽
  629. shake:false,//是否抖动代开
  630. showIcon:true,//是否显示图标
  631. showBottom:true,//是否显示底部
  632. showHeader:false,//是否显示头部
  633. showResize:true,//是否resize
  634. showMin:true,//是否显示最小化
  635. showMax:true,//是否显示最大化
  636. showClose:true,//是否显示关闭
  637. showCenter:false,//默认随机显示位置
  638. showOverlay:true,//是否需要阴影层
  639. removeHeader:true,//删除头
  640. zindex:"",
  641. overlayHide:false,//
  642. icon:"success",//默认按钮
  643. limitWidth :360,//resize限制宽度
  644. limitHeight:88,//resize限制宽度
  645. wicon:"",//窗体图标
  646. message:false,//消息设置
  647. title:"提示",//标题
  648. value:"",//prompt的值
  649. left:'40%',//设定left
  650. top:'40%',//设定top
  651. timer:"",//几秒关闭
  652. content:"请输入内容....",//显示的内容
  653. cancleText:"取消",//取消按钮文件
  654. sureText :"确定",//确定按钮文件
  655. textarea:false,//prompt的展示
  656. finish:function(){
  657. },
  658. loadSuccess:function(){
  659. },
  660. validator:function($input){
  661. return true;
  662. },
  663. callback:function(ok){
  664. },
  665. mincallback:function(){
  666. },
  667. maxcallback:function(){
  668. }
  669. };
  670. $.tmLoading = function(content,options){
  671. var opts = $.extend({},$.tmLoading.defaults,options);
  672. if($(".tmui-loading").length==0)$("body").append('<div class="tmui-loading" title="click close me!"><span id="tm-loading-content"></span></div>');
  673. var $loading = $(".tmui-loading");
  674. if(opts.skin=="black"){
  675. $(".tmui-loading").css({background:"#3e4146","color":"#fff","border":"1px solid #555"});
  676. }
  677. if(opts.overlay){
  678. var overlayHeight = Math.max($.tmUtil._getClientHeight(),document.body.scrollHeight,document.documentElement.scrollHeight);
  679. $("body").append("<div class=\"tmui_loading_overlay\" style=\"z-index: 1001; height: 100%; display: none;\"></div>");
  680. $(".tmui_loading_overlay").on("click",function(){
  681. $loading.slideUp(250,function(){
  682. $(this).remove();
  683. clearInterval(loadingTimer);
  684. });
  685. $(this).remove();
  686. }).css({"opacity":"0.6","z-index":"999","height":overlayHeight}).show();
  687. }
  688. if(!opts.showLoad)$loading.find("#tm-loading-content").css("background","none");
  689. if(content=="remove"){
  690. $loading.slideUp(250,function(){
  691. opts.timeSuccess($loading);
  692. $(".tmui_loading_overlay").remove();
  693. if(opts._remove)$loading.remove();
  694. });
  695. }else{
  696. if(isEmpty(content))content = opts.content ;
  697. $loading.show().find("#tm-loading-content").html(content);
  698. var selfWidth = $loading.width();
  699. var selfHeight = $loading.height();
  700. var left = ($.tmUtil._getClientWidth()-selfWidth)/2;
  701. var top = ($.tmUtil._getHeight()-selfHeight)/2;
  702. if(isNotEmpty(opts.left))left= opts.left;
  703. if(isNotEmpty(opts.top))top = opts.top;
  704. $loading.css({left:left,top:top});
  705. opts.callback($loading,opts);
  706. var loadingTimer = null;
  707. if(opts.timer>0){
  708. loadingTimer = setTimeout(function(){
  709. $loading.slideUp(250,function(){
  710. clearInterval(loadingTimer);
  711. opts.timeSuccess($loading);
  712. $(".tmui_loading_overlay").remove();
  713. if(opts._remove)$loading.remove();
  714. });},opts.timer*1000);
  715. }else{
  716. $(".tmui_loading_overlay").remove();
  717. if(opts._remove)$loading.remove();
  718. }
  719. }
  720. };
  721. $.tmLoading.defaults = {
  722. top:"",
  723. left:"",
  724. timer:0,
  725. _remove:false,
  726. skin:"black",
  727. content:"loading...",
  728. showLoad:true,
  729. overlay:false,
  730. timeSuccess:function(){
  731. },
  732. callback:function($this,opts){
  733. if(opts._remove){
  734. $this.on("click",function(){
  735. $this.slideUp(250,function(){
  736. $(this).remove();
  737. });
  738. $(".tmui_loading_overlay").remove();
  739. });
  740. }else{
  741. $this.on("click",function(){
  742. $this.slideUp(250,function(){
  743. $(this).hide();
  744. });
  745. $(".tmui_loading_overlay").remove();
  746. });
  747. }
  748. }
  749. };
  750. /*loading plugin end*/
  751. /*tpAjax*/
  752. $.tpAjax = {
  753. request : function(options,dataJson){
  754. var opts = $.extend({},{limit:true,beforeSend:function(){
  755. //tmLoading("数据处理中,请稍后...",1);
  756. },error:function(){
  757. },callback:function(data){
  758. }},options);
  759. var _url = opts.url;
  760. if(isEmpty(_url)){
  761. _url = jsonPath+"/"+opts.model+"/"+opts.method+"?ajax=true";
  762. }
  763. if(isNotEmpty(opts.params)){
  764. _url+="&"+opts.params;
  765. }
  766. if(opts.limit){
  767. clearTimeout(ajaxTimeout);
  768. ajaxTimeout = setTimeout(function(){
  769. $.tpAjax.ajaxMain(opts,_url,dataJson);
  770. },200);
  771. }else{
  772. $.tpAjax.ajaxMain(opts,_url, dataJson);
  773. }
  774. },
  775. ajaxMain:function(opts,_url,dataJson){
  776. $.ajax({
  777. type:"post",
  778. data : dataJson,
  779. url : _url,
  780. beforeSend:function(){opts.beforeSend();},
  781. error:function(){tmLoading("抱歉!因为操作不能够及时响应,请稍后在试...",1);opts.error();clearTimeout(ajaxTimeout);},
  782. success:function(data){
  783. if(data.result=="logout"){
  784. $.tmLogin._dialogLogin();
  785. }else if(data.result=="frontLogout"){
  786. window.location = "/tp/login/login.html";
  787. }else{
  788. if(opts.callback)opts.callback(data);
  789. }
  790. clearTimeout(ajaxTimeout);
  791. }
  792. });
  793. }
  794. };
  795. })(jQuery);