fw.popwin.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * options can be:
  3. * title
  4. * width
  5. * height
  6. * top
  7. * left
  8. * center this option has a hight priority than top and left options
  9. * content
  10. * modal
  11. * canMove
  12. * glueMouse
  13. * closeAction:'close' or 'hide'
  14. * buttons [NA]
  15. *
  16. *
  17. * your can pass these options to the construtor or the show method:
  18. {
  19. modal:true, //show as a modal window or not
  20. canMove:true, //whether your window can be moved
  21. glueMouse:true,//if set to true and canMove is true,you can move your window without keeping pressing the mouse
  22. left:500, //position
  23. top:300, //position
  24. center:true,//whether to center your window in your explorer. If this option is set to true the left and top option will be ignored
  25. width:400, //size
  26. height:300, //size
  27. title:'new title', //the title
  28. content:'your content',//your content
  29. closeAction:'hide' //'hide' means hide the window and 'close' means destroy the window. You can not show it again if you close a window
  30. }
  31. */
  32. var CFWPopWin = _FW.fCreateClass();
  33. CFWPopWin.prototype = {
  34. fInitialize:function(options) {
  35. /**
  36. * create window markup:
  37. *
  38. * overlay(bgObj) div
  39. * <div name='panel'>
  40. * <table>
  41. * <tr name='captiontr'>
  42. * <td name='titlecell'></td><td name='closecell'><span name='closeBtn'></span></td>
  43. * </tr>
  44. * <tr><td name='msgBox' colspan=2><div name='contentdiv'></div></td></tr>
  45. * </table>
  46. * </div>
  47. */
  48. var opt=options||{};
  49. this.options=opt;
  50. if(opt.canMove===undefined)opt.canMove=true;
  51. this._minWidth=20;
  52. this._minHeight=10;
  53. this._createMoveHandlers();
  54. var w=opt.width||400,h=opt.height||300;
  55. this.modal=opt.modal;
  56. var iWidth = document.documentElement.clientWidth;
  57. var iHeight = document.documentElement.clientHeight;
  58. var bgObj = document.createElement("div");
  59. bgObj.style.width = iWidth+"px";
  60. bgObj.style.height = Math.max(document.body.clientHeight, iHeight)+"px";
  61. bgObj.style.display="none";
  62. bgObj.className = "popwin_bg";
  63. document.body.appendChild(bgObj);
  64. this.overlay=bgObj;
  65. var msgObj=document.createElement("div");
  66. if(opt.center){
  67. msgObj.style.top = (iHeight-h)/2+"px";
  68. msgObj.style.left = (iWidth-w)/2+"px";
  69. }else{
  70. msgObj.style.top = (opt.top||"100")+"px";
  71. msgObj.style.left = (opt.left||"100")+"px";
  72. }
  73. msgObj.style.width = w+"px";
  74. msgObj.style.height = h+"px";
  75. msgObj.style.display="none";
  76. msgObj.className = "popwin";
  77. document.body.appendChild(msgObj);
  78. this.panel=msgObj;
  79. var table = document.createElement("table");
  80. this.panel.appendChild(table);
  81. var captiontr = table.insertRow(-1);
  82. var titlecell = captiontr.insertCell(-1);
  83. captiontr.className = "popwin_title_bg";
  84. titlecell.className = "popwin_title";
  85. this.captionbar=titlecell;
  86. this.setTitle(opt.title||"please set your title");
  87. if(opt.canMove){
  88. this.registerMove();
  89. }
  90. var closecell = captiontr.insertCell(-1);
  91. closeBtn = document.createElement("span");
  92. closeBtn.innerHTML="<span title='关闭'>×</span>"
  93. closecell.className = "popwin_close";
  94. closecell.appendChild(closeBtn);
  95. if(opt.closeAction=="hide"){
  96. this.options.closeAction="hide";
  97. closeBtn.onclick = this.dele(this.hide,this);
  98. }else{
  99. closeBtn.onclick = this.dele(this.close,this);
  100. this.options.closeAction="close";
  101. }
  102. this.closeButton=closeBtn;
  103. var msgBox = table.insertRow(-1).insertCell(-1);
  104. msgBox.className = "popwin_msg_box";
  105. msgBox.colSpan = "2";
  106. var contentdiv = document.createElement("div");
  107. contentdiv.style.overflow='auto';
  108. msgBox.appendChild(contentdiv);
  109. this.contentPanel=contentdiv;
  110. this.onresize();
  111. this.setContent(opt.content||"please set your content");
  112. },
  113. applyStyles:function (el,obj){
  114. for (name in obj){
  115. el.style[name]=obj[name];
  116. }
  117. },
  118. isNumber : function(v){
  119. return typeof v === 'number' && isFinite(v);
  120. },
  121. apply: function(o, c, defaults){
  122. if(defaults){
  123. apply(o, defaults);
  124. }
  125. if(o && c && typeof c == 'object'){
  126. for(var p in c){
  127. o[p] = c[p];
  128. }
  129. }
  130. return o;
  131. },
  132. getEvent : function() {
  133. return window.event || arguments.callee.caller.arguments[0];
  134. },
  135. dele: function (method,scope, args, appendArgs){
  136. var _this = this;
  137. return function() {
  138. var callArgs = args || arguments;
  139. if (appendArgs === true){
  140. callArgs = Array.prototype.slice.call(arguments, 0);
  141. callArgs = callArgs.concat(args);
  142. }else if (_this.isNumber(appendArgs)){
  143. callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
  144. var applyArgs = [appendArgs, 0].concat(args); // create method call params
  145. Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
  146. }
  147. return method.apply(scope || window, callArgs);
  148. };
  149. },
  150. _createMoveHandlers:function(){
  151. var moveData = {
  152. startX : 0,
  153. startY : 0,
  154. startTop : 0,
  155. startLeft : 0,
  156. moving : false,
  157. mousedown:0,
  158. docMouseMoveHandler : document.onmousemove,
  159. docMouseUpHandler : document.onmouseup,
  160. me:this
  161. };
  162. var _this = this;
  163. this.onCaptionMousedown=function() {
  164. var e = _this.getEvent();
  165. moveData.startX = e.clientX;
  166. moveData.startY = e.clientY;
  167. moveData.startTop = parseInt(moveData.me.panel.style.top);
  168. moveData.startLeft = parseInt(moveData.me.panel.style.left);
  169. if (moveData.me.options.glueMouse){
  170. if(moveData.mousedown==0){
  171. moveData.moving=true;
  172. document.onmousemove =moveData.me.onDocMousemove;
  173. moveData.mousedown=1;
  174. }else{
  175. document.onmousemove = moveData.docMouseMoveHandler;
  176. moveData.moving = false;
  177. moveData.mousedown=0;
  178. }
  179. }else{
  180. moveData.moving = true;
  181. document.onmousemove =moveData.me.onDocMousemove;
  182. document.onmouseup = moveData.me.onDocMouseup;
  183. }
  184. };
  185. this.onDocMousemove=function() {
  186. if (moveData.moving) {
  187. var e = _this.getEvent();
  188. var deltaX = moveData.startLeft + e.clientX - moveData.startX;
  189. var deltaY = moveData.startTop + e.clientY - moveData.startY;
  190. if ( deltaX > 0 &&( deltaX +2+ moveData.me.getWidth() < document.documentElement.clientWidth) &&
  191. deltaY > 0 && (deltaY +2+ moveData.me.getHeight() < document.documentElement.clientHeight) ) {
  192. moveData.me.panel.style.left = deltaX + "px";
  193. moveData.me.panel.style.top = deltaY + "px";
  194. }
  195. }
  196. };
  197. this.onDocMouseup=function () {
  198. if (moveData.moving) {
  199. document.onmousemove = moveData.docMouseMoveHandler;
  200. document.onmouseup = moveData.docMouseUpHandler;
  201. moveData.moving = false;
  202. }
  203. };
  204. },
  205. registerMove:function(){
  206. try{
  207. if(!this._movehandlerregistered){
  208. this.captionbar.onmousedown = this.onCaptionMousedown;
  209. this._movehandlerregistered=true;
  210. }}catch(err){alert(err)}
  211. },
  212. unRegisterMove:function(){
  213. if(this._movehandlerregistered){
  214. this.captionbar.onmousedown = undefined;
  215. this._movehandlerregistered=false;
  216. }
  217. },
  218. show:function(options){
  219. this._hideSelectTags();
  220. if(options){
  221. if(options.title)this.setTitle(options.title);
  222. if(options.content)this.setContent(options.content);
  223. if(this.isNumber(options.width))this.setWidth(options.width);
  224. if(this.isNumber(options.height))this.setHeight(options.height);
  225. if(options.center){
  226. this.center();
  227. }else{
  228. if(this.isNumber(options.top))this.setTop(options.top);
  229. if(this.isNumber(options.left))this.setLeft(options.left);
  230. }
  231. if(options.canMove!==undefined){
  232. if(options.canMove){
  233. this.registerMove();
  234. }else{
  235. this.unRegisterMove();
  236. }
  237. }
  238. if(options.closeAction&&options.closeAction!=this.options.closeAction){
  239. if(options.closeAction=="hide"){
  240. this.closeButton.onclick = this.dele(this.hide,this);
  241. }else{
  242. this.closeButton.onclick = this.dele(this.close,this);
  243. }
  244. this.options.closeAction=options.closeAction;
  245. }
  246. if(options.glueMouse!==undefined){
  247. this.options.glueMouse=options.glueMouse;
  248. }
  249. }
  250. var o=options||{};
  251. var modal=(o.modal===undefined?this.modal:o.modal);
  252. if (modal){
  253. this.overlay.style.display="";
  254. }else{
  255. this.overlay.style.display="none";
  256. }
  257. this.panel.style.display="";
  258. },
  259. hide:function(){
  260. this._showSelectTags();
  261. this.overlay.style.display="none";
  262. this.panel.style.display="none";
  263. },
  264. close:function(){
  265. this._showSelectTags();
  266. document.body.removeChild(this.overlay);
  267. document.body.removeChild(this.panel);
  268. },
  269. _hideSelectTags:function(){
  270. var sl = document.getElementsByTagName("select");
  271. this._selects=sl;
  272. for(var j=0;j<sl.length;j++){
  273. sl[j].style.display="none";
  274. }
  275. },
  276. _showSelectTags:function(){
  277. var sl=this._selects;
  278. if(sl){
  279. for(var j=0;j<sl.length;j++){
  280. sl[j].style.display="none";
  281. }
  282. }
  283. },
  284. setTitle:function(title){
  285. this.captionbar.innerHTML = title;
  286. },
  287. setContent:function(content){
  288. this.contentPanel.innerHTML =content;
  289. },
  290. setWidth:function(val){
  291. if(val>=this._minWidth){
  292. this.panel.style.width=val+"px";
  293. this.onresize();
  294. }
  295. },
  296. getWidth:function(){
  297. return parseInt(this.panel.style.width);
  298. },
  299. setHeight:function(val){
  300. if(val>=this._minHeight){
  301. this.panel.style.height=val+"px";
  302. this.onresize();
  303. }
  304. },
  305. getHeight:function(){
  306. return parseInt(this.panel.style.height);
  307. },
  308. setTop:function(val){
  309. if(val>=0&&val+10<= document.documentElement.clientHeight){
  310. this.panel.style.top=val+"px";
  311. }
  312. },
  313. getTop:function(){
  314. return parseInt(this.panel.style.top);
  315. },
  316. setLeft:function(val){
  317. if(val>=0&&val+10<=document.documentElement.clientWidth){
  318. this.panel.style.left=val+"px";
  319. }
  320. },
  321. getLeft:function(){
  322. return parseInt(this.panel.style.left);
  323. },
  324. center:function(){
  325. this.panel.style.top = (document.documentElement.clientHeight-this.getHeight())/2+"px";
  326. this.panel.style.left = (document.documentElement.clientWidth-this.getWidth())/2+"px";
  327. },
  328. onresize:function(){
  329. this.contentPanel.style.width=(this.getWidth()-1)+"px";
  330. this.contentPanel.style.height=(this.getHeight()-29)+"px";
  331. }
  332. };