/**
* jquery dialog plugin
*
* depends:
* - jquery 1.4.2+
* author:
* - lanqy 2015-12-28
* github:
* - https://github.com/lanqy/dialog
*/
(function($) {
$.extend($.fn, {
dialog: function(options) {
return this.each(function() {
var dialog = $.data(this, "dialog");
if (!dialog) {
dialog = new $.dialog(options, this);
$.data(this, "dialog", dialog);
}
});
}
});
$.dialog = function(options, el) {
if (arguments.length) {
this._init(options, el);
}
}
$.dialog.prototype = {
options: {
title: 'title', // title
showheader:true, //
dragable: false,
cache: true, // jquery dataļ¼default true
html: '', // html template
width: 'auto', // width
height: 'auto', // height
cannelbtn:true,
confirmbtn:true,
canneltext: 'cannel', // cannel text
confirmtext: 'confirm', // confirm text
showfooter: true,
onclose: false, // colse callback
onopen: false, // open callback
onconfirm: false, // confirm callback
oncannel: false, // cannel callback
getcontent: false // get content callback
},
_init: function(options, el) { // init
this.options = $.extend(true, {}, this.options, options)
this.element = $(el);
this._build(this.options.html);
this._bindevents();
},
_bindevents: function() { // bind events
var self = this;
this.element.delegate('.close','click',function(){
self.close(self.options.onclose);
});
this.element.delegate('.cannel','click',function(){
self._cannel(self.options.oncannel);
});
this.element.delegate('.confirm','click',function(){
self._confirm(self.options.onconfirm);
});
$(window).bind("resize", function() {
self._center();
self._setlayerwidthheight();
});
if (self.options.dragable) {
self._dragable();
}
},
_build: function(html) { // build the template for dialog
var html;
var footer = '';
var header = '';
var cfbtn ='';
var clbtn ='';
var bodycontent = '
';
if (html) {
html = html;
} else {
if(this.options.confirmbtn){
cfbtn = '';
}
if(this.options.cannelbtn){
clbtn = '';
}
if (this.options.showfooter) {
footer = '';
}
if(this.options.showheader){
header = '';
}
if(this.options.showfooter){
var h = this.options.height - 80;
bodycontent = '';
}else{
bodycontent = '';
}
html = '\
'+ header +'\
\
'+ bodycontent +'\
' + footer + '
';
}
this.element.html(html);
$("body").append("");
if (this.options.getcontent) {
this._setcontent(this.options.getcontent);
}
this._center();
this._setlayerwidthheight();
},
_dragable: function() { // dragable
var h = this.element.find('.header').find('h2'),
c = this.element.find('.dialog'),
d,
dragging = false,
startobjectx,
startobjecty,
ondragstart = function(e) {
dragging = true;
var pos = c.offset();
startobjectx = pos.left;
startobjecty = pos.top;
d = {
maxx: $(document).width() - c.width(),
maxy: $(document).height() - c.height(),
posx: e.clientx - startobjectx,
posy: e.clienty - startobjecty
};
},
ondragging = function(e) {
if (dragging) {
c.css({
left: math.max(0, math.min(e.clientx - d.posx, d.maxx)) + "px",
top: math.max(0, math.min(e.clienty - d.posy, d.maxy)) + "px"
});
}
},
ondragstop = function() {
dragging = false;
};
h.mousedown(ondragstart);
$(document).mousemove(ondragging).mouseup(ondragstop);
h.undraggable = function() {
h.unbind('mousedown', ondragstart);
$(document).unbind('mousemove', ondragging).unbind('mouseup', ondragstop);
delete h.undraggable;
ondragstart = ondragstop = ondragging = null;
return this;
};
},
_center: function() { // center dialog in window
var d = this.element.find('.dialog');
d.css({
left: ($(document).width() - d.width()) / 2,
top: (document.documentelement.clientheight - d.height()) / 2 + $(document).scrolltop()
});
},
_setlayerwidthheight: function() {
var height = this._getdocheight();
var width = this._getdocwidth();
$(".layer-box, .layer-box .back,.layer-box .lay").css({
width: this._getdocwidth(),
height: this._getdocheight()
})
},
_getdocheight: function() { // get document height
var d = document;
return math.max(d.body.scrollheight, d.documentelement.scrollheight, d.body.offsetheight, d.documentelement.offsetheight, d.body.clientheight, d.documentelement.clientheight);
},
_getdocwidth: function() { // get docuement width
var d = document;
return math.max(math.max(d.body.scrollwidth, d.documentelement.scrollwidth), math.max(d.body.offsetwidth, d.documentelement.offsetwidth), math.max(d.body.clientwidth, d.documentelement.clientwidth));
},
_confirm: function(cb) { // confirm
this._callback(cb);
this._hide();
this.clearcache();
},
_cannel: function(cb) { // cannel
this._callback(cb);
this._hide();
this.clearcache();
},
close: function(cb) { // close
this._callback(cb);
this._hide();
this.clearcache();
},
open: function(cb) { // open
this._callback(cb);
this.element.show();
$('.layer-box').show();
this._center();
this._setlayerwidthheight();
this.clearcache();
},
clearcache: function() { // clear cache
if (!this.options.cache) {
this.element.data('dialog', '');
}
},
_hide: function() { // hidden
this.element.hide();
$('.layer-box').hide();
},
_callback: function(cb) { // callback
if (cb && (typeof(cb) === 'function')) {
cb.call(this);
}
},
_setcontent: function(cb) { // set dialog content
if (cb && (typeof(cb) === 'function')) {
cb();
}
}
}
$.extend($.fn, {
open: function(cb) {
$(this).data("dialog") && $(this).data("dialog").open(cb)
},
close: function(cb) {
$(this).data("dialog") && $(this).data("dialog").close(cb)
}
});
})(jquery);