jquery.weebox.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. * weebox.js
  3. *
  4. * weebox js
  5. *
  6. * @category javascript
  7. * @package jquery
  8. * @author Jack <xiejinci@gmail.com>
  9. * @copyright Copyright (c) 2006-2008 9wee Com. (http://www.9wee.com)
  10. * @license http://www.9wee.com/license/
  11. * @version
  12. */
  13. (function($) {
  14. /*if(typeof($.fn.bgIframe) == 'undefined') {
  15. $.ajax({
  16. type: "GET",
  17. url: '/js/jquery/bgiframe.js',//路径不好处理
  18. success: function(js){eval(js);},
  19. async: false
  20. });
  21. }*/
  22. var weebox = function(content, options) {
  23. var self = this;
  24. this._dragging = false;
  25. this._content = content;
  26. this._options = options;
  27. this.dh = null;
  28. this.mh = null;
  29. this.dt = null;
  30. this.dc = null;
  31. this.bo = null;
  32. this.bc = null;
  33. this.selector = null;
  34. this.ajaxurl = null;
  35. this.options = null;
  36. this.defaults = {
  37. boxid: null,
  38. boxclass: null,
  39. type: 'dialog',
  40. title: '',
  41. width: 0,
  42. height: 0,
  43. timeout: 0,
  44. draggable: true,
  45. modal: true,
  46. focus: null,
  47. position: 'center',
  48. overlay: 75,
  49. showTitle: true,
  50. showButton: true,
  51. showCancel: true,
  52. showOk: true,
  53. okBtnName: '确定',
  54. cancelBtnName: '取消',
  55. contentType: 'text',
  56. contentChange: false,
  57. clickClose: false,
  58. zIndex: 999,
  59. animate: false,
  60. trigger: null,
  61. onclose: null,
  62. onopen: null,
  63. onok: null
  64. };
  65. this.types = new Array(
  66. "dialog",
  67. "error",
  68. "warning",
  69. "success",
  70. "prompt",
  71. "box"
  72. );
  73. this.titles = {
  74. "error": "!! Error !!",
  75. "warning": "Warning!",
  76. "success": "Success",
  77. "prompt": "Please Choose",
  78. "dialog": "Dialog",
  79. "box": ""
  80. };
  81. this.initOptions = function() {
  82. if (typeof(self._options) == "undefined") {
  83. self._options = {};
  84. }
  85. if (typeof(self._options.type) == "undefined") {
  86. self._options.type = 'dialog';
  87. }
  88. if(!$.inArray(self._options.type, self.types)) {
  89. self._options.type = self.types[0];
  90. }
  91. if (typeof(self._options.boxclass) == "undefined") {
  92. self._options.boxclass = self._options.type+"box";
  93. }
  94. if (typeof(self._options.title) == "undefined") {
  95. self._options.title = self.titles[self._options.type];
  96. }
  97. if (content.substr(0, 1) == "#") {
  98. self._options.contentType = 'selector';
  99. self.selector = content;
  100. }
  101. self.options = $.extend({}, self.defaults, self._options);
  102. };
  103. this.initBox = function() {
  104. var html = '';
  105. if (self.options.type == 'wee') {
  106. html = '<div class="weedialog">' +
  107. ' <div class="dialog-header">' +
  108. ' <div class="dialog-tl"></div>' +
  109. ' <div class="dialog-title"></div>' +
  110. ' <div class="dialog-tr">' +
  111. ' <div class="dialog-close" title="关闭"><a href="#"></a></div>' +
  112. ' </div>' +
  113. ' </div>' +
  114. ' <div class="dialog-con">' +
  115. ' <div class="dialog-con2">' +
  116. ' <div class="dialog-content"></div>' +
  117. ' <div class="dialog-button">' +
  118. ' <input type="button" class="dialog-ok" value="确认" />&nbsp;' +
  119. ' <input type="button" class="dialog-cancel" value="取消" />' +
  120. ' </div>' +
  121. ' </div>' +
  122. ' </div>' +
  123. ' <div class="dialog-bot">' +
  124. ' <div class="dialog-bl"></div>' +
  125. ' <div class="dialog-bc"></div>' +
  126. ' <div class="dialog-br"></div>' +
  127. ' </div>' +
  128. '</div>';
  129. } else {
  130. html = "<div class='dialog-box'>" +
  131. "<div class='dialog-header'>" +
  132. "<div class='dialog-title'></div>" +
  133. "<div class='dialog-close'></div>" +
  134. "</div>" +
  135. "<div class='dialog-content'></div>" +
  136. "<div style='clear:both'></div>" +
  137. "<div class='dialog-button'>" +
  138. "<input type='button' class='dialog-ok' value='确定'>" +
  139. "<input type='button' class='dialog-cancel' value='取消'>" +
  140. "</div>" +
  141. "</div>";
  142. }
  143. self.dh = $(html).appendTo('body').hide().css({
  144. position: 'absolute',
  145. overflow: 'hidden',
  146. zIndex: self.options.zIndex
  147. });
  148. self.dt = self.dh.find('.dialog-title');
  149. self.dc = self.dh.find('.dialog-content');
  150. self.bo = self.dh.find('.dialog-ok');
  151. self.bc = self.dh.find('.dialog-cancel');
  152. if (self.options.boxid) {
  153. self.dh.attr('id', self.options.boxid);
  154. }
  155. if (self.options.boxclass) {
  156. self.dh.addClass(self.options.boxclass);
  157. }
  158. if (self.options.height>0) {
  159. self.dc.css('height', self.options.height);
  160. }
  161. if (self.options.width>0) {
  162. self.dh.css('width', self.options.width);
  163. }
  164. self.dh.bgiframe();
  165. }
  166. this.initMask = function() {
  167. if (self.options.modal) {
  168. self.mh = $("<div class='dialog-mask'></div>")
  169. .appendTo('body').hide().css({
  170. opacity: self.options.overlay/100,
  171. filter: 'alpha(opacity='+self.options.overlay+')',
  172. width: self.bwidth(),
  173. height: self.bheight(),
  174. zIndex: self.options.zIndex-1
  175. });
  176. }
  177. }
  178. this.initContent = function(content) {
  179. self.dh.find(".dialog-ok").val(self.options.okBtnName);
  180. self.dh.find(".dialog-cancel").val(self.options.cancelBtnName);
  181. self.dh.find('.dialog-title').html(self.options.title);
  182. if (!self.options.showTitle) {
  183. self.dh.find('.dialog-header').hide();
  184. }
  185. if (!self.options.showButton) {
  186. self.dh.find('.dialog-button').hide();
  187. }
  188. if (!self.options.showCancel) {
  189. self.dh.find('.dialog-cancel').hide();
  190. }
  191. if (!self.options.showOk) {
  192. self.dh.find(".dialog-ok").hide();
  193. }
  194. if (self.options.contentType == "selector") {
  195. self.selector = self._content;
  196. self._content = $(self.selector).html();
  197. self.setContent(self._content);
  198. //if have checkbox do
  199. var cs = $(self.selector).find(':checkbox');
  200. self.dh.find('.dialog-content').find(':checkbox').each(function(i){
  201. this.checked = cs[i].checked;
  202. });
  203. $(self.selector).empty();
  204. self.onopen();
  205. self.show();
  206. self.focus();
  207. } else if (self.options.contentType == "ajax") {
  208. self.ajaxurl = self._content;
  209. self.setContent('<div class="dialog-loading"></div>');
  210. self.show();
  211. $.get(self.ajaxurl, function(data) {
  212. self._content = data;
  213. self.setContent(self._content);
  214. self.onopen();
  215. self.focus();
  216. });
  217. } else {
  218. self.setContent(self._content);
  219. self.onopen();
  220. self.show();
  221. self.focus();
  222. }
  223. }
  224. this.initEvent = function() {
  225. self.dh.find(".dialog-close, .dialog-cancel, .dialog-ok").unbind('click').click(function(){self.close();});
  226. if (typeof(self.options.onok) == "function") {
  227. self.dh.find(".dialog-ok").unbind('click').click(self.options.onok);
  228. }
  229. if (typeof(self.options.oncancel) == "function") {
  230. self.dh.find(".dialog-cancel").unbind('click').click(self.options.oncancel);
  231. }
  232. if (self.options.timeout>0) {
  233. window.setTimeout(self.close, (self.options.timeout * 1000));
  234. }
  235. this.draggable();
  236. }
  237. this.draggable = function() {
  238. if (self.options.draggable && self.options.showTitle) {
  239. self.dh.find('.dialog-header').mousedown(function(event){
  240. self._ox = self.dh.position().left;
  241. self._oy = self.dh.position().top;
  242. self._mx = event.clientX;
  243. self._my = event.clientY;
  244. self._dragging = true;
  245. });
  246. if (self.mh) {
  247. var handle = self.mh;
  248. } else {
  249. var handle = $(document);
  250. }
  251. $(document).mousemove(function(event){
  252. if (self._dragging == true) {
  253. //window.status = "X:"+event.clientX+"Y:"+event.clientY;
  254. self.dh.css({
  255. left: self._ox+event.clientX-self._mx,
  256. top: self._oy+event.clientY-self._my
  257. });
  258. }
  259. }).mouseup(function(){
  260. self._mx = null;
  261. self._my = null;
  262. self._dragging = false;
  263. });
  264. var e = self.dh.find('.dialog-header').get(0);
  265. e.unselectable = "on";
  266. e.onselectstart = function() {
  267. return false;
  268. };
  269. if (e.style) {
  270. e.style.MozUserSelect = "none";
  271. }
  272. }
  273. }
  274. this.onopen = function() {
  275. if (typeof(self.options.onopen) == "function") {
  276. self.options.onopen();
  277. }
  278. }
  279. this.show = function() {
  280. if (self.options.position == 'center') {
  281. self.setCenterPosition();
  282. }
  283. if (self.options.position == 'element') {
  284. self.setElementPosition();
  285. }
  286. if (self.options.animate) {
  287. self.dh.fadeIn("slow");
  288. if (self.mh) {
  289. self.mh.fadeIn("normal");
  290. }
  291. } else {
  292. self.dh.show();
  293. if (self.mh) {
  294. self.mh.show();
  295. }
  296. }
  297. }
  298. this.focus = function() {
  299. if (self.options.focus) {
  300. self.dh.find(self.options.focus).focus();
  301. } else {
  302. self.dh.find('.dialog-cancel').focus();
  303. }
  304. }
  305. this.find = function(selector) {
  306. return self.dh.find(selector);
  307. }
  308. this.setTitle = function(title) {
  309. self.dh.find('.dialog-title').html(title);
  310. }
  311. this.getTitle = function() {
  312. return self.dh.find('.dialog-title').html();
  313. }
  314. this.setContent = function(content) {
  315. self.dh.find('.dialog-content').html(content);
  316. }
  317. this.getContent = function() {
  318. return self.dh.find('.dialog-content').html();
  319. }
  320. this.hideButton = function(btname) {
  321. self.dh.find('.dialog-'+btname).hide();
  322. }
  323. this.showButton = function(btname) {
  324. self.dh.find('.dialog-'+btname).show();
  325. }
  326. this.setButtonTitle = function(btname, title) {
  327. self.dh.find('.dialog-'+btname).val(title);
  328. }
  329. this.close = function() {
  330. if (self.animate) {
  331. self.dh.fadeOut("slow", function () { self.dh.hide(); });
  332. if (self.mh) {
  333. self.mh.fadeOut("normal", function () { self.mh.hide(); });
  334. }
  335. } else {
  336. self.dh.hide();
  337. if (self.mh) {
  338. self.mh.hide();
  339. }
  340. }
  341. if (self.options.contentType == 'selector') {
  342. if (self.options.contentChange) {
  343. //if have checkbox do
  344. var cs = self.find(':checkbox');
  345. $(self.selector).html(self.getContent());
  346. if (cs.length > 0) {
  347. $(self.selector).find(':checkbox').each(function(i){
  348. this.checked = cs[i].checked;
  349. });
  350. }
  351. } else {
  352. $(self.selector).html(self._content);
  353. }
  354. }
  355. if (typeof(self.options.onclose) == "function") {
  356. self.options.onclose();
  357. }
  358. self.dh.remove();
  359. if (self.mh) {
  360. self.mh.remove();
  361. }
  362. }
  363. this.bheight = function() {
  364. if ($.browser.msie && $.browser.version < 7) {
  365. var scrollHeight = Math.max(
  366. document.documentElement.scrollHeight,
  367. document.body.scrollHeight
  368. );
  369. var offsetHeight = Math.max(
  370. document.documentElement.offsetHeight,
  371. document.body.offsetHeight
  372. );
  373. if (scrollHeight < offsetHeight) {
  374. return $(window).height();
  375. } else {
  376. return scrollHeight;
  377. }
  378. } else {
  379. return $(document).height();
  380. }
  381. }
  382. this.bwidth = function() {
  383. if ($.browser.msie && $.browser.version < 7) {
  384. var scrollWidth = Math.max(
  385. document.documentElement.scrollWidth,
  386. document.body.scrollWidth
  387. );
  388. var offsetWidth = Math.max(
  389. document.documentElement.offsetWidth,
  390. document.body.offsetWidth
  391. );
  392. if (scrollWidth < offsetWidth) {
  393. return $(window).width();
  394. } else {
  395. return scrollWidth;
  396. }
  397. } else {
  398. return $(document).width();
  399. }
  400. }
  401. this.setCenterPosition = function() {
  402. var wnd = $(window), doc = $(document),
  403. pTop = doc.scrollTop(), pLeft = doc.scrollLeft(),
  404. minTop = pTop;
  405. pTop += (wnd.height() - self.dh.height()) / 2;
  406. pTop = Math.max(pTop, minTop);
  407. pLeft += (wnd.width() - self.dh.width()) / 2;
  408. self.dh.css({top: pTop, left: pLeft});
  409. }
  410. this.setElementPosition = function() {
  411. var trigger = $("#"+self.options.trigger);
  412. if (trigger.length == 0) {
  413. alert('请设置位置的相对元素');
  414. self.close();
  415. return false;
  416. }
  417. var scrollWidth = 0;
  418. if (!$.browser.msie || $.browser.version >= 7) {
  419. scrollWidth = $(window).width() - document.body.scrollWidth;
  420. }
  421. var left = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft)+trigger.position().left;
  422. if (left+self.dh.width() > document.body.clientWidth) {
  423. left = trigger.position().left + trigger.width() + scrollWidth - self.dh.width();
  424. }
  425. var top = Math.max(document.documentElement.scrollTop, document.body.scrollTop)+trigger.position().top;
  426. if (top+self.dh.height()+trigger.height() > document.documentElement.clientHeight) {
  427. top = top - self.dh.height() - 5;
  428. } else {
  429. top = top + trigger.height() + 5;
  430. }
  431. self.dh.css({top: top, left: left});
  432. return true;
  433. }
  434. //PNG透明图片
  435. this.correctPNG = function() {
  436. for(var i=0; i<document.images.length; i++) {
  437. var img = document.images[i]
  438. var imgName = img.src.toUpperCase()
  439. if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
  440. var imgID = (img.id) ? "id='" + img.id + "' " : ""
  441. var imgClass = (img.className) ? "class='" + img.className + "' " : ""
  442. var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
  443. var imgStyle = "display:inline-block;" + img.style.cssText
  444. if (img.align == "left") imgStyle = "float:left;" + imgStyle
  445. if (img.align == "right") imgStyle = "float:right;" + imgStyle
  446. if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
  447. var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
  448. img.outerHTML = strNewHTML;
  449. i = i-1;
  450. }
  451. }
  452. }
  453. //窗口初始化
  454. this.initialize = function() {
  455. self.initOptions();
  456. self.initMask();
  457. self.initBox();
  458. self.initContent();
  459. self.initEvent();
  460. //self.correctPNG();
  461. return self;
  462. }
  463. //初始化
  464. this.initialize();
  465. }
  466. var weeboxs = function() {
  467. var self = this;
  468. this._onbox = false;
  469. this._opening = false;
  470. this.boxs = new Array();
  471. this.zIndex = 999;
  472. this.push = function(box) {
  473. this.boxs.push(box);
  474. }
  475. this.pop = function() {
  476. if (this.boxs.length > 0) {
  477. return this.boxs.pop();
  478. } else {
  479. return false;
  480. }
  481. }
  482. this.open = function(content, options) {
  483. self._opening = true;
  484. if (typeof(options) == "undefined") {
  485. options = {};
  486. }
  487. if (options.boxid) {
  488. this.close(options.boxid);
  489. }
  490. options.zIndex = this.zIndex;
  491. this.zIndex += 10;
  492. var box = new weebox(content, options);
  493. box.dh.click(function(){
  494. self._onbox = true;
  495. });
  496. this.push(box);
  497. return box;
  498. }
  499. this.close = function(id) {
  500. if (id) {
  501. for(var i=0; i<this.boxs.length; i++) {
  502. if (this.boxs[i].dh.attr('id') == id) {
  503. this.boxs[i].close();
  504. this.boxs.splice(i,1);
  505. }
  506. }
  507. } else {
  508. this.pop().close();
  509. }
  510. }
  511. this.length = function() {
  512. return this.boxs.length;
  513. }
  514. this.getTopBox = function() {
  515. return this.boxs[this.boxs.length-1];
  516. }
  517. this.find = function(selector) {
  518. return this.getTopBox().dh.find(selector);
  519. }
  520. this.setTitle = function(title) {
  521. this.getTopBox().setTitle(title);
  522. }
  523. this.getTitle = function() {
  524. return this.getTopBox().getTitle();
  525. }
  526. this.setContent = function(content) {
  527. this.getTopBox().setContent(content);
  528. }
  529. this.getContent = function() {
  530. return this.getTopBox().getContent();
  531. }
  532. this.hideButton = function(btname) {
  533. this.getTopBox().hideButton(btname);
  534. }
  535. this.showButton = function(btname) {
  536. this.getTopBox().showButton(btname);
  537. }
  538. this.setButtonTitle = function(btname, title) {
  539. this.getTopBox().setButtonTitle(btname, title);
  540. }
  541. $(window).scroll(function() {
  542. if (self.length() > 0) {
  543. var box = self.getTopBox();
  544. if (box.options.position == "center") {
  545. self.getTopBox().setCenterPosition();
  546. }
  547. }
  548. });
  549. $(document).click(function() {
  550. if (self.length()>0) {
  551. var box = self.getTopBox();
  552. if(!self._opening && !self._onbox && box.options.clickClose) {
  553. box.close();
  554. }
  555. }
  556. self._opening = false;
  557. self._onbox = false;
  558. });
  559. }
  560. $.extend({weeboxs: new weeboxs()});
  561. })(jQuery);