5
<title>popovers.js - Ubuntu HTML5 API</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.8.0pr2/build/cssgrids/cssgrids-min.css">
7
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
9
<script src="http://yui.yahooapis.com/combo?3.8.0pr2/build/yui/yui-min.js"></script>
11
<body class="yui3-skin-sam">
14
<div id="hd" class="yui3-g header">
15
<div class="yui3-u-3-4">
16
<h1><a href="../index.html"><img src="../assets/css/logo.png">Ubuntu HTML5 API: popovers.js</a></h1>
18
<div class="yui3-u-1-4 version">
19
<em>API Docs for: HTML-14.04-dev~bzr202</em>
24
<div id="sidebar" class="yui3-u">
25
<div id="modules" class="sidebox">
27
<h2 class="no-toc">Modules</h2>
31
<li><a href="../modules/UbuntuUI.html">UbuntuUI</a>
37
<div id="classes" class="sidebox">
39
<h2 class="no-toc">Classes</h2>
43
<li><a href="../classes/UbuntuUI.html">UbuntuUI</a></li>
44
<li><a href="../classes/UbuntuUI.Button.html">UbuntuUI.Button</a></li>
45
<li><a href="../classes/UbuntuUI.Dialog.html">UbuntuUI.Dialog</a></li>
46
<li><a href="../classes/UbuntuUI.Header.html">UbuntuUI.Header</a></li>
47
<li><a href="../classes/UbuntuUI.List.html">UbuntuUI.List</a></li>
48
<li><a href="../classes/UbuntuUI.OptionSelector.html">UbuntuUI.OptionSelector</a></li>
49
<li><a href="../classes/UbuntuUI.Page.html">UbuntuUI.Page</a></li>
50
<li><a href="../classes/UbuntuUI.Pagestack.html">UbuntuUI.Pagestack</a></li>
51
<li><a href="../classes/UbuntuUI.Popover.html">UbuntuUI.Popover</a></li>
52
<li><a href="../classes/UbuntuUI.Progress.html">UbuntuUI.Progress</a></li>
53
<li><a href="../classes/UbuntuUI.Shape.html">UbuntuUI.Shape</a></li>
54
<li><a href="../classes/UbuntuUI.Tab.html">UbuntuUI.Tab</a></li>
55
<li><a href="../classes/UbuntuUI.Tabs.html">UbuntuUI.Tabs</a></li>
56
<li><a href="../classes/UbuntuUI.Toolbar.html">UbuntuUI.Toolbar</a></li>
65
<div id="fileTree" class="sidebox">
67
<h2 class="no-toc">Files</h2>
70
<ul><li>buttons.js/<ul></ul></li><li>core.js/<ul></ul></li><li>dialogs.js/<ul></ul></li><li>fast-buttons.js/<ul></ul></li><li>header.js/<ul></ul></li><li>list.js/<ul></ul></li><li>option-selector.js/<ul></ul></li><li>page.js/<ul></ul></li><li>pagestacks.js/<ul></ul></li><li>popovers.js/<ul></ul></li><li>progress.js/<ul></ul></li><li>shape.js/<ul></ul></li><li>tab.js/<ul></ul></li><li>tabs.js/<ul></ul></li><li>toolbars.js/<ul></ul></li></ul>
76
<div id="main" class="yui3-u">
77
<div class="content"><h4>popovers.js</h4>
79
<pre class="code prettyprint linenums">
81
* Copyright (C) 2013 Adnane Belmadiaf <daker@ubuntu.com>
82
* License granted by Canonical Limited
84
* This file is part of ubuntu-html5-ui-toolkit.
86
* This package is free software; you can redistribute it and/or modify
87
* it under the terms of the GNU Lesser General Public License as
88
* published by the Free Software Foundation; either version 3 of the
90
* (at your option) any later version.
92
* This package is distributed in the hope that it will be useful,
93
* but WITHOUT ANY WARRANTY; without even the implied warranty of
94
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95
* GNU General Public License for more details.
97
* You should have received a copy of the GNU Lesser General Public
98
* License along with this program. If not, see
99
* <http://www.gnu.org/licenses/>.
103
* A Popover is a div containng markup that can pop up and disappear. (Unlike a Dialog, Popovers are not full screen.)
105
A Popoves often contain a List whose items are connected to useful JavaScript functions.
108
######Popover Position
109
The Popover's position is set relative to a specified base element with the <em>data-gravity="LETTER"</em> attribute. LETTER values:
111
- 'n': the base element is above the Popover
112
- 's': the base element is below the Popover
113
- 'e': the base element is to the east of (to the right of) the Popover (in right-to-left locales)
114
- 'w': the base element is to the west of (to the left of) the Popover (in right-to-left locale)
118
* @namespace UbuntuUI
119
* @param {String} elem - The element to which the Popover's position is relative
120
* @param {ID} id - The id attribute of the Popover in HTML
123
<p id="popoverBase">Text</p>
124
<div class="popover active" data-gravity="n" id="popover">
125
<ul class="list">
126
<li class="active"><a href="#">Item1</a></li>
127
<li><a href="#">Item2</a></li>
132
var popBase = document.getElementById("popoverBase");
133
var popover = UI.popover(popBase, "popover");
136
var Popover = function (elem, id) {
138
this.popover = document.getElementById(id);
140
this.parent = elem.parentNode.parentNode.parentNode.parentNode;
141
this.is_parent_footer = (this.parent.nodeName.toLowerCase() === "footer");
144
Popover.prototype = {
150
if (this.popover === null) {
151
console.error('The popover with the ID #' + this.id + ' doesn\'t exist');
154
var de = document.documentElement,
155
w = (de&&de.clientWidth) || document.body.clientWidth,
158
gravity = "s",
163
this.popover.classList.add('active');
164
popoverWidth = this.popover.offsetWidth;
165
popoverHeight = this.popover.offsetHeight;
167
gravity = this.popover.getAttribute("data-gravity");
169
if (this.is_parent_footer) {
170
ot = this.parent.offsetTop;
171
ol = this.elem.offsetLeft;
174
ot = this.elem.offsetTop;
175
ol = this.elem.offsetLeft;
180
pos_top = this.elem.offsetTop + this.elem.offsetHeight + 10;
181
pos_left = this.elem.offsetLeft + this.elem.offsetWidth / 2 - popoverWidth / 2;
184
if (this.is_parent_footer) {
185
pos_top = ot - popoverHeight - 90;
186
pos_left = ol + this.elem.offsetWidth / 2 - popoverWidth / 2;
190
if (pos_left + popoverWidth > w) {
198
pos_top = ot - popoverHeight - 10;
199
pos_left = ol + this.elem.offsetWidth / 2 - popoverWidth / 2;
203
pos_top = this.elem.offsetTop + this.elem.offsetHeight / 2 - popoverHeight / 2;
204
pos_left = this.elem.offsetLeft - popoverWidth - 10;
207
pos_top = this.elem.offsetTop + this.elem.offsetHeight / 2 - popoverHeight / 2;
208
pos_left = this.elem.offsetLeft + this.elem.offsetWidth + 10;
212
this.popover.style.top = pos_top + 'px';
213
if (pos_left === -1) {
214
this.popover.style.left = 'auto';
215
this.popover.style.right = pos_right+ 'px';
218
this.popover.style.left = pos_left+ 'px';
227
this.popover.classList.remove('active');
228
this.popover.style.top = '0px';
229
this.popover.style.left = '0px';
233
* Toggle show/hide status of a Popover
236
toggle: function () {
237
if (this.popover === null) {
238
console.error('The popover with the ID #' + this.id + ' doesn\'t exist');
241
var de = document.documentElement,
242
w = (de&&de.clientWidth) || document.body.clientWidth,
245
gravity = "s",
250
this.popover.classList.toggle('active');
251
popoverWidth = this.popover.offsetWidth;
252
popoverHeight = this.popover.offsetHeight;
254
gravity = this.popover.getAttribute("data-gravity");
256
if (this.is_parent_footer) {
257
ot = this.parent.offsetTop;
258
ol = this.elem.offsetLeft;
261
ot = this.elem.offsetTop;
262
ol = this.elem.offsetLeft;
267
pos_top = ot + this.elem.offsetHeight + 10;
268
pos_left = ol + this.elem.offsetWidth / 2 - popoverWidth / 2;
271
if (this.is_parent_footer) {
272
pos_top = ot - popoverHeight - 90;
273
pos_left = ol + this.elem.offsetWidth / 2 - popoverWidth / 2;
277
if (pos_left + popoverWidth > w) {
285
pos_top = ot - popoverHeight - 10;
286
pos_left = ol + this.elem.offsetWidth / 2 - popoverWidth / 2;
290
pos_top = ot + this.elem.offsetHeight / 2 - popoverHeight / 2;
291
pos_left = ol - popoverWidth - 10;
294
pos_top = ot + this.elem.offsetHeight / 2 - popoverHeight / 2;
295
pos_left = ol + this.elem.offsetWidth + 10;
299
this.popover.style.top = pos_top + 'px';
300
if (pos_left === -1) {
301
this.popover.style.left = 'auto';
302
this.popover.style.right = pos_right+ 'px';
305
this.popover.style.left = pos_left+ 'px';
310
* Returns the DOM element associated with the id this widget is bind to.
313
var mypopover = UI.popover("popoverid").element();
315
element: function () {
326
<script src="../assets/vendor/prettify/prettify-min.js"></script>
327
<script>prettyPrint();</script>
328
<script src="../assets/js/yui-prettify.js"></script>
329
<script src="../assets/js/tabs.js"></script>