/demos/_assets/js/h2widget.js
https://github.com/gabrielschulhof/jquery-mobile · JavaScript · 169 lines · 155 code · 10 blank · 4 comment · 18 complexity · 3d16913ca1be654a1f0058cef9215fa8 MD5 · raw file
- (function( $, undefined ) {
- //special click handling to make widget work remove after nav changes in 1.4
- var href,
- ele = "";
- $( document ).on( "click", "a", function() {
- href = $( this ).attr( "href" );
- var hash = $.mobile.path.parseUrl( href );
- if( typeof href !== "undefined" && hash !== "" && href !== href.replace( hash,"" ) && hash.search( "/" ) !== -1 ){
- //remove the hash from the link to allow normal loading of the page.
- var newHref = href.replace( hash,"" );
- $( this ).attr( "href", newHref );
- }
- ele = $( this );
- });
- $( document ).on( "pagebeforechange", function( e, f ){
- f.originalHref = href;
- });
- $( document ).on("pagebeforechange", function( e,f ){
- var hash = $.mobile.path.parseUrl(f.toPage).hash,
- hashEl, hashElInPage;
- try {
- hashEl = $( hash );
- } catch( err ) {
- hashEl = $();
- }
- try {
- hashElInPage = $( ".ui-page-active " + hash );
- } catch( err ) {
- hashElInPage = $();
- }
- if( typeof hash !== "undefined" &&
- hash.search( "/" ) === -1 &&
- hash !== "" &&
- hashEl.length > 0 &&
- !hashEl.hasClass( "ui-page" ) &&
- !hashEl.hasClass( "ui-popup" ) &&
- hashEl.data( "role" ) !== "page" &&
- !hashElInPage.hasClass( "ui-panel" ) &&
- !hashElInPage.hasClass( "ui-popup" ) ) {
- //scroll to the id
- var pos = hashEl.offset().top;
- $.mobile.silentScroll( pos );
- $.mobile.navigate( hash, "", true );
- } else if( typeof f.toPage !== "object" &&
- hash !== "" &&
- $.mobile.path.parseUrl( href ).hash !== "" &&
- !hashEl.hasClass( "ui-page" ) && hashEl.attr( "data-role" ) !== "page" &&
- !hashElInPage.hasClass( "ui-panel" ) &&
- !hashElInPage.hasClass( "ui-popup" ) ) {
- $( ele ).attr( "href", href );
- $.mobile.document.one( "pagechange", function() {
- if( typeof hash !== "undefined" &&
- hash.search( "/" ) === -1 &&
- hash !== "" &&
- hashEl.length > 0 &&
- hashElInPage.length > 0 &&
- !hashEl.hasClass( "ui-page" ) &&
- hashEl.data( "role" ) !== "page" &&
- !hashElInPage.hasClass( "ui-panel" ) &&
- !hashElInPage.hasClass( "ui-popup" ) ) {
- hash = $.mobile.path.parseUrl( href ).hash;
- var pos = hashElInPage.offset().top;
- $.mobile.silentScroll( pos );
- }
- } );
- }
- });
- $( document ).on( "mobileinit", function(){
- var hash = window.location.hash;
- $.mobile.document.one( "pageshow", function(){
- var hashEl, hashElInPage;
- try {
- hashEl = $( hash );
- } catch( e ) {
- hashEl = $();
- }
- try {
- hashElInPage = $( ".ui-page-active " + hash );
- } catch( e ) {
- hashElInPage = $();
- }
- if( hash !== "" &&
- hashEl.length > 0 &&
- hashElInPage.length > 0 &&
- hashEl.attr( "data-role" ) !== "page" &&
- !hashEl.hasClass( "ui-page" ) &&
- !hashElInPage.hasClass( "ui-panel" ) &&
- !hashElInPage.hasClass( "ui-popup" ) &&
- !hashEl.is( "body" ) ){
- var pos = hashElInPage.offset().top;
- setTimeout( function(){
- $.mobile.silentScroll( pos );
- }, 100 );
- }
- });
- });
- //h2 widget
- $( document ).on( "mobileinit", function(){
- $.widget( "mobile.h2linker", {
- options:{
- initSelector: ":jqmData(quicklinks='true')"
- },
- _create:function(){
- var self = this,
- bodyid = "ui-page-top",
- panel = "<div data-role='panel' class='jqm-quicklink-panel' data-position='left' data-display='overlay' data-theme='a'><ul data-role='listview' data-inset='false' data-theme='a' data-divider-theme='a' data-icon='false'><li data-role='list-divider'>Table of Contents</li></ul></div>",
- first = true,
- h2dictionary = {};
- if(typeof $("body").attr("id") === "undefined"){
- $("body").attr("id",bodyid);
- } else {
- bodyid = $("body").attr("id");
- }
- this.element.find("div.jqm-content>h2").each(function(){
- var id, text = $(this).text();
- if(typeof $(this).attr("id") === "undefined"){
- id = text.replace(/[^\.a-z0-9:_-]+/gi,"");
- $(this).attr( "id", id );
- } else {
- id = $(this).attr("id");
- }
- h2dictionary[id] = text;
- if(!first){
- $(this).before( "<div class='jqm-top-link-container'><span><a href='#" + bodyid + "' class='jqm-top-link ui-nodisc-icon ui-alt-icon'>Top <span class='ui-icon ui-icon-arrow-u'></span></a></span></div>");
- } else {
- $(this).before("<a href='#' data-ajax='false' class='jqm-toc-link jqm-open-quicklink-panel ui-nodisc-icon ui-alt-icon'><span class='ui-icon ui-icon-caret-r'></span> Table of Contents</a>");
- }
- first = false;
- });
- this._on(".jqm-open-quicklink-panel", {
- "click": function(){
- $(".ui-page-active .jqm-quicklink-panel").panel("open");
- $(".ui-page-active").addClass("jqm-demos-quicklink-panel-open");
- return false;
- }
- });
- this._on( document, {
- "pagebeforechange": function(){
- this.element.find(".jqm-quicklink-panel").panel("close");
- this.element.find(".jqm-quicklink-panel .ui-button-active").removeClass("ui-button-active");
- }
- });
- if( $(h2dictionary).length > 0 ){
- this.element.append(panel);
- this.element.find(".jqm-quicklink-panel").panel().find("ul").listview();
- }
- $.each(h2dictionary,function(id,text){
- self.element.find(".jqm-quicklink-panel ul").append("<li><a href='#"+id+"'>"+text+"</a></li>");
- });
- self.element.find(".jqm-quicklink-panel ul").listview("refresh");
- }
- });
- });
- $( document ).bind( "pagecreate create", function( e ) {
- if($(e.target).data("quicklinks")){
- $(e.target).h2linker();
- }
- });
- })( jQuery );