Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit fb03c14

Browse files
author
Gabriel Schulhof
committed
Demos: Add popup alignment demo featuring new popup extension. Fixes #6186,#6187.
1 parent 702af72 commit fb03c14

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Popup alignment - jQuery Mobile Demos</title>
7+
<link rel="stylesheet" href="../../../css/themes/default/jquery.mobile.css">
8+
<link rel="stylesheet" href="../../_assets/css/jqm-demos.css">
9+
<link rel="shortcut icon" href="../../favicon.ico">
10+
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
11+
<script src="../../../js/jquery.js"></script>
12+
<script src="../../_assets/js/"></script>
13+
<script src="../../../js/"></script>
14+
<script id="extension" src="popup.alignment.js"></script>
15+
<script id="glue">
16+
( function( $, undefined ) {
17+
18+
$.mobile.document.on( "slidestop", function() {
19+
20+
setTimeout( function() {
21+
$( "#alignment-example" ).popup( "option", "align",
22+
$( "#xalign" ).val() + "," + $( "#yalign" ).val() );
23+
}, 250 );
24+
25+
});
26+
27+
})( jQuery );
28+
</script>
29+
<style id="alignment-example-style">
30+
#alignment-example {
31+
min-width: 200px;
32+
opacity: 0.8;
33+
}
34+
#open-alignment-example {
35+
position: absolute;
36+
width: 3em;
37+
min-width: 3em;
38+
max-width: 3em;
39+
height: 1.2em;
40+
min-height: 1.2em;
41+
max-height: 1.2em;
42+
margin-left: -1.5em;
43+
margin-top: -0.6em;
44+
left: 50%;
45+
top: 50%;
46+
background-color: #ffa0a0;
47+
border-color: black;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<div data-role="page" id="demo-intro" class="jqm-demos">
53+
54+
<div data-role="header" class="jqm-header">
55+
<h1 class="jqm-logo"><a href="../../"><img src="../../_assets/img/jquery-logo.png" alt="jQuery Mobile Framework"></a></h1>
56+
<a href="#" class="jqm-navmenu-link" data-icon="bars" data-iconpos="notext">Navigation</a>
57+
<a href="#" class="jqm-search-link" data-icon="search" data-iconpos="notext">Search</a>
58+
<?php include( '../../search.php' ); ?>
59+
</div><!-- /header -->
60+
61+
<div data-role="content" class="jqm-content">
62+
<div data-demo-html="true" data-demo-js="#extension" data-demo-css="#alignment-example-style">
63+
<div data-role="popup" id="alignment-example" class="ui-content">
64+
<form data-role="fieldset">
65+
<div data-role="fieldcontain">
66+
<label for="xalign">X Alignment</label>
67+
<input type="range" id="xalign" name="xalign" value="0.5" min="-1" max="2" step="0.5"></input>
68+
</div>
69+
<div data-role="fieldcontain">
70+
<label for="yalign">Y Alignment</label>
71+
<input type="range" id="yalign" name="yalign" value="0.5" min="-1" max="2" step="0.5"></input>
72+
</div>
73+
</form>
74+
</div>
75+
</div>
76+
</div><!-- /content -->
77+
78+
<div data-role="footer" class="jqm-footer">
79+
<p class="jqm-version"></p>
80+
<p>Copyright 2013 The jQuery Foundation</p>
81+
</div><!-- /jqm-footer -->
82+
83+
<?php include( '../../global-nav.php' ); ?>
84+
85+
<a href="#alignment-example" id="open-alignment-example" data-rel="popup" data-role="button" data-inline="true">Open</a>
86+
</div><!-- /page -->
87+
</body>
88+
</html>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
( function( $, undefined ) {
2+
3+
// Eventually, the real handleLink function should pass the coordinates and
4+
// size of the rectangle representing the origin into popup's "open" method, or
5+
// maybe the origin itself, instead of merely the midpoint of it. Here, the
6+
// origin (link) is saved in lastLink, and appended to the options in the
7+
// overridden version of open. Thankfully, JS is not multithreaded. Hooray!
8+
var lastLink,
9+
origHandleLink = $.mobile.popup.handleLink,
10+
handleLink = function( link ) {
11+
lastLink = link;
12+
return origHandleLink.apply( this, arguments );
13+
};
14+
15+
$.mobile.popup.handleLink = handleLink;
16+
17+
$.widget( "mobile.popup", $.mobile.popup, {
18+
options: {
19+
align: "0.5,0.5"
20+
},
21+
22+
open: function( options ) {
23+
this._ui.link = lastLink;
24+
return this._super( options );
25+
},
26+
27+
_closePrereqsDone: function() {
28+
this._ui.link = null;
29+
this._superApply( arguments );
30+
},
31+
32+
_alignmentToCoeffs: function( alignment ) {
33+
return {
34+
originCoeff:
35+
alignment < 0 ? 0 :
36+
alignment <= 1 ? alignment :
37+
1,
38+
popupCoeff:
39+
alignment < 0 ? alignment :
40+
alignment <= 1 ? -alignment :
41+
alignment - 2
42+
};
43+
},
44+
45+
_getAlignment: function() {
46+
var ar, align;
47+
48+
if ( this.options.align ) {
49+
ar = this.options.align.split( "," );
50+
}
51+
52+
if ( ar && ar.length > 0 ) {
53+
align = {
54+
x: parseFloat( ar[ 0 ] ),
55+
y: ar.length > 1 ? parseFloat( ar[ 1 ] ) : align.x
56+
};
57+
}
58+
59+
if ( align && !( isNaN( align.x ) || isNaN( align.y ) ) ) {
60+
return {
61+
x: this._alignmentToCoeffs( align.x ),
62+
y: this._alignmentToCoeffs( align.y )
63+
};
64+
}
65+
},
66+
67+
_setOptions: function( options ) {
68+
var linkOffset, linkSize;
69+
70+
this._super( options );
71+
72+
if ( this._isOpen &&
73+
options.align !== undefined &&
74+
this._ui.link !== null &&
75+
( this._ui.link.jqmData( "position-to" ) === "origin" ||
76+
this.options.positionTo === "origin" ) ) {
77+
linkOffset = this._ui.link.offset();
78+
linkSize = {
79+
cx: this._ui.link.outerWidth(),
80+
cy: this._ui.link.outerHeight()
81+
};
82+
83+
this._reposition({
84+
x: linkOffset.left + linkSize.cx / 2,
85+
y: linkOffset.top + linkSize.cy / 2,
86+
positionTo: "origin"
87+
});
88+
}
89+
},
90+
91+
_alignedCoord: function( start, coeffs, originSize, popupSize ) {
92+
return (
93+
94+
// Start at the origin
95+
start +
96+
97+
// Apply lignment
98+
coeffs.originCoeff * originSize + coeffs.popupCoeff * popupSize +
99+
100+
// Resulting coordinate needs to be that of the middle of the popup, so
101+
// add half a popup width
102+
popupSize / 2 );
103+
},
104+
105+
_desiredCoords: function( options ) {
106+
var linkBox, offset, clampInfo,
107+
alignment = this._getAlignment();
108+
109+
if ( alignment && options.positionTo === "origin" && this._ui.link ) {
110+
111+
// Grab the size of the popup and the offset and size of the link
112+
clampInfo = this._clampPopupWidth( true );
113+
clampInfo.menuSize.cx = Math.min( clampInfo.menuSize.cx, clampInfo.rc.cx );
114+
offset = this._ui.link.offset();
115+
linkBox = {
116+
x: offset.left,
117+
y: offset.top,
118+
cx: this._ui.link.outerWidth(),
119+
cy: this._ui.link.outerHeight()
120+
};
121+
122+
// Determine the desired coordinates of the middle of the popup
123+
options.x = this._alignedCoord( linkBox.x, alignment.x, linkBox.cx, clampInfo.menuSize.cx );
124+
options.y = this._alignedCoord( linkBox.y, alignment.y, linkBox.cy, clampInfo.menuSize.cy );
125+
}
126+
127+
return this._super( options );
128+
}
129+
130+
});
131+
132+
})( jQuery );

demos/nav-examples.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
<li data-section="Demo Showcase" data-filtertext="arrow popups popover"><a href="examples/popups/arrow.php" data-ajax="false">Popup with arrow</a></li>
4545

46+
<li data-section="Demo Showcase" data-filtertext="popups popup position alignment"><a href="examples/popups/alignment.php" data-ajax="false">Popup alignment</a></li>
47+
4648
<li data-role="list-divider">Responsive Tables</li>
4749

4850
<li data-section="Demo Showcase" data-filtertext="responsive tables reflow stack custom styles"><a href="examples/tables/movie-list.php" data-ajax="false">Reflow: Custom styles</a></li>

0 commit comments

Comments
 (0)