| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- /**
- * weebox.js
- *
- * weebox js
- *
- * @category javascript
- * @package jquery
- * @author Jack <xiejinci@gmail.com>
- * @copyright Copyright (c) 2006-2008 9wee Com. (http://www.9wee.com)
- * @license http://www.9wee.com/license/
- * @version
- */
- (function($) {
- /*if(typeof($.fn.bgIframe) == 'undefined') {
- $.ajax({
- type: "GET",
- url: '/js/jquery/bgiframe.js',//路径不好处理
- success: function(js){eval(js);},
- async: false
- });
- }*/
- var weebox = function(content, options) {
- var self = this;
- this._dragging = false;
- this._content = content;
- this._options = options;
- this.dh = null;
- this.mh = null;
- this.dt = null;
- this.dc = null;
- this.bo = null;
- this.bc = null;
- this.selector = null;
- this.ajaxurl = null;
- this.options = null;
- this.defaults = {
- boxid: null,
- boxclass: null,
- type: 'dialog',
- title: '',
- width: 0,
- height: 0,
- timeout: 0,
- draggable: true,
- modal: true,
- focus: null,
- position: 'center',
- overlay: 75,
- showTitle: true,
- showButton: true,
- showCancel: true,
- showOk: true,
- okBtnName: '确定',
- cancelBtnName: '取消',
- contentType: 'text',
- contentChange: false,
- clickClose: false,
- zIndex: 999,
- animate: false,
- trigger: null,
- onclose: null,
- onopen: null,
- onok: null
- };
- this.types = new Array(
- "dialog",
- "error",
- "warning",
- "success",
- "prompt",
- "box"
- );
- this.titles = {
- "error": "!! Error !!",
- "warning": "Warning!",
- "success": "Success",
- "prompt": "Please Choose",
- "dialog": "Dialog",
- "box": ""
- };
- this.initOptions = function() {
- if (typeof(self._options) == "undefined") {
- self._options = {};
- }
- if (typeof(self._options.type) == "undefined") {
- self._options.type = 'dialog';
- }
- if(!$.inArray(self._options.type, self.types)) {
- self._options.type = self.types[0];
- }
- if (typeof(self._options.boxclass) == "undefined") {
- self._options.boxclass = self._options.type+"box";
- }
- if (typeof(self._options.title) == "undefined") {
- self._options.title = self.titles[self._options.type];
- }
- if (content.substr(0, 1) == "#") {
- self._options.contentType = 'selector';
- self.selector = content;
- }
- self.options = $.extend({}, self.defaults, self._options);
- };
- this.initBox = function() {
- var html = '';
- if (self.options.type == 'wee') {
- html = '<div class="weedialog">' +
- ' <div class="dialog-header">' +
- ' <div class="dialog-tl"></div>' +
- ' <div class="dialog-title"></div>' +
- ' <div class="dialog-tr">' +
- ' <div class="dialog-close" title="关闭"><a href="#"></a></div>' +
- ' </div>' +
- ' </div>' +
- ' <div class="dialog-con">' +
- ' <div class="dialog-con2">' +
- ' <div class="dialog-content"></div>' +
- ' <div class="dialog-button">' +
- ' <input type="button" class="dialog-ok" value="确认" /> ' +
- ' <input type="button" class="dialog-cancel" value="取消" />' +
- ' </div>' +
- ' </div>' +
- ' </div>' +
- ' <div class="dialog-bot">' +
- ' <div class="dialog-bl"></div>' +
- ' <div class="dialog-bc"></div>' +
- ' <div class="dialog-br"></div>' +
- ' </div>' +
- '</div>';
- } else {
- html = "<div class='dialog-box'>" +
- "<div class='dialog-header'>" +
- "<div class='dialog-title'></div>" +
- "<div class='dialog-close'></div>" +
- "</div>" +
- "<div class='dialog-content'></div>" +
- "<div style='clear:both'></div>" +
- "<div class='dialog-button'>" +
- "<input type='button' class='dialog-ok' value='确定'>" +
- "<input type='button' class='dialog-cancel' value='取消'>" +
- "</div>" +
- "</div>";
- }
- self.dh = $(html).appendTo('body').hide().css({
- position: 'absolute',
- overflow: 'hidden',
- zIndex: self.options.zIndex
- });
- self.dt = self.dh.find('.dialog-title');
- self.dc = self.dh.find('.dialog-content');
- self.bo = self.dh.find('.dialog-ok');
- self.bc = self.dh.find('.dialog-cancel');
- if (self.options.boxid) {
- self.dh.attr('id', self.options.boxid);
- }
- if (self.options.boxclass) {
- self.dh.addClass(self.options.boxclass);
- }
- if (self.options.height>0) {
- self.dc.css('height', self.options.height);
- }
- if (self.options.width>0) {
- self.dh.css('width', self.options.width);
- }
- self.dh.bgiframe();
- }
- this.initMask = function() {
- if (self.options.modal) {
- self.mh = $("<div class='dialog-mask'></div>")
- .appendTo('body').hide().css({
- opacity: self.options.overlay/100,
- filter: 'alpha(opacity='+self.options.overlay+')',
- width: self.bwidth(),
- height: self.bheight(),
- zIndex: self.options.zIndex-1
- });
- }
- }
- this.initContent = function(content) {
- self.dh.find(".dialog-ok").val(self.options.okBtnName);
- self.dh.find(".dialog-cancel").val(self.options.cancelBtnName);
- self.dh.find('.dialog-title').html(self.options.title);
- if (!self.options.showTitle) {
- self.dh.find('.dialog-header').hide();
- }
- if (!self.options.showButton) {
- self.dh.find('.dialog-button').hide();
- }
- if (!self.options.showCancel) {
- self.dh.find('.dialog-cancel').hide();
- }
- if (!self.options.showOk) {
- self.dh.find(".dialog-ok").hide();
- }
- if (self.options.contentType == "selector") {
- self.selector = self._content;
- self._content = $(self.selector).html();
- self.setContent(self._content);
- //if have checkbox do
- var cs = $(self.selector).find(':checkbox');
- self.dh.find('.dialog-content').find(':checkbox').each(function(i){
- this.checked = cs[i].checked;
- });
- $(self.selector).empty();
- self.onopen();
- self.show();
- self.focus();
- } else if (self.options.contentType == "ajax") {
- self.ajaxurl = self._content;
- self.setContent('<div class="dialog-loading"></div>');
- self.show();
- $.get(self.ajaxurl, function(data) {
- self._content = data;
- self.setContent(self._content);
- self.onopen();
- self.focus();
- });
- } else {
- self.setContent(self._content);
- self.onopen();
- self.show();
- self.focus();
- }
- }
- this.initEvent = function() {
- self.dh.find(".dialog-close, .dialog-cancel, .dialog-ok").unbind('click').click(function(){self.close();});
- if (typeof(self.options.onok) == "function") {
- self.dh.find(".dialog-ok").unbind('click').click(self.options.onok);
- }
- if (typeof(self.options.oncancel) == "function") {
- self.dh.find(".dialog-cancel").unbind('click').click(self.options.oncancel);
- }
- if (self.options.timeout>0) {
- window.setTimeout(self.close, (self.options.timeout * 1000));
- }
- this.draggable();
- }
- this.draggable = function() {
- if (self.options.draggable && self.options.showTitle) {
- self.dh.find('.dialog-header').mousedown(function(event){
- self._ox = self.dh.position().left;
- self._oy = self.dh.position().top;
- self._mx = event.clientX;
- self._my = event.clientY;
- self._dragging = true;
- });
- if (self.mh) {
- var handle = self.mh;
- } else {
- var handle = $(document);
- }
- $(document).mousemove(function(event){
- if (self._dragging == true) {
- //window.status = "X:"+event.clientX+"Y:"+event.clientY;
- self.dh.css({
- left: self._ox+event.clientX-self._mx,
- top: self._oy+event.clientY-self._my
- });
- }
- }).mouseup(function(){
- self._mx = null;
- self._my = null;
- self._dragging = false;
- });
- var e = self.dh.find('.dialog-header').get(0);
- e.unselectable = "on";
- e.onselectstart = function() {
- return false;
- };
- if (e.style) {
- e.style.MozUserSelect = "none";
- }
- }
- }
- this.onopen = function() {
- if (typeof(self.options.onopen) == "function") {
- self.options.onopen();
- }
- }
- this.show = function() {
- if (self.options.position == 'center') {
- self.setCenterPosition();
- }
- if (self.options.position == 'element') {
- self.setElementPosition();
- }
- if (self.options.animate) {
- self.dh.fadeIn("slow");
- if (self.mh) {
- self.mh.fadeIn("normal");
- }
- } else {
- self.dh.show();
- if (self.mh) {
- self.mh.show();
- }
- }
- }
- this.focus = function() {
- if (self.options.focus) {
- self.dh.find(self.options.focus).focus();
- } else {
- self.dh.find('.dialog-cancel').focus();
- }
- }
- this.find = function(selector) {
- return self.dh.find(selector);
- }
- this.setTitle = function(title) {
- self.dh.find('.dialog-title').html(title);
- }
- this.getTitle = function() {
- return self.dh.find('.dialog-title').html();
- }
- this.setContent = function(content) {
- self.dh.find('.dialog-content').html(content);
- }
- this.getContent = function() {
- return self.dh.find('.dialog-content').html();
- }
- this.hideButton = function(btname) {
- self.dh.find('.dialog-'+btname).hide();
- }
- this.showButton = function(btname) {
- self.dh.find('.dialog-'+btname).show();
- }
- this.setButtonTitle = function(btname, title) {
- self.dh.find('.dialog-'+btname).val(title);
- }
- this.close = function() {
- if (self.animate) {
- self.dh.fadeOut("slow", function () { self.dh.hide(); });
- if (self.mh) {
- self.mh.fadeOut("normal", function () { self.mh.hide(); });
- }
- } else {
- self.dh.hide();
- if (self.mh) {
- self.mh.hide();
- }
- }
- if (self.options.contentType == 'selector') {
- if (self.options.contentChange) {
- //if have checkbox do
- var cs = self.find(':checkbox');
- $(self.selector).html(self.getContent());
- if (cs.length > 0) {
- $(self.selector).find(':checkbox').each(function(i){
- this.checked = cs[i].checked;
- });
- }
- } else {
- $(self.selector).html(self._content);
- }
- }
- if (typeof(self.options.onclose) == "function") {
- self.options.onclose();
- }
- self.dh.remove();
- if (self.mh) {
- self.mh.remove();
- }
- }
- this.bheight = function() {
- if ($.browser.msie && $.browser.version < 7) {
- var scrollHeight = Math.max(
- document.documentElement.scrollHeight,
- document.body.scrollHeight
- );
- var offsetHeight = Math.max(
- document.documentElement.offsetHeight,
- document.body.offsetHeight
- );
- if (scrollHeight < offsetHeight) {
- return $(window).height();
- } else {
- return scrollHeight;
- }
- } else {
- return $(document).height();
- }
- }
- this.bwidth = function() {
- if ($.browser.msie && $.browser.version < 7) {
- var scrollWidth = Math.max(
- document.documentElement.scrollWidth,
- document.body.scrollWidth
- );
- var offsetWidth = Math.max(
- document.documentElement.offsetWidth,
- document.body.offsetWidth
- );
- if (scrollWidth < offsetWidth) {
- return $(window).width();
- } else {
- return scrollWidth;
- }
- } else {
- return $(document).width();
- }
- }
- this.setCenterPosition = function() {
- var wnd = $(window), doc = $(document),
- pTop = doc.scrollTop(), pLeft = doc.scrollLeft(),
- minTop = pTop;
- pTop += (wnd.height() - self.dh.height()) / 2;
- pTop = Math.max(pTop, minTop);
- pLeft += (wnd.width() - self.dh.width()) / 2;
- self.dh.css({top: pTop, left: pLeft});
- }
- this.setElementPosition = function() {
- var trigger = $("#"+self.options.trigger);
- if (trigger.length == 0) {
- alert('请设置位置的相对元素');
- self.close();
- return false;
- }
- var scrollWidth = 0;
- if (!$.browser.msie || $.browser.version >= 7) {
- scrollWidth = $(window).width() - document.body.scrollWidth;
- }
- var left = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft)+trigger.position().left;
- if (left+self.dh.width() > document.body.clientWidth) {
- left = trigger.position().left + trigger.width() + scrollWidth - self.dh.width();
- }
- var top = Math.max(document.documentElement.scrollTop, document.body.scrollTop)+trigger.position().top;
- if (top+self.dh.height()+trigger.height() > document.documentElement.clientHeight) {
- top = top - self.dh.height() - 5;
- } else {
- top = top + trigger.height() + 5;
- }
- self.dh.css({top: top, left: left});
- return true;
- }
- //PNG透明图片
- this.correctPNG = function() {
- for(var i=0; i<document.images.length; i++) {
- var img = document.images[i]
- var imgName = img.src.toUpperCase()
- if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
- var imgID = (img.id) ? "id='" + img.id + "' " : ""
- var imgClass = (img.className) ? "class='" + img.className + "' " : ""
- var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
- var imgStyle = "display:inline-block;" + img.style.cssText
- if (img.align == "left") imgStyle = "float:left;" + imgStyle
- if (img.align == "right") imgStyle = "float:right;" + imgStyle
- if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
- 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>"
- img.outerHTML = strNewHTML;
- i = i-1;
- }
- }
- }
- //窗口初始化
- this.initialize = function() {
- self.initOptions();
- self.initMask();
- self.initBox();
- self.initContent();
- self.initEvent();
- //self.correctPNG();
- return self;
- }
- //初始化
- this.initialize();
- }
- var weeboxs = function() {
- var self = this;
- this._onbox = false;
- this._opening = false;
- this.boxs = new Array();
- this.zIndex = 999;
- this.push = function(box) {
- this.boxs.push(box);
- }
- this.pop = function() {
- if (this.boxs.length > 0) {
- return this.boxs.pop();
- } else {
- return false;
- }
- }
- this.open = function(content, options) {
- self._opening = true;
- if (typeof(options) == "undefined") {
- options = {};
- }
- if (options.boxid) {
- this.close(options.boxid);
- }
- options.zIndex = this.zIndex;
- this.zIndex += 10;
- var box = new weebox(content, options);
- box.dh.click(function(){
- self._onbox = true;
- });
- this.push(box);
- return box;
- }
- this.close = function(id) {
- if (id) {
- for(var i=0; i<this.boxs.length; i++) {
- if (this.boxs[i].dh.attr('id') == id) {
- this.boxs[i].close();
- this.boxs.splice(i,1);
- }
- }
- } else {
- this.pop().close();
- }
- }
- this.length = function() {
- return this.boxs.length;
- }
- this.getTopBox = function() {
- return this.boxs[this.boxs.length-1];
- }
- this.find = function(selector) {
- return this.getTopBox().dh.find(selector);
- }
- this.setTitle = function(title) {
- this.getTopBox().setTitle(title);
- }
- this.getTitle = function() {
- return this.getTopBox().getTitle();
- }
- this.setContent = function(content) {
- this.getTopBox().setContent(content);
- }
- this.getContent = function() {
- return this.getTopBox().getContent();
- }
- this.hideButton = function(btname) {
- this.getTopBox().hideButton(btname);
- }
- this.showButton = function(btname) {
- this.getTopBox().showButton(btname);
- }
- this.setButtonTitle = function(btname, title) {
- this.getTopBox().setButtonTitle(btname, title);
- }
- $(window).scroll(function() {
- if (self.length() > 0) {
- var box = self.getTopBox();
- if (box.options.position == "center") {
- self.getTopBox().setCenterPosition();
- }
- }
- });
- $(document).click(function() {
- if (self.length()>0) {
- var box = self.getTopBox();
- if(!self._opening && !self._onbox && box.options.clickClose) {
- box.close();
- }
- }
- self._opening = false;
- self._onbox = false;
- });
- }
- $.extend({weeboxs: new weeboxs()});
- })(jQuery);
|