Page MenuHomePhabricator

application2.js

Authored By
bzimport
Nov 21 2014, 11:51 PM
Size
7 KB
Referenced Files
None
Subscribers
None

application2.js

// this replaces MobileFrontend/javascripts/opensearch.js and MobileFrontend/javascripts/application.js.
// Assumes that this is in the head, and that pageLoaded is called from the bottom of that body, and
// that #logo has onclick="logoClick()" in the html, and that #results is available.
// handle clicking on headers to expand sections and clicking on links that lead to hidden sections
document.onclick = document.ontouchstart = function(e) {
e = e || event;
for( e = e.target || e.srcElement; e && !e.hash && e.className != "section_heading" && e.id != "results"; ) {
e = e.parentNode;
}
if( e && e.className == "section_heading") { // headers
wm_toggle_section( parseInt( e.id.replace( /section_(\d+)/, '$1' ) ) );
} else if( e && e.hash && e.hash.indexOf( '#' ) == 0 ) { // links
wm_reveal_for_hash( e.hash );
}
if( (!e || e.id != "results") && withResults() ) {
results.style.display = 'none';
}
}
document.onkeyup = function(e) {
e = e || event; e = e.target || e.srcElement;
if( e.id == "search") {
if ( !withResults() ){ // not yet loaded?
return;
}
if ( clearSearch ) {
clearSearch.style.display = search.value.length > 0 ? 'block' : results.style.display = 'none';
}
// from bottom
clearTimeout( timer );
var term = e.value;
if ( term.length < 1 ) {
results.innerHTML = '';
} else {
timer = setTimeout( function () { searchApi( term ); }, typingDelay );
}
}
}
var languageSelection, search, results, clearSearch;
function withResults() {
if(results){
return true;
}
results = document.getElementById('results');
clearSearch = document.getElementById('clearsearch');
search = document.getElementById('search');
return !!results;
}
// called from the bottom of the body
function pageLoaded() {
if ( document.location.hash.indexOf( '#' ) == 0 ) {
wm_reveal_for_hash( document.location.hash );
}
var sq = document.getElementById( 'sq' ),
sb = document.getElementById( 'searchbox' );
withResults(); // set variables
languageSelection = document.getElementById( 'languageselection' );
clearSearch.setAttribute('title', 'Clear'); // should start with correct title attribute
function updateSearchWidth() { // everything here but the results box could really be done just in the html
if ( sq && search && sb && results ) {
var iw = document.documentElement.clientWidth || document.body.clientWidth;
sb.style.width = ( iw - 30 ) + pixels;
sq.style.width = ( iw - 110 ) + pixels;
search.style.width = ( iw - 140 ) + pixels;
results.style.width = ( sq.offsetWidth - 2 ) + pixels;
results.style.left = sq.offsetLeft + pixels;
results.style.top = ( sq.offsetTop + sq.offsetHeight ) + pixels;
}
}
updateSearchWidth();
window.onorientationchange = function() {
switch( window.orientation ) {
case 0:
case 90:
case -90:
updateSearchWidth();
break;
}
}
function clearSearchBox() {
search.value = '';
clearSearch.style.display = results.style.display = 'none';
return false;
}
clearSearch.onmousedown = clearSearchBox;
// Try to scroll and hide URL bar
window.scrollTo( 0, 1 );
sq.parentNode.onsubmit = function(){
newHistSuggestion(search.value)
}
};
// Activated by "onclick='logoClick()'" in #logo html
function logoClick() {
var n = document.getElementById( 'nav' ).style;
n.display = n.display == 'block' ? 'none' : 'block';
if (n.display == 'block' && languageSelection && languageSelection.offsetWidth > 175 ) {
n.width = (languageSelection.offsetWidth + 30) + 'px';
}
};
// Activated by "onchange='javascript:navigateToLanguageSelection();'" in #languageSelection html
function navigateToLanguageSelection() {
if ( languageSelection ) {
var url = languageSelection.options[languageSelection.selectedIndex].value;
if ( url ) {
location.href = url;
}
}
}
function wm_reveal_for_hash(hash) {
var targetel = document.getElementById(hash.substr(1));
if (targetel) {
for (var p = targetel.parentNode; p && p.className != 'content_block' && p.className != 'section_heading';) {
p = p.parentNode;
}
if (p && p.style.display != 'block') {
var section_idx = parseInt(p.id.split('_')[1]);
wm_toggle_section(section_idx);
}
}
}
function wm_toggle_section(section_id) {
var b = document.getElementById('section_' + section_id),
bb = b.getElementsByTagName('button');
for (var i = 0; i <= 1; i++) {
var s = bb[i].style;
s.display = s.display == 'none' || (i && !s.display) ? 'inline-block' : 'none';
}
for (var i = 0, d = ['content_', 'anchor_']; i <= 1; i++) {
var s = document.getElementById(d[i] + section_id).style;
s.display = s.display == 'block' ? 'none' : 'block';
}
}
var apiUrl = (window.scriptPath?scriptPath:'/w')+'/api.php';
var timer = -1,
typingDelay = 500,
numResults = 5,
pixels = 'px',
maxHistSpace = 10;
function histSuggestions() {
var a=document.cookie.split("histsugg=");
return a.length < 2?'':'|'+a[1].split(";")[0];
}
function newHistSuggestion(term) {
if((histSuggestions()+"|").indexOf("|"+term+"|") == -1 && term){
document.cookie = "histsugg="+term+histSuggestions().split("|").slice(0, maxHistSpace ).join("|")+";path=/;expires="+(new Date(+new Date()+(3600000*24*30)).toGMTString())
}
}
function searchApi( term ) {
var xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject( 'Microsoft.XMLHTTP' );
for(var hist = histSuggestions().split("|").slice(1), histS = []; hist.length; hist.shift()) {
if(hist[0].toLowerCase().indexOf(term.toLowerCase()) == 0) {
histS.push(hist[0]);
}
}
histS.splice( numResults );
xmlHttp.onreadystatechange = function() {
if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) {
var response = histS.concat((window.JSON && JSON.parse ? JSON.parse : eval)( xmlHttp.responseText )[1]), dontDuplicate={};
results.style.display = 'block';
for(;results.firstChild;){results.removeChild(results.firstChild)}
if(response.length === 0){
results.appendChild( document.createTextNode( "No results" ) );
} else {
results.appendChild( document.createElement('div') ).className = "suggestions-results";
for( var i = 0; i < response.length; i++ ){
if( dontDuplicate[ response[i] ] ){
continue;
}
dontDuplicate[response[i]] = true;
(function(re){
var newdiv = results.lastChild.appendChild( document.createElement('div') ),
newlink = results.lastChild.appendChild( document.createElement('a') );
newdiv.appendChild( document.createTextNode(' + ') );
newdiv.onclick = function(){
search.value = re;
search.focus();
searchApi(re);
}
newlink.title = re;
newlink.href = '/wiki/' + re;
newlink.onclick = function(){ newHistSuggestion( re ) };
newlink.appendChild( document.createTextNode( re ) );
})(response[i])
}
}
}
}
var url = apiUrl + '?action=opensearch&limit=' + (numResults - histS.length) + '&namespace=0&format=json&search=' + term;
xmlHttp.open( 'GET', url, true );
xmlHttp.send();
}

File Metadata

Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
7634
Default Alt Text
application2.js (7 KB)

Event Timeline