|
| 1 | +(function( $, undefined ) { |
| 2 | + //special click handling to make widget work remove after nav changes in 1.4 |
| 3 | + var href, |
| 4 | + ele = ""; |
| 5 | + $( document ).on( "click", "a", function( e ) { |
| 6 | + href = $( this ).attr( "href" ); |
| 7 | + var hash = $.mobile.path.parseUrl( href ); |
| 8 | + if( typeof href !== "undefined" && hash !== "" && href !== href.replace( hash,"" ) && hash.search( "/" ) !== -1 ){ |
| 9 | + //remove the hash from the link to allow normal loading of the page. |
| 10 | + var newHref = href.replace( hash,"" ); |
| 11 | + $( this ).attr( "href", newHref ); |
| 12 | + } |
| 13 | + ele = $( this ); |
| 14 | + }); |
| 15 | + $( document ).on( "pagebeforechange", function( e, f ){ |
| 16 | + f.originalHref = href; |
| 17 | + }); |
| 18 | + $( document ).on("pagebeforechange", function( e,f ){ |
| 19 | + var hash = $.mobile.path.parseUrl(f.toPage).hash, |
| 20 | + hashEl, hashElInPage; |
| 21 | + |
| 22 | + try { |
| 23 | + hashEl = $( hash ); |
| 24 | + } catch( e ) { |
| 25 | + hashEl = $(); |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + hashElInPage = $( ".ui-page-active " + hash ); |
| 30 | + } catch( e ) { |
| 31 | + hashElInPage = $(); |
| 32 | + } |
| 33 | + |
| 34 | + if( typeof hash !== "undefined" && |
| 35 | + hash.search( "/" ) === -1 && |
| 36 | + hash !== "" && |
| 37 | + hashEl.length > 0 && |
| 38 | + !hashEl.hasClass( "ui-page" ) && |
| 39 | + hashEl.data('role') !== "page" && |
| 40 | + !hashElInPage.hasClass( "ui-panel" ) && |
| 41 | + !hashElInPage.hasClass( "ui-popup" ) ) { |
| 42 | + //scroll to the id |
| 43 | + var pos = hashEl.offset().top; |
| 44 | + $.mobile.silentScroll( pos ); |
| 45 | + $.mobile.navigate( hash, '', true ); |
| 46 | + } else if( typeof f.toPage !== "object" && |
| 47 | + hash !== "" && |
| 48 | + $.mobile.path.parseUrl( href ).hash !== "" && |
| 49 | + !hashEl.hasClass( "ui-page" ) && hashEl.attr('data-role') !== "page" && |
| 50 | + !hashElInPage.hasClass( "ui-panel" ) && |
| 51 | + !hashElInPage.hasClass( "ui-popup" ) ) { |
| 52 | + $( ele ).attr( "href", href ); |
| 53 | + $.mobile.document.one( "pagechange", function() { |
| 54 | + if( typeof hash !== "undefined" && |
| 55 | + hash.search( "/" ) === -1 && |
| 56 | + hash !== "" && |
| 57 | + hashEl.length > 0 && |
| 58 | + hashElInPage.length > 0 && |
| 59 | + !hashEl.hasClass( "ui-page" ) && |
| 60 | + hashEl.data('role') !== "page" && |
| 61 | + !hashElInPage.hasClass( "ui-panel" ) && |
| 62 | + !hashElInPage.hasClass( "ui-popup" ) ) { |
| 63 | + hash = $.mobile.path.parseUrl( href ).hash; |
| 64 | + var pos = hashElInPage.offset().top; |
| 65 | + $.mobile.silentScroll( pos ); |
| 66 | + } |
| 67 | + } ); |
| 68 | + } |
| 69 | + }); |
| 70 | + $( document ).on( "mobileinit", function(){ |
| 71 | + hash = window.location.hash; |
| 72 | + $.mobile.document.one( "pageshow", function(){ |
| 73 | + var hashEl, hashElInPage; |
| 74 | + |
| 75 | + try { |
| 76 | + hashEl = $( hash ); |
| 77 | + } catch( e ) { |
| 78 | + hashEl = $(); |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + hashElInPage = $( ".ui-page-active " + hash ); |
| 83 | + } catch( e ) { |
| 84 | + hashElInPage = $(); |
| 85 | + } |
| 86 | + |
| 87 | + if( hash !== "" && |
| 88 | + hashEl.length > 0 && |
| 89 | + hashElInPage.length > 0 && |
| 90 | + hashEl.attr('data-role') !== "page" && |
| 91 | + !hashEl.hasClass( "ui-page" ) && |
| 92 | + !hashElInPage.hasClass( "ui-panel" ) && |
| 93 | + !hashElInPage.hasClass( "ui-popup" ) && |
| 94 | + !hashEl.is( "body" ) ){ |
| 95 | + var pos = hashElInPage.offset().top; |
| 96 | + setTimeout( function(){ |
| 97 | + $.mobile.silentScroll( pos ); |
| 98 | + }, 100 ); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + //h2 widget |
| 103 | + $( document ).on( "mobileinit", function(){ |
| 104 | + $.widget( "mobile.h2linker", $.mobile.widget, { |
| 105 | + options:{ |
| 106 | + initSelector: ":jqmData(quicklinks='true')" |
| 107 | + }, |
| 108 | + |
| 109 | + _create:function(){ |
| 110 | + var self = this, |
| 111 | + bodyid = "ui-page-top", |
| 112 | + panel = "<div data-role='panel' class='jqm-nav-panel jqm-quicklink-panel' data-position='right' data-display='overlay' data-theme='c'><ul data-role='listview' data-inset='false' data-theme='d' data-divider-theme='d' data-icon='false' class='jqm-list'><li data-role='list-divider'>Jump to section</li></ul></div>", |
| 113 | + first = true, |
| 114 | + h2dictionary = new Object(); |
| 115 | + if(typeof $("body").attr("id") === "undefined"){ |
| 116 | + $("body").attr("id",bodyid); |
| 117 | + } else { |
| 118 | + bodyid = $("body").attr("id"); |
| 119 | + } |
| 120 | + this.element.find("div.jqm-content>h2").each(function(){ |
| 121 | + var id, text = $(this).text(); |
| 122 | + |
| 123 | + if(typeof $(this).attr("id") === "undefined"){ |
| 124 | + id = text.replace(/[^\.a-z0-9:_-]+/gi,""); |
| 125 | + $(this).attr( "id", id ); |
| 126 | + } else { |
| 127 | + id = $(this).attr("id"); |
| 128 | + } |
| 129 | + |
| 130 | + h2dictionary[id] = text; |
| 131 | + if(!first){ |
| 132 | + $(this).before( "<a href='#" + bodyid + "' class='jqm-deeplink ui-link'>Return to top<span class='ui-icon ui-icon-arrow-u'> </span></a>"); |
| 133 | + } else { |
| 134 | + $(this).before("<a href='#' data-ajax='false' class='jqm-deeplink ui-link jqm-open-quicklink-panel'>Jump to section<span class='ui-icon ui-icon-bars'> </span></a>"); |
| 135 | + } |
| 136 | + first = false; |
| 137 | + }); |
| 138 | + this._on(".jqm-open-quicklink-panel", { |
| 139 | + "click": function(){ |
| 140 | + $(".ui-page-active .jqm-quicklink-panel").panel("open"); |
| 141 | + return false; |
| 142 | + } |
| 143 | + }); |
| 144 | + this._on( document, { |
| 145 | + "pagebeforechange": function(){ |
| 146 | + this.element.find(".jqm-quicklink-panel").panel("close"); |
| 147 | + this.element.find(".jqm-quicklink-panel .ui-btn-active").removeClass("ui-btn-active"); |
| 148 | + } |
| 149 | + }); |
| 150 | + if( $(h2dictionary).length > 0 ){ |
| 151 | + this.element.prepend(panel) |
| 152 | + this.element.find(".jqm-quicklink-panel").panel().find("ul").listview(); |
| 153 | + } |
| 154 | + $.each(h2dictionary,function(id,text){ |
| 155 | + self.element.find(".jqm-quicklink-panel ul").append("<li><a href='#"+id+"'>"+text+"</a></li>"); |
| 156 | + }); |
| 157 | + self.element.find(".jqm-quicklink-panel ul").listview("refresh"); |
| 158 | + |
| 159 | + |
| 160 | + } |
| 161 | + }); |
| 162 | + }); |
| 163 | + $( document ).bind( "pagecreate create", function( e ) { |
| 164 | + var initselector = $.mobile.h2linker.prototype.options.initSelector; |
| 165 | + if($(e.target).data("quicklinks")){ |
| 166 | + $(e.target).h2linker(); |
| 167 | + } |
| 168 | + }); |
| 169 | +})( jQuery ); |
0 commit comments