2015-07-18 16:33:31 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-07-18 16:33:31 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
RED.popover = (function() {
|
2017-05-10 16:49:12 +02:00
|
|
|
var deltaSizes = {
|
|
|
|
"default": {
|
|
|
|
top: 10,
|
|
|
|
leftRight: 17,
|
|
|
|
leftLeft: 25
|
|
|
|
},
|
|
|
|
"small": {
|
|
|
|
top: 5,
|
|
|
|
leftRight: 8,
|
|
|
|
leftLeft: 16
|
|
|
|
}
|
|
|
|
}
|
2015-07-18 16:33:31 +02:00
|
|
|
function createPopover(options) {
|
|
|
|
var target = options.target;
|
2017-05-10 16:49:12 +02:00
|
|
|
var direction = options.direction || "right";
|
|
|
|
var trigger = options.trigger;
|
2015-07-18 16:33:31 +02:00
|
|
|
var content = options.content;
|
|
|
|
var delay = options.delay;
|
2017-05-10 16:49:12 +02:00
|
|
|
var width = options.width||"auto";
|
|
|
|
var size = options.size||"default";
|
|
|
|
if (!deltaSizes[size]) {
|
|
|
|
throw new Error("Invalid RED.popover size value:",size);
|
|
|
|
}
|
|
|
|
|
2015-07-18 16:33:31 +02:00
|
|
|
var timer = null;
|
|
|
|
var active;
|
|
|
|
var div;
|
|
|
|
|
|
|
|
var openPopup = function() {
|
|
|
|
if (active) {
|
2017-05-10 16:49:12 +02:00
|
|
|
div = $('<div class="red-ui-popover red-ui-popover-'+direction+'"></div>').appendTo("body");
|
|
|
|
if (size !== "default") {
|
|
|
|
div.addClass("red-ui-popover-size-"+size);
|
|
|
|
}
|
|
|
|
if (typeof content === 'function') {
|
|
|
|
content.call(res).appendTo(div);
|
|
|
|
} else {
|
|
|
|
div.html(content);
|
|
|
|
}
|
|
|
|
if (width !== "auto") {
|
|
|
|
div.width(width);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-18 16:33:31 +02:00
|
|
|
var targetPos = target.offset();
|
|
|
|
var targetWidth = target.width();
|
|
|
|
var targetHeight = target.height();
|
|
|
|
|
|
|
|
var divHeight = div.height();
|
2017-05-10 16:49:12 +02:00
|
|
|
var divWidth = div.width();
|
|
|
|
if (direction === 'right') {
|
|
|
|
div.css({top: targetPos.top+targetHeight/2-divHeight/2-deltaSizes[size].top,left:targetPos.left+targetWidth+deltaSizes[size].leftRight});
|
|
|
|
} else if (direction === 'left') {
|
|
|
|
div.css({top: targetPos.top+targetHeight/2-divHeight/2-deltaSizes[size].top,left:targetPos.left-deltaSizes[size].leftLeft-divWidth});
|
|
|
|
}
|
2015-07-18 16:33:31 +02:00
|
|
|
|
|
|
|
div.fadeIn("fast");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var closePopup = function() {
|
|
|
|
if (!active) {
|
|
|
|
if (div) {
|
|
|
|
div.fadeOut("fast",function() {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
div = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:49:12 +02:00
|
|
|
if (trigger === 'hover') {
|
|
|
|
|
|
|
|
target.on('mouseenter',function(e) {
|
2015-07-18 16:33:31 +02:00
|
|
|
clearTimeout(timer);
|
2017-05-10 16:49:12 +02:00
|
|
|
active = true;
|
|
|
|
timer = setTimeout(openPopup,delay.show);
|
|
|
|
});
|
|
|
|
target.on('mouseleave', function(e) {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
active = false;
|
|
|
|
setTimeout(closePopup,delay.hide);
|
|
|
|
});
|
|
|
|
} else if (trigger === 'click') {
|
|
|
|
target.click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
active = !active;
|
|
|
|
if (!active) {
|
|
|
|
closePopup();
|
|
|
|
} else {
|
|
|
|
openPopup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-18 16:33:31 +02:00
|
|
|
var res = {
|
|
|
|
setContent: function(_content) {
|
|
|
|
content = _content;
|
2017-05-10 16:49:12 +02:00
|
|
|
},
|
|
|
|
open: function () {
|
|
|
|
active = true;
|
|
|
|
openPopup();
|
|
|
|
},
|
|
|
|
close: function () {
|
|
|
|
active = false;
|
|
|
|
closePopup();
|
2015-07-18 16:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
create: createPopover
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|