forked from jquery-archive/jquery-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqm-demos.js
372 lines (315 loc) · 10.8 KB
/
jqm-demos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Turn off Ajax for local file browsing
if ( location.protocol.substr(0,4) === 'file' ||
location.protocol.substr(0,11) === '*-extension' ||
location.protocol.substr(0,6) === 'widget' ) {
// Start with links with only the trailing slash and that aren't external links
var fixLinks = function() {
$( "a[href$='/'], a[href='.'], a[href='..']" ).not( "[rel='external']" ).each( function() {
if( !$( this ).attr( "href" ).match("http") ){
this.href = $( this ).attr( "href" ).replace( /\/$/, "" ) + "/index.html";
}
});
};
// Fix the links for the initial page
$( fixLinks );
// Fix the links for subsequent ajax page loads
$( document ).on( "pagecreate", fixLinks );
// Check to see if ajax can be used. This does a quick ajax request and blocks the page until its done
$.ajax({
url: '.',
async: false,
isLocal: true
}).error(function() {
// Ajax doesn't work so turn it off
$( document ).on( "mobileinit", function() {
$.mobile.ajaxEnabled = false;
var message = $( '<div>' , {
'class': "jqm-content",
style: "border:none; padding: 10px 15px; overflow: auto;",
'data-ajax-warning': true
});
message
.append( "<h3>Note: Navigation may not work if viewed locally</h3>" )
.append( "<p>The Ajax-based navigation used throughout the jQuery Mobile docs may need to be viewed on a web server to work in certain browsers. If you see an error message when you click a link, please try a different browser.</p>" );
$( document ).on( "pagecreate", function( event ) {
$( event.target ).append( message );
});
});
});
}
$( document ).on( "pagecreate", ".jqm-demos", function( event ) {
var search,
page = $( this ),
searchUrl = ( $( this ).hasClass( "jqm-home" ) ) ? "_search/" : "../_search/",
searchContents = $( ".jqm-search-panel ul.jqm-search-list" ).find( "li[data-filtertext]" ),
version = $.mobile.version || "Dev",
words = version.split( "-" ),
ver = words[0],
str = words[1] || "",
text = ver,
versionNumbers = ver.split( "." ),
apiVersion = versionNumbers[ 0 ] + "." + versionNumbers[ 1 ],
href;
// Insert jqm version in header
if ( str.indexOf( "rc" ) == -1 ) {
str = str.charAt( 0 ).toUpperCase() + str.slice( 1 );
} else {
str = str.toUpperCase().replace( ".", "" );
}
if ( $.mobile.version && str ) {
text += " " + str;
}
if ( "@VERSION" === $.mobile.version ) {
text = version = "Dev";
}
$( ".jqm-version" ).html( text );
// Insert version in API documentation links
if ( version !== "Dev" ) {
$( ".jqm-api-docs-link" ).each(function() {
href = $( this ).attr( "href" ).replace( "api.jquerymobile.com/", "api.jquerymobile.com/" + apiVersion + "/" );
$( this ).attr( "href", href );
});
}
// Global navmenu panel
$( ".jqm-navmenu-panel ul" ).listview();
$( ".jqm-navmenu-panel ul" ).accordion({
"header": "> li > h3",
"collapsible": true,
"active": false,
"heightStyle": "content",
"icons": {
"header": "ui-icon-plus",
"activeHeader": "ui-icon-minus"
}
});
// Collapse nested accordions when their parent is being collapsed.
$( ".jqm-navmenu-panel > .ui-panel-inner > .ui-accordion" ).on( "accordionbeforeactivate", function( event, ui ) {
var target = $( event.target );
if ( target.is( ".jqm-navmenu-panel > .ui-panel-inner > .ui-accordion" ) ) {
target.find( ".ui-accordion" ).accordion( "option", "active", false );
}
});
// Keyboard accessibility of the navmenu.
$( ".jqm-navmenu-panel .ui-accordion-header, .jqm-navmenu-panel .ui-listview-item-button" ).on( "keydown", function( event ) {
if ( event.which == 9 ) {
var target = $( event.target ),
parent = target.parent( "li" );
parent.next( "li" )
.add( parent.prev( "li" ) )
.children( "h3" )
.attr( "tabIndex", 0 );
}
});
// On panel demo pages copy the navmenu into the wrapper
if ( $( this ).is( '.jqm-panel-page' ) ) {
var wrapper = $( this ).children( '.ui-panel-wrapper' );
if ( wrapper ) {
$( '.jqm-navmenu-panel' ).clone( true, true ).appendTo( wrapper );
}
}
$( ".jqm-navmenu-link" ).on( "click", function() {
page.find( ".jqm-navmenu-panel" ).panel( "open" );
});
// Turn off autocomplete / correct for demos search
$( this ).find( ".jqm-search input" ).attr( "autocomplete", "off" ).attr( "autocorrect", "off" );
// Global search
// Initalize search panel
$( ".jqm-search-panel" ).panel({
position: "right",
display: "overlay",
theme: "a",
});
$( ".jqm-search-link" ).on( "click", function() {
$( "body" ).find( ".jqm-search-panel" ).panel( "open" );
$( ".ui-page-active" ).addClass( "jqm-demos-search-panel-open" );
});
$( document ).on( "panelopen", ".jqm-search-panel", function() {
$( this ).find( ".jqm-search-input" ).focus();
})
// Initalize search panel list and filter
$( ".jqm-search-panel ul.jqm-search-list" ).html( searchContents ).listview({
inset: false,
theme: null,
dividerTheme: null,
icon: false,
autodividers: true,
autodividersSelector: function ( li ) {
return "";
},
arrowKeyNav: true,
enterToNav: true,
highlight: true,
submitTo: searchUrl
}).filterable();
// Initalize search page list
$( this ).find( ".jqm-search-results-wrap ul.jqm-search-list" ).html( searchContents ).listview({
inset: false,
theme: null,
dividerTheme: null,
icon: false,
arrowKeyNav: true,
enterToNav: true,
highlight: true
}).filterable();
// Search results page get search query string and enter it into filter then trigger keyup to filter
if ( $( event.target ).hasClass( "jqm-demos-search-results" ) ) {
search = $.mobile.path.parseUrl( window.location.href ).search.split( "=" )[ 1 ];
setTimeout(function() {
e = $.Event( "keyup" );
e.which = 65;
$( this ).find( "#jqm-search-results-input" ).val( search ).trigger(e).trigger( "change" );
}, 0 );
}
// Fix links on homepage to point to sub directories
if ( $( event.target ).hasClass( "jqm-home") ) {
$( this ).find( "a" ).each( function() {
$( this ).attr( "href", $( this ).attr( "href" ).replace( "../", "" ) );
});
}
});
// Append keywords list to each list item
$( document ).one( "pagecreate", ".jqm-demos", function( event ) {
$( ".jqm-search-results-list li, .jqm-search li" ).each(function() {
var text = $( this ).attr( "data-filtertext" );
$( this )
.find( "a" )
.append( "<span class='jqm-search-results-keywords ui-listview-item-description'>" +
text + "</span>" );
});
});
// Functions for highlighting text used for keywords highlight in search
jQuery.fn.highlight = function( pat ) {
function innerHighlight( node, pat ) {
var skip = 0;
if ( node.nodeType == 3 ) {
var pos = node.data.toUpperCase().indexOf( pat );
if ( pos >= 0 ) {
var spannode = document.createElement( "span" );
spannode.className = "jqm-search-results-highlight";
var middlebit = node.splitText( pos );
var endbit = middlebit.splitText( pat.length );
var middleclone = middlebit.cloneNode( true );
spannode.appendChild( middleclone );
middlebit.parentNode.replaceChild( spannode, middlebit );
skip = 1;
}
} else if ( node.nodeType == 1 && node.childNodes && !/(script|style)/i.test( node.tagName ) ) {
for ( var i = 0; i < node.childNodes.length; ++i ) {
i += innerHighlight( node.childNodes[i], pat );
}
}
return skip;
}
return this.length && pat && pat.length ? this.each(function() {
innerHighlight( this, pat.toUpperCase() );
}) : this;
};
// Function to remove highlights in text
jQuery.fn.removeHighlight = function() {
return this.find( "span.jqm-search-results-highlight" ).each(function() {
this.parentNode.firstChild.nodeName;
with ( this.parentNode ) {
replaceChild( this.firstChild, this );
normalize();
}
}).end();
};
// Extension to listview to add keyboard navigation
$( document ).on( "mobileinit", function() {
(function( $, undefined ) {
$.widget( "mobile.listview", $.mobile.listview, {
options: {
arrowKeyNav: false,
enterToNav: false,
highlight: false,
submitTo: false
},
_create: function() {
this._super();
if ( this.options.arrowKeyNav ) {
this._on( document, { "pageshow": "arrowKeyNav" });
}
if ( this.options.enterToNav ) {
this._on( document, { "pageshow": "enterToNav" });
}
},
submitTo: function() {
var url,
form = this.element.parent().find( "form" );
form.attr( "method", "get" )
.attr( "action", this.options.submitTo );
url = this.options.submitTo + "?search=" + this.element.parent().find( "input" ).val();
window.location = url;
},
enterToNav: function() {
var form = this.element.parent().find( "form" );
form.append( "<button type='submit' data-icon='caret-r' data-inline='true' class='ui-hidden-accessible' data-iconpos='notext'>Submit</button>" )
.parent()
.enhanceWithin();
this.element.parent().find( "form" ).children( ".ui-button" ).addClass( "ui-hidden-accessible" );
this._on( form, {
"submit": "submitHandler"
});
},
enhanced: false,
arrowKeyNav: function() {
var input = this.element.prev("form").find( "input" );
if ( !this.enhanced ) {
this._on( input, {
"keyup": "handleKeyUp"
});
this.enhanced = true;
}
},
handleKeyUp: function( e ) {
var search,
toBeHighlightled,
input = this.element.prev("form").find( "input" ),
isDownKeyUp = e.which === $.ui.keyCode.DOWN,
isUpKeyUp = e.which === $.ui.keyCode.UP;
if ( isDownKeyUp || isUpKeyUp ) {
if ( this.element.find( "li.ui-listview-item-active" ).length === 0 ) {
toBeHighlightled = this.element.find( "li" )
.not( ".ui-screen-hidden" )
[ isDownKeyUp ? "first" : "last" ]();
} else {
this.element.find( "li.ui-listview-item-active a" )
.toggleClass( "ui-button-active");
toBeHighlightled = this.element.find( "li.ui-listview-item-active" )
.toggleClass( "ui-listview-item-active" )
[ isDownKeyUp ? "nextAll" : "prevAll" ]( "li" )
.not( ".ui-screen-hidden" )
.first();
}
// Highlight the selected list item
toBeHighlightled
.toggleClass( "ui-listview-item-active" )
.find( "a" )
.toggleClass( "ui-button-active" );
} else if ( e.which === $.ui.keyCode.ENTER ) {
this.submitHandler();
} else if ( typeof e.which !== "undefined" ) {
this.element.find( "li.ui-listview-item-active" )
.removeClass( "ui-listview-item-active" );
if ( this.options.highlight ) {
search = input.val();
this.element.find( "li" ).each(function() {
$( this ).removeHighlight();
$( this ).highlight( search );
});
}
}
},
submitHandler: function() {
if ( this.element.find( "li.ui-listview-item-active" ).length !== 0 ) {
var href = this.element.find( "li.ui-listview-item-active a" ).attr( "href" );
$( ":mobile-pagecontainer" ).pagecontainer( "change", href );
return false;
}
if ( this.options.submitTo ) {
this.submitTo();
}
}
});
})( jQuery );
});