2013-04-01 03:43:01 +02:00
|
|
|
(function($) {
|
|
|
|
$.widget('oc.ocdialog', {
|
|
|
|
options: {
|
|
|
|
width: 'auto',
|
2013-04-03 16:50:50 +02:00
|
|
|
height: 'auto',
|
|
|
|
closeButton: true,
|
|
|
|
closeOnEscape: true
|
2013-04-01 03:43:01 +02:00
|
|
|
},
|
|
|
|
_create: function() {
|
2013-04-03 16:50:50 +02:00
|
|
|
console.log('ocdialog._create');
|
2013-04-01 03:43:01 +02:00
|
|
|
var self = this;
|
|
|
|
|
2013-04-03 16:50:50 +02:00
|
|
|
this.originalCss = {
|
|
|
|
display: this.element[0].style.display,
|
|
|
|
width: this.element[0].style.width,
|
|
|
|
height: this.element[0].style.height,
|
|
|
|
};
|
2013-04-01 15:12:53 +02:00
|
|
|
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$dialog = $('<div class="oc-dialog" />')
|
|
|
|
.attr({
|
|
|
|
// Setting tabIndex makes the div focusable
|
|
|
|
tabIndex: -1,
|
|
|
|
role: 'dialog'
|
|
|
|
})
|
|
|
|
.insertBefore(this.element);
|
|
|
|
this.$dialog.append(this.element.detach());
|
|
|
|
this.element.addClass('oc-dialog-content').appendTo(this.$dialog);
|
2013-04-01 03:43:01 +02:00
|
|
|
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$dialog.css({
|
|
|
|
display: 'inline-block',
|
|
|
|
position: 'fixed'
|
|
|
|
});
|
2013-04-01 03:43:01 +02:00
|
|
|
|
2013-04-03 16:50:50 +02:00
|
|
|
$(document).on('keydown keyup', function(event) {
|
|
|
|
if(event.target !== self.$dialog.get(0) && self.$dialog.find($(event.target)).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Escape
|
|
|
|
if(event.keyCode === 27 && self.options.closeOnEscape) {
|
|
|
|
self.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Enter
|
|
|
|
if(event.keyCode === 13) {
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
if(event.type === 'keyup') {
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If no button is selected we trigger the primary
|
|
|
|
if(self.$buttonrow && self.$buttonrow.find($(event.target)).length === 0) {
|
|
|
|
var $button = self.$buttonrow.find('button.primary');
|
|
|
|
if($button) {
|
|
|
|
$button.trigger('click');
|
|
|
|
}
|
|
|
|
} else if(self.$buttonrow) {
|
|
|
|
$(event.target).trigger('click');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-04-01 03:43:01 +02:00
|
|
|
$(window).resize(function() {
|
2013-04-03 16:50:50 +02:00
|
|
|
self.parent = self.$dialog.parent().length > 0 ? self.$dialog.parent() : $('body');
|
2013-04-01 15:12:53 +02:00
|
|
|
var pos = self.parent.position();
|
2013-04-03 16:50:50 +02:00
|
|
|
self.$dialog.css({
|
|
|
|
left: pos.left + (self.parent.width() - self.$dialog.outerWidth())/2,
|
|
|
|
top: pos.top + (self.parent.height() - self.$dialog.outerHeight())/2
|
2013-04-01 03:43:01 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this._setOptions(this.options);
|
|
|
|
$(window).trigger('resize');
|
|
|
|
},
|
2013-04-01 15:12:53 +02:00
|
|
|
_init: function() {
|
|
|
|
console.log('ocdialog._init');
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$dialog.focus();
|
2013-04-01 15:12:53 +02:00
|
|
|
},
|
2013-04-01 03:43:01 +02:00
|
|
|
_setOption: function(key, value) {
|
|
|
|
console.log('_setOption', key, value);
|
2013-04-03 16:50:50 +02:00
|
|
|
var self = this;
|
2013-04-01 03:43:01 +02:00
|
|
|
switch(key) {
|
|
|
|
case 'title':
|
|
|
|
var $title = $('<h3 class="oc-dialog-title">' + this.options.title
|
2013-04-05 19:05:47 +02:00
|
|
|
+ '</h3>'); //<hr class="oc-dialog-separator" />');
|
2013-04-01 03:43:01 +02:00
|
|
|
if(this.$title) {
|
|
|
|
this.$title.replaceWith($title);
|
|
|
|
} else {
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$title = $title.prependTo(this.$dialog);
|
2013-04-01 03:43:01 +02:00
|
|
|
}
|
|
|
|
this._setSizes();
|
|
|
|
break;
|
|
|
|
case 'buttons':
|
|
|
|
var $buttonrow = $('<div class="oc-dialog-buttonrow" />');
|
|
|
|
if(this.$buttonrow) {
|
|
|
|
this.$buttonrow.replaceWith($buttonrow);
|
|
|
|
} else {
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$buttonrow = $buttonrow.appendTo(this.$dialog);
|
2013-04-01 03:43:01 +02:00
|
|
|
}
|
|
|
|
$.each(value, function(idx, val) {
|
2013-04-01 15:12:53 +02:00
|
|
|
var $button = $('<button>' + val.text + '</button>');
|
2013-04-03 16:50:50 +02:00
|
|
|
if(val.defaultButton) {
|
|
|
|
$button.addClass('primary');
|
|
|
|
self.$defaultButton = $button;
|
|
|
|
}
|
2013-04-01 03:43:01 +02:00
|
|
|
self.$buttonrow.append($button);
|
2013-04-01 15:12:53 +02:00
|
|
|
$button.click(function() {
|
|
|
|
val.click.apply(self.element[0], arguments);
|
|
|
|
});
|
2013-04-01 03:43:01 +02:00
|
|
|
});
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$buttonrow.find('button')
|
|
|
|
.on('focus', function(event) {
|
|
|
|
self.$buttonrow.find('button').removeClass('primary');
|
|
|
|
$(this).addClass('primary');
|
|
|
|
});
|
2013-04-01 03:43:01 +02:00
|
|
|
this._setSizes();
|
|
|
|
break;
|
2013-04-03 16:50:50 +02:00
|
|
|
case 'closeButton':
|
|
|
|
console.log('closeButton', value);
|
|
|
|
if(value) {
|
|
|
|
var $closeButton = $('<a class="oc-dialog-close svg"></a>');
|
|
|
|
console.log('closeButton', $closeButton);
|
|
|
|
this.$dialog.prepend($closeButton);
|
|
|
|
$closeButton.on('click', function() {
|
|
|
|
self.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2013-04-01 03:43:01 +02:00
|
|
|
case 'width':
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$dialog.css('width', value);
|
2013-04-01 03:43:01 +02:00
|
|
|
break;
|
|
|
|
case 'height':
|
2013-04-03 16:50:50 +02:00
|
|
|
this.$dialog.css('height', value);
|
2013-04-01 03:43:01 +02:00
|
|
|
break;
|
|
|
|
case 'close':
|
|
|
|
this.closeCB = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//this._super(key, value);
|
|
|
|
$.Widget.prototype._setOption.apply(this, arguments );
|
|
|
|
},
|
|
|
|
_setOptions: function(options) {
|
|
|
|
console.log('_setOptions', options);
|
|
|
|
//this._super(options);
|
|
|
|
$.Widget.prototype._setOptions.apply(this, arguments);
|
|
|
|
},
|
|
|
|
_setSizes: function() {
|
2013-04-03 16:50:50 +02:00
|
|
|
var content_height = this.$dialog.height();
|
2013-04-01 03:43:01 +02:00
|
|
|
if(this.$title) {
|
|
|
|
content_height -= this.$title.outerHeight(true);
|
|
|
|
}
|
|
|
|
if(this.$buttonrow) {
|
|
|
|
content_height -= this.$buttonrow.outerHeight(true);
|
|
|
|
}
|
2013-04-06 02:10:32 +02:00
|
|
|
this.parent = this.$dialog.parent().length > 0 ? this.$dialog.parent() : $('body');
|
|
|
|
content_height = Math.min(content_height, this.parent.height()-20)
|
2013-04-03 16:50:50 +02:00
|
|
|
this.element.css({
|
2013-04-01 03:43:01 +02:00
|
|
|
height: content_height + 'px',
|
2013-04-03 16:50:50 +02:00
|
|
|
width: this.$dialog.innerWidth() + 'px'
|
2013-04-01 03:43:01 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
close: function() {
|
2013-04-03 16:50:50 +02:00
|
|
|
console.log('close');
|
|
|
|
var self = this;
|
|
|
|
// Ugly hack to catch remaining keyup events.
|
|
|
|
setTimeout(function() {
|
|
|
|
self._trigger('close', self);
|
|
|
|
self.$dialog.hide();
|
|
|
|
}, 200);
|
2013-04-01 03:43:01 +02:00
|
|
|
},
|
|
|
|
destroy: function() {
|
|
|
|
console.log('destroy');
|
|
|
|
if(this.$title) {
|
|
|
|
this.$title.remove()
|
|
|
|
}
|
|
|
|
if(this.$buttonrow) {
|
|
|
|
this.$buttonrow.remove()
|
|
|
|
}
|
2013-04-03 16:50:50 +02:00
|
|
|
this.element.removeClass('oc-dialog-content')
|
|
|
|
.css(this.originalCss).detach().insertBefore(this.$dialog);
|
|
|
|
this.$dialog.remove();
|
2013-04-01 03:43:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}(jQuery));
|