/*! * artdialog 5 * date: 2012-03-21 * http://code.google.com/p/artdialog/ * (c) 2009-2012 tangbin, http://www.planeart.cn * * this is licensed under the gnu lgpl, version 2.1 or later. * for details, see: http://creativecommons.org/licenses/lgpl/2.1/ */ ; (function (window, undefined) { var $ = window.art = function (selector, context) { return new $.fn.constructor(selector, context); }, quickexpr = /^(?:[^<]*(<[\w\w]+>)[^>]*$|#([\w\-]+)$)/, rclass = /[\n\t]/g; if (window.$ === undefined) { window.$ = $; }; $.fn = $.prototype = { constructor: function (selector, context) { var match, elem; context = context || document; if (!selector) { return this; }; if (selector.nodetype) { this[0] = selector; return this; }; if (typeof selector === 'string') { match = quickexpr.exec(selector); if (match && match[2]) { elem = context.getelementbyid(match[2]); if (elem && elem.parentnode) this[0] = elem; return this; }; }; this[0] = selector; return this; }, /** * 判断样式类是否存在 * @param {string} 名称 * @return {boolean} */ hasclass: function (name) { var classname = ' ' + name + ' '; if ((' ' + this[0].classname + ' ').replace(rclass, ' ').indexof(classname) > -1) { return true; }; return false; }, /** * 添加样式类 * @param {string} 名称 */ addclass: function (name) { if (!this.hasclass(name)) { this[0].classname += ' ' + name; }; return this; }, /** * 移除样式类 * @param {string} 名称 */ removeclass: function (name) { var elem = this[0]; if (!name) { elem.classname = ''; } else if (this.hasclass(name)) { elem.classname = elem.classname.replace(name, ' '); }; return this; }, /** * 读写样式
* css(name) 访问第一个匹配元素的样式属性
* css(properties) 把一个"名/值对"对象设置为所有匹配元素的样式属性
* css(name, value) 在所有匹配的元素中,设置一个样式属性的值
*/ css: function (name, value) { var i, elem = this[0], obj = arguments[0]; if (typeof name === 'string') { if (value === undefined) { return $.css(elem, name); } else { elem.style[name] = value; }; } else { for (i in obj) { elem.style[i] = obj[i]; }; }; return this; }, /** 显示元素 */ show: function () { return this.css('display', 'block'); }, /** 隐藏元素 */ hide: function () { return this.css('display', 'none'); }, /** * 获取相对文档的坐标 * @return {object} 返回left、top的数值 */ offset: function () { var elem = this[0], box = elem.getboundingclientrect(), doc = elem.ownerdocument, body = doc.body, docelem = doc.documentelement, clienttop = docelem.clienttop || body.clienttop || 0, clientleft = docelem.clientleft || body.clientleft || 0, top = box.top + (self.pageyoffset || docelem.scrolltop) - clienttop, left = box.left + (self.pagexoffset || docelem.scrollleft) - clientleft; return { left: left, top: top }; }, /** * 读写html - (不支持文本框) * @param {string} 内容 */ html: function (content) { var elem = this[0]; if (content === undefined) return elem.innerhtml; $.cleandata(elem.getelementsbytagname('*')); elem.innerhtml = content; return this; }, /** * 移除节点 */ remove: function () { var elem = this[0]; $.cleandata(elem.getelementsbytagname('*')); $.cleandata([elem]); elem.parentnode.removechild(elem); return this; }, /** * 事件绑定 * @param {string} 类型 * @param {function} 要绑定的函数 */ bind: function (type, callback) { $.event.add(this[0], type, callback); return this; }, /** * 移除事件 * @param {string} 类型 * @param {function} 要卸载的函数 */ unbind: function (type, callback) { $.event.remove(this[0], type, callback); return this; } }; $.fn.constructor.prototype = $.fn; /** 检测window */ $.iswindow = function (obj) { return obj && typeof obj === 'object' && 'setinterval' in obj; }; /** * 搜索子元素 * 注意:只支持nodename或.classname的形式,并且只返回第一个元素 * @param {string} */ $.fn.find = function (expr) { var value, elem = this[0], classname = expr.split('.')[1]; if (classname) { if (document.getelementsbyclassname) { value = elem.getelementsbyclassname(classname); } else { value = getelementsbyclassname(classname, elem); }; } else { value = elem.getelementsbytagname(expr); }; return $(value[0]); }; function getelementsbyclassname(classname, node, tag) { node = node || document; tag = tag || '*'; var i = 0, j = 0, classelements = [], els = node.getelementsbytagname(tag), elslen = els.length, pattern = new regexp("(^|\\s)" + classname + "(\\s|$)"); for (; i < elslen; i++) { if (pattern.test(els[i].classname)) { classelements[j] = els[i]; j++; }; }; return classelements; }; /** * 遍历 * @param {object} * @param {function} */ $.each = function (obj, callback) { var name, i = 0, length = obj.length, isobj = length === undefined; if (isobj) { for (name in obj) { if (callback.call(obj[name], name, obj[name]) === false) { break; }; }; } else { for ( var value = obj[0]; i < length && callback.call(value, i, value) !== false; value = obj[++i] ) { }; }; return obj; }; /** * 读写缓存 * @param {htmlelement} 元素 * @param {string} 缓存名称 * @param {any} 数据 * @return {any} 如果无参数data则返回缓存数据 */ $.data = function (elem, name, data) { var cache = $.cache, id = uuid(elem); if (name === undefined) { return cache[id]; }; if (!cache[id]) { cache[id] = {}; }; if (data !== undefined) { cache[id][name] = data; }; return cache[id][name]; }; /** * 删除缓存 * @param {htmlelement} 元素 * @param {string} 缓存名称 */ $.removedata = function (elem, name) { var empty = true, expando = $.expando, cache = $.cache, id = uuid(elem), thiscache = id && cache[id]; if (!thiscache) { return; }; if (name) { delete thiscache[name]; for (var n in thiscache) { empty = false; }; if (empty) { delete $.cache[id]; }; } else { delete cache[id]; if (elem.removeattribute) { elem.removeattribute(expando); } else { elem[expando] = null; }; }; }; $.uuid = 0; $.cache = {}; $.expando = '@cache' + +new date; // 标记元素唯一身份 function uuid(elem) { var expando = $.expando, id = elem === window ? 0 : elem[expando]; if (id === undefined) elem[expando] = id = ++$.uuid; return id; }; /** * 事件机制 * @namespace * @requires [$.data, $.removedata] */ $.event = { /** * 添加事件 * @param {htmlelement} 元素 * @param {string} 事件类型 * @param {function} 要添加的函数 */ add: function (elem, type, callback) { var cache, listeners, that = $.event, data = $.data(elem, '@events') || $.data(elem, '@events', {}); cache = data[type] = data[type] || {}; listeners = cache.listeners = cache.listeners || []; listeners.push(callback); if (!cache.handler) { cache.elem = elem; cache.handler = that.handler(cache); elem.addeventlistener ? elem.addeventlistener(type, cache.handler, false) : elem.attachevent('on' + type, cache.handler); }; }, /** * 卸载事件 * @param {htmlelement} 元素 * @param {string} 事件类型 * @param {function} 要卸载的函数 */ remove: function (elem, type, callback) { var i, cache, listeners, that = $.event, empty = true, data = $.data(elem, '@events'); if (!data) { return; }; if (!type) { for (i in data) that.remove(elem, i); return; }; cache = data[type]; if (!cache) { return; }; listeners = cache.listeners; if (callback) { for (i = 0; i < listeners.length; i++) { listeners[i] === callback && listeners.splice(i--, 1); }; } else { cache.listeners = []; }; if (cache.listeners.length === 0) { elem.removeeventlistener ? elem.removeeventlistener(type, cache.handler, false) : elem.detachevent('on' + type, cache.handler); delete data[type]; cache = $.data(elem, '@events'); for (var n in cache) { empty = false; }; if (empty) { $.removedata(elem, '@events'); }; }; }, /** @inner 事件句柄 */ handler: function (cache) { return function (event) { event = $.event.fix(event || window.event); for (var i = 0, list = cache.listeners, fn; fn = list[i++]; ) { if (fn.call(cache.elem, event) === false) { event.preventdefault(); event.stoppropagation(); }; }; }; }, /** @inner event对象兼容处理 */ fix: function (event) { if (event.target) { return event; }; var eventobj = { target: event.srcelement || document, preventdefault: function () { event.returnvalue = false }, stoppropagation: function () { event.cancelbubble = true } }; // ie6/7/8 在原生window.event对象写入数据会导致内存无法回收,应当采用拷贝 for (var i in event) { eventobj[i] = event[i]; } return eventobj; } }; /** * 清理元素集的事件与缓存 * @requires [$.removedata, $.event] * @param {htmlcollection} 元素集 */ $.cleandata = function (elems) { var i = 0, elem, len = elems.length, removeevent = $.event.remove, removedata = $.removedata; for (; i < len; i++) { elem = elems[i]; removeevent(elem); removedata(elem); }; }; // 获取css $.css = 'defaultview' in document && 'getcomputedstyle' in document.defaultview ? function (elem, name) { return document.defaultview.getcomputedstyle(elem, false)[name]; } : function (elem, name) { return elem.currentstyle[name] || ''; }; /** * 获取滚动条位置 - [不支持写入] * $.fn.scrollleft, $.fn.scrolltop * @example 获取文档垂直滚动条:$(document).scrolltop() * @return {number} 返回滚动条位置 */ $.each(['left', 'top'], function (i, name) { var method = 'scroll' + name; $.fn[method] = function () { var elem = this[0], win; win = getwindow(elem); return win ? ('pagexoffset' in win) ? win[i ? 'pageyoffset' : 'pagexoffset'] : win.document.documentelement[method] || win.document.body[method] : elem[method]; }; }); function getwindow(elem) { return $.iswindow(elem) ? elem : elem.nodetype === 9 ? elem.defaultview || elem.parentwindow : false; }; /** * 获取窗口或文档尺寸 - [只支持window与document读取] * @example 获取文档宽度:$(document).width() 获取可视范围:$(window).width() * @return {number} */ $.each(['height', 'width'], function (i, name) { var type = name.tolowercase(); $.fn[type] = function (size) { var elem = this[0]; if (!elem) { return size == null ? null : this; }; return $.iswindow(elem) ? elem.document.documentelement['client' + name] || elem.document.body['client' + name] : (elem.nodetype === 9) ? math.max( elem.documentelement['client' + name], elem.body['scroll' + name], elem.documentelement['scroll' + name], elem.body['offset' + name], elem.documentelement['offset' + name] ) : null; }; }); return $ } (window)); ; (function ($, window, undefined) { // artdialog 只支持 xhtml 1.0 或者以上的 doctype 声明 if (document.compatmode === 'backcompat') { throw new error('artdialog: document types require more than xhtml1.0'); }; var _singleton, _count = 0, _expando = 'artdialog' + +new date, _isie6 = window.vbarray && !window.xmlhttprequest, _ismobile = 'createtouch' in document && !('onmousemove' in document) || /(iphone|ipad|ipod)/i.test(navigator.useragent), _isfixed = !_isie6 && !_ismobile; var artdialog = function (config, ok, cancel) { config = config || {}; if (typeof config === 'string' || config.nodetype === 1) { config = { content: config, fixed: !_ismobile }; }; var api, defaults = artdialog.defaults; var elem = config.follow = this.nodetype === 1 && this || config.follow; // 合并默认配置 for (var i in defaults) { if (config[i] === undefined) { config[i] = defaults[i]; }; }; config.id = elem && elem[_expando + 'follow'] || config.id || _expando + _count; api = artdialog.list[config.id]; if (api) { if (elem) { api.follow(elem) }; api.zindex().focus(); return api; }; // 目前主流移动设备对fixed支持不好 if (!_isfixed) { config.fixed = false; }; // !$.isarray(config.button) if (!config.button || !config.button.push) { config.button = []; }; // 确定按钮 if (ok !== undefined) { config.ok = ok; }; if (config.ok) { config.button.push({ id: 'ok', value: config.okvalue, callback: config.ok, focus: true }); }; // 取消按钮 if (cancel !== undefined) { config.cancel = cancel; }; if (config.cancel) { config.button.push({ id: 'cancel', value: config.cancelvalue, callback: config.cancel }); }; if(config.iframe) { config.content = ""; config.esc = false; } // 更新 zindex 全局配置 artdialog.defaults.zindex = config.zindex; _count++; return artdialog.list[config.id] = _singleton ? _singleton.constructor(config) : new artdialog.fn.constructor(config); }; artdialog.version = '5.0'; artdialog.fn = artdialog.prototype = { /** @inner */ constructor: function (config) { var dom; this.closed = false; this.config = config; this.dom = dom = this.dom || this._getdom(); config.skin && dom.wrap.addclass(config.skin); dom.wrap.css('position', config.fixed ? 'fixed' : 'absolute'); dom.close[config.cancel === false ? 'hide' : 'show'](); dom.content.css('padding', config.padding); this.button.apply(this, config.button); this.title(config.title) .content(config.content) .size(config.width, config.height) .time(config.time); config.follow ? this.follow(config.follow) : this.position(); this.zindex(); config.lock && this.lock(); this._addevent(); this[config.visible ? 'visible' : 'hidden']().focus(); _singleton = null; config.initialize && config.initialize.call(this); return this; }, /** * 设置内容 * @param {string, htmlelement, object} 内容 (可选) */ content: function (message) { var prev, next, parent, display, that = this, $content = this.dom.content, content = $content[0]; if (this._elemback) { this._elemback(); delete this._elemback; }; if (typeof message === 'string') { $content.html(message); } else if (message && message.nodetype === 1) { // 让传入的元素在对话框关闭后可以返回到原来的地方 display = message.style.display; prev = message.previoussibling; next = message.nextsibling; parent = message.parentnode; this._elemback = function () { if (prev && prev.parentnode) { prev.parentnode.insertbefore(message, prev.nextsibling); } else if (next && next.parentnode) { next.parentnode.insertbefore(message, next); } else if (parent) { parent.appendchild(message); }; message.style.display = display; that._elemback = null; }; $content.html(''); content.appendchild(message); $(message).show(); }; return this.position(); }, /** * 设置标题 * @param {string, boolean} 标题内容. 为 false 则隐藏标题栏 */ title: function (content) { var dom = this.dom, outer = dom.outer, $title = dom.title, classname = 'd-state-notitle'; if (content === false) { $title.hide().html(''); outer.addclass(classname); } else { $title.show().html(content); outer.removeclass(classname); }; return this; }, /** @inner 位置居中 */ position: function () { var dom = this.dom, wrap = dom.wrap[0], $window = dom.window, $document = dom.document, fixed = this.config.fixed, dl = fixed ? 0 : $document.scrollleft(), dt = fixed ? 0 : $document.scrolltop(), ww = $window.width(), wh = $window.height(), ow = wrap.offsetwidth, oh = wrap.offsetheight, left = (ww - ow) / 2 + dl, top = top = (oh < 4 * wh / 7 ? wh * 0.382 - oh / 2 : (wh - oh) / 2) + dt, style = wrap.style; style.left = math.max(left, dl) + 'px'; style.top = math.max(top, dt) + 'px'; return this; }, /** * 尺寸 * @param {number, string} 宽度 * @param {number, string} 高度 */ size: function (width, height) { var style = this.dom.main[0].style; if (typeof width === 'number') { width = width + 'px'; }; if (typeof height === 'number') { height = height + 'px'; }; style.width = width; style.height = height; return this; }, /** * 跟随元素 * @param {htmlelement} */ follow: function (elem) { var $elem = $(elem), config = this.config; // 隐藏元素不可用 if (!elem || !elem.offsetwidth && !elem.offsetheight) { return this.position(this._left, this._top); }; var fixed = config.fixed, expando = _expando + 'follow', dom = this.dom, $window = dom.window, $document = dom.document, winwidth = $window.width(), winheight = $window.height(), docleft = $document.scrollleft(), doctop = $document.scrolltop(), offset = $elem.offset(), width = elem.offsetwidth, height = elem.offsetheight, left = fixed ? offset.left - docleft : offset.left, top = fixed ? offset.top - doctop : offset.top, wrap = this.dom.wrap[0], style = wrap.style, wrapwidth = wrap.offsetwidth, wrapheight = wrap.offsetheight, setleft = left - (wrapwidth - width) / 2, settop = top + height, dl = fixed ? 0 : docleft, dt = fixed ? 0 : doctop; setleft = setleft < dl ? left : (setleft + wrapwidth > winwidth) && (left - wrapwidth > dl) ? left - wrapwidth + width : setleft; settop = (settop + wrapheight > winheight + dt) && (top - wrapheight > dt) ? top - wrapheight : settop; style.left = setleft + 'px'; style.top = settop + 'px'; this._follow && this._follow.removeattribute(expando); this._follow = elem; elem[expando] = config.id; return this; }, /** * 自定义按钮 * @example button({ value: 'login', callback: function () {}, disabled: false, focus: true }, .., ..) */ button: function () { var dom = this.dom, $buttons = dom.buttons, elem = $buttons[0], strongbutton = 'd-state-highlight', listeners = this._listeners = this._listeners || {}, ags = [].slice.call(arguments); var i = 0, val, value, id, isnewbutton, button; for (; i < ags.length; i++) { val = ags[i]; value = val.value; id = val.id || value; isnewbutton = !listeners[id]; button = !isnewbutton ? listeners[id].elem : document.createelement('input'); button.type = 'button'; button.classname = 'd-button'; if (!listeners[id]) { listeners[id] = {}; }; if (value) { button.value = value; }; if (val.width) { button.style.width = val.width; }; if (val.callback) { listeners[id].callback = val.callback; }; if (val.focus) { this._focus && this._focus.removeclass(strongbutton); this._focus = $(button).addclass(strongbutton); this.focus(); }; button[_expando + 'callback'] = id; button.disabled = !!val.disabled; if (isnewbutton) { listeners[id].elem = button; elem.appendchild(button); }; }; $buttons[0].style.display = ags.length ? '' : 'none'; return this; }, /** 显示对话框 */ visible: function () { //this.dom.wrap.show(); this.dom.wrap.css('visibility', 'visible'); this.dom.outer.addclass('d-state-visible'); if (this._islock) { this._lockmask.show(); }; return this; }, /** 隐藏对话框 */ hidden: function () { //this.dom.wrap.hide(); this.dom.wrap.css('visibility', 'hidden'); this.dom.outer.removeclass('d-state-visible'); if (this._islock) { this._lockmask.hide(); }; return this; }, /** 关闭对话框 */ close: function () { try { window.parent.document.getelementbyid("d_close").style.display = 'block'; } catch(e) { } if (this.closed) { return this; }; var dom = this.dom, $wrap = dom.wrap, list = artdialog.list, beforeunload = this.config.beforeunload, follow = this.config.follow; if (beforeunload && beforeunload.call(this) === false) { return this; }; if (artdialog.focus === this) { artdialog.focus = null; }; if (follow) { follow.removeattribute(_expando + 'follow'); }; if (this._elemback) { this._elemback(); }; this.time(); this.unlock(); this._removeevent(); delete list[this.config.id]; if (_singleton) { $wrap.remove(); // 使用单例模式 } else { _singleton = this; dom.title.html(''); dom.content.html(''); dom.buttons.html(''); $wrap[0].classname = $wrap[0].style.csstext = ''; dom.outer[0].classname = 'd-outer'; $wrap.css({ left: 0, top: 0, position: _isfixed ? 'fixed' : 'absolute' }); for (var i in this) { if (this.hasownproperty(i) && i !== 'dom') { delete this[i]; }; }; this.hidden(); }; this.closed = true; return this; }, /** * 定时关闭 * @param {number} 单位毫秒, 无参数则停止计时器 */ time: function (time) { var that = this, timer = this._timer; timer && cleartimeout(timer); if (time) { this._timer = settimeout(function () { that._click('cancel'); }, time); }; return this; }, /** @inner 设置焦点 */ focus: function () { if (this.config.focus) { //settimeout(function () { try { var elem = this._focus && this._focus[0] || this.dom.close[0]; elem && elem.focus(); // ie对不可见元素设置焦点会报错 } catch (e) { }; //}, 0); }; return this; }, /** 置顶对话框 */ zindex: function () { var dom = this.dom, top = artdialog.focus, index = artdialog.defaults.zindex++; // 设置叠加高度 dom.wrap.css('zindex', index); this._lockmask && this._lockmask.css('zindex', index - 1); // 设置最高层的样式 top && top.dom.outer.removeclass('d-state-focus'); artdialog.focus = this; dom.outer.addclass('d-state-focus'); return this; }, /** 设置屏锁 */ lock: function () { if (this._islock) { return this; }; var that = this, config = this.config, dom = this.dom, div = document.createelement('div'), $div = $(div), index = artdialog.defaults.zindex - 1; this.zindex(); dom.outer.addclass('d-state-lock'); $div.css({ zindex: index, position: 'fixed', left: 0, top: 0, width: '100%', height: '100%', overflow: 'hidden' }).addclass('d-mask'); if (!_isfixed) { $div.css({ position: 'absolute', width: $(window).width() + 'px', height: $(document).height() + 'px' }); }; // $div.bind('click', function () { // that._reset(); // }).bind('dblclick', function () { // that._click('cancel'); // }); document.body.appendchild(div); this._lockmask = $div; this._islock = true; return this; }, /** 解开屏锁 */ unlock: function () { if (!this._islock) { return this; }; this._lockmask.unbind(); this._lockmask.hide(); this._lockmask.remove(); this.dom.outer.removeclass('d-state-lock'); this._islock = false; return this; }, // 获取元素 _getdom: function () { var body = document.body; if (!body) { throw new error('artdialog: "documents.body" not ready'); }; var wrap = document.createelement('div'); wrap.style.csstext = 'position:absolute;left:0;top:0'; wrap.innerhtml = artdialog._templates; body.insertbefore(wrap, body.firstchild); var name, i = 0, dom = {}, els = wrap.getelementsbytagname('*'), elslen = els.length; for (; i < elslen; i++) { name = els[i].classname.split('d-')[1]; if (name) { dom[name] = $(els[i]); }; }; dom.window = $(window); dom.document = $(document); dom.wrap = $(wrap); return dom; }, // 按钮回调函数触发 _click: function (id) { var fn = this._listeners[id] && this._listeners[id].callback; return typeof fn !== 'function' || fn.call(this) !== false ? this.close() : this; }, // 重置位置 _reset: function () { var elem = this.config.follow; elem ? this.follow(elem) : this.position(); }, // 事件代理 _addevent: function () { var that = this, dom = this.dom; // 监听点击 dom.wrap .bind('click', function (event) { var target = event.target, callbackid; // ie bug if (target.disabled) { return false; }; if (target === dom.close[0]) { that._click('cancel'); return false; } else { callbackid = target[_expando + 'callback']; callbackid && that._click(callbackid); }; }) .bind('mousedown', function () { that.zindex(); }); }, // 卸载事件代理 _removeevent: function () { this.dom.wrap.unbind(); } }; artdialog.fn.constructor.prototype = artdialog.fn; $.fn.dialog = $.fn.artdialog = function () { var config = arguments; this[this.live ? 'live' : 'bind']('click', function () { artdialog.apply(this, config); return false; }); return this; }; /** 最顶层的对话框api */ artdialog.focus = null; /** * 根据 id 获取某对话框 api * @param {string} 对话框 id * @return {object} 对话框 api (实例) */ artdialog.get = function (id) { return id === undefined ? artdialog.list : artdialog.list[id]; }; artdialog.list = {}; // 全局快捷键 $(document).bind('keydown', function (event) { var target = event.target, nodename = target.nodename, rinput = /^input|textarea$/i, api = artdialog.focus, keycode = event.keycode; if (!api || !api.config.esc || rinput.test(nodename) && target.type !== 'button') { return; }; // esc keycode === 27 && api._click('cancel'); }); // 浏览器窗口改变后重置对话框位置 $(window).bind('resize', function () { var dialogs = artdialog.list; for (var id in dialogs) { dialogs[id]._reset(); }; }); // xhtml 模板 // 使用 uglifyjs 压缩能够预先处理"+"号合并字符串 // @see http://marijnhaverbeke.nl/uglifyjs artdialog._templates = '
' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
' + '
' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
' + '
' + '
' + '
' + '
关闭' + '
' + '
' + '
' + '
' + '
'; /** * 默认配置 */ artdialog.defaults = { // 消息内容 content: '
loading..
', // 标题 title: 'message', // 自定义按钮 button: null, // 确定按钮回调函数 ok: null, // 取消按钮回调函数 cancel: null, // 对话框初始化后执行的函数 initialize: null, // 对话框关闭前执行的函数 beforeunload: null, // 确定按钮文本 okvalue: 'ok', // 取消按钮文本 cancelvalue: 'cancel', // 内容宽度 width: 'auto', // 内容高度 height: 'auto', // 内容与边界填充距离 padding: '20px 25px', // 风格创意名(多风格创意共存预留接口) skin: null, // 自动关闭时间 time: null, // 是否支持esc键关闭 esc: true, // 是否支持对话框按钮自动聚焦 focus: true, // 初始化后是否显示对话框 visible: true, // 让对话框跟随某元素 follow: null, // 是否锁屏 lock: false, // 是否固定定位 fixed: false, // 对话框叠加高度值(重要:此值不能超过浏览器最大限制) zindex: 1987, // 是否框架 iframe: false, scroll: false }; this.artdialog = $.dialog = $.artdialog = artdialog; } (this.art || this.jquery, this)); ;(function ($) { /** * 警告 * @param {string, htmlelement} 消息内容 * @param {function} (可选) 回调函数 */ $.alert = $.dialog.alert = function (content, callback) { return $.dialog({ id: 'alert', fixed: true, lock: true, content: content, ok: true, beforeunload: callback }); }; /** * 确认选择 * @param {string, htmlelement} 消息内容 * @param {function} 确定按钮回调函数 * @param {function} 取消按钮回调函数 */ $.confirm = $.dialog.confirm = function (content, ok, cancel) { return $.dialog({ id: 'confirm', fixed: true, lock: true, content: content, ok: ok, cancel: cancel }); }; /** * 输入框 * @param {string, htmlelement} 消息内容 * @param {function} 确定按钮回调函数。函数第一个参数接收用户录入的数据 * @param {string} 输入框默认文本 */ $.prompt = $.dialog.prompt = function (content, ok, defaultvalue) { defaultvalue = defaultvalue || ''; var input; return $.dialog({ id: 'prompt', fixed: true, lock: true, content: [ '
', content, '
', '
', '', '
' ].join(''), initialize: function () { input = this.dom.content.find('.d-input-text')[0]; input.select(); input.focus(); }, ok: function () { return ok && ok.call(this, input.value); }, cancel: function () {} }); }; /** 抖动效果 */ $.dialog.prototype.shake = (function () { var fx = function (ontween, onend, duration) { var starttime = + new date; var timer = setinterval(function () { var runtime = + new date - starttime; var pre = runtime / duration; if (pre >= 1) { clearinterval(timer); onend(pre); } else { ontween(pre); }; }, 13); }; var animate = function (elem, distance, duration) { var quantity = arguments[3]; if (quantity === undefined) { quantity = 6; duration = duration / quantity; }; var style = elem.style; var from = parseint(style.marginleft) || 0; fx(function (pre) { elem.style.marginleft = from + (distance - from) * pre + 'px'; }, function () { if (quantity !== 0) { animate( elem, quantity === 1 ? 0 : (distance / quantity - distance) * 1.3, duration, -- quantity ); }; }, duration); }; return function () { animate(this.dom.wrap[0], 40, 600); return this; }; })(); // 拖拽支持 var dragevent = function () { var that = this, proxy = function (name) { var fn = that[name]; that[name] = function () { return fn.apply(that, arguments); }; }; proxy('start'); proxy('over'); proxy('end'); }; dragevent.prototype = { // 开始拖拽 // onstart: function () {}, start: function (event) { $(document) .bind('mousemove', this.over) .bind('mouseup', this.end); this._sclientx = event.clientx; this._sclienty = event.clienty; this.onstart(event.clientx, event.clienty); return false; }, // 正在拖拽 // onover: function () {}, over: function (event) { this._mclientx = event.clientx; this._mclienty = event.clienty; this.onover( event.clientx - this._sclientx, event.clienty - this._sclienty ); return false; }, // 结束拖拽 // onend: function () {}, end: function (event) { $(document) .unbind('mousemove', this.over) .unbind('mouseup', this.end); this.onend(event.clientx, event.clienty); return false; } }; var $window = $(window), $document = $(document), html = document.documentelement, isie6 = !('minwidth' in html.style), islosecapture = !isie6 && 'onlosecapture' in html, issetcapture = 'setcapture' in html, dragstart = function () { return false }; var draginit = function (event) { var dragevent = new dragevent, api = artdialog.focus, dom = api.dom, $wrap = dom.wrap, $title = dom.title, $main = dom.main, wrap = $wrap[0], title = $title[0], main = $main[0], wrapstyle = wrap.style, mainstyle = main.style; var isresize = event.target === dom.se[0] ? true : false; var isfixed = wrap.style.position === 'fixed', minx = isfixed ? 0 : $document.scrollleft(), miny = isfixed ? 0 : $document.scrolltop(), maxx = $window.width() - wrap.offsetwidth + minx, maxy = $window.height() - wrap.offsetheight + miny; var startwidth, startheight, startleft, starttop; // 对话框准备拖动 dragevent.onstart = function (x, y) { if (isresize) { startwidth = main.offsetwidth; startheight = main.offsetheight; } else { startleft = wrap.offsetleft; starttop = wrap.offsettop; }; $document.bind('dblclick', dragevent.end) .bind('dragstart', dragstart); if (islosecapture) { $title.bind('losecapture', dragevent.end) } else { $window.bind('blur', dragevent.end) }; issetcapture && title.setcapture(); $wrap.addclass('d-state-drag'); api.focus(); }; // 对话框拖动进行中 dragevent.onover = function (x, y) { if (isresize) { var width = x + startwidth, height = y + startheight; wrapstyle.width = 'auto'; mainstyle.width = math.max(0, width) + 'px'; wrapstyle.width = wrap.offsetwidth + 'px'; mainstyle.height = math.max(0, height) + 'px'; } else { var left = math.max(minx, math.min(maxx, x + startleft)), top = math.max(miny, math.min(maxy, y + starttop)); wrapstyle.left = left + 'px'; wrapstyle.top = top + 'px'; }; }; // 对话框拖动结束 dragevent.onend = function (x, y) { $document.unbind('dblclick', dragevent.end) .unbind('dragstart', dragstart); if (islosecapture) { $title.unbind('losecapture', dragevent.end); } else { $window.unbind('blur', dragevent.end) }; issetcapture && title.releasecapture(); $wrap.removeclass('d-state-drag'); }; dragevent.start(event); }; // 代理 mousedown 事件触发对话框拖动 $(document).bind('mousedown', function (event) { if(!artdialog) return; var api = artdialog.focus; if (!api) return; var target = event.target, config = api.config, dom = api.dom; if (!config) { return; } if (config.drag !== false && target === dom.title[0] || config.resize !== false && target === dom.se[0]) { draginit(event); // 防止firefox与chrome滚屏 return false; }; }); }(this.art || this.jquery));