Commit c805f70f authored by Kevin Abel's avatar Kevin Abel
Browse files

Convert JS into AMD for loading through requirejs

parent bb2313dd
(function(window) {
define(['jquery', 'analytics'], function ($, analytics) {
"use strict";
var
initCallback = 'searchInit',
// Service server (defaults to //directory.unl.edu)
directoryServer = null,
unlContext = '015236299699564929946:nk1siew10ie',
transitionDelay = 400,
inputSel = '#search_q',
formSel = '#searchform form',
resultSel = '.search-results',
googleSel = '.google-results',
var directoryServer = null;
evtStateChange = 'statechange',
wrapperMain = '#search_wrapper',
wrapperWeb = '#search_results',
wrapperDir = '#directory_results',
var unlContext = '015236299699564929946:nk1siew10ie';
dirResults = 'ppl_results',
unlResults = 'unl_results',
localResults = 'local_results';
var transitionDelay = 400;
window[initCallback] = function() {
window[initCallback] = null;
require(['jquery', 'analytics'], function($, analytics) {
// Caching Class
var Cache = function() {
this.storage = {};
};
Cache.prototype.get = function(key) {
return this.storage[key] || undefined;
};
Cache.prototype.save = function(key, value) {
this.storage[key] = value;
return this;
};
var inputSel = '#search_q';
var formSel = '#searchform form';
var resultSel = '.search-results';
var googleSel = '.google-results';
// Directory Controller Class
var Directory = function(server, containerId) {
var cntSel = '#' + containerId;
this._server = server || '//directory.unl.edu';
this._cache = new Cache();
this._searchCanceled = false;
this._viewState = 0;
this._renderTo = cntSel;
$(function() {
$(cntSel).on('click', '.fn a', function() {
if (this.target !== '_blank') {
this.target = '_blank';
}
});
});
};
Directory.prototype._render = function(data) {
if (this._searchCanceled) {
return;
}
var evtStateChange = 'statechange';
var wrapperMain = '#search_wrapper';
var wrapperWeb = '#search_results';
var wrapperDir = '#directory_results';
$(this._renderTo)
.html(data)
.addClass('active');
var dirResults = 'ppl_results';
var unlResults = 'unl_results';
var localResults = 'local_results';
this._renderState(0);
};
Directory.prototype._renderState = function(duration) {
var $innerRes = $('.results', $(this._renderTo)),
$showRes, failState,
depFilter = '.departments';
if (!$innerRes.length) {
return;
}
window.pf_getUID = function() {
return true;
};
$innerRes.slideUp(duration);
if (this._viewState === 0) {
$showRes = $innerRes.not(depFilter);
failState = 1;
} else {
$showRes = $innerRes.filter(depFilter);
failState = 0;
}
// Caching Class
var Cache = function() {
this.storage = {};
};
Cache.prototype.get = function(key) {
return this.storage[key] || undefined;
};
Cache.prototype.save = function(key, value) {
this.storage[key] = value;
return this;
};
if (!$showRes.length && typeof duration !== "undefined") {
this.changeViewState(failState);
return;
}
// Directory Controller Class
var Directory = function(server, containerId) {
var cntSel = '#' + containerId;
$showRes.slideDown();
};
Directory.prototype.cancelSearch = function() {
this._searchCanceled = true;
if (this._xhr) {
this._xhr.abort();
}
};
Directory.prototype.execute = function(q) {
var cacheData = this._cache.get(q),
self = this;
this._server = server || '//directory.unl.edu';
this._cache = new Cache();
this._searchCanceled = false;
this._viewState = 0;
this._renderTo = cntSel;
this._searchCanceled = false;
if (this._xhr) {
this._xhr.abort();
$(function() {
$(cntSel).on('click', '.fn a', function() {
if (this.target !== '_blank') {
this.target = '_blank';
}
});
});
};
Directory.prototype._render = function(data) {
if (this._searchCanceled) {
return;
}
if (cacheData) {
this._render(cacheData);
} else {
this._xhr = $.get(this._server + '/service.php?q=' + encodeURIComponent(q), function(data) {
self._cache.save(q, data);
self._render(data);
});
}
};
Directory.prototype.changeViewState = function(state) {
if (this._viewState == state) {
return;
}
$(this._renderTo)
.html(data)
.addClass('active');
var prevState = this._viewState;
this._renderState(0);
};
Directory.prototype._renderState = function(duration) {
var $innerRes = $('.results', $(this._renderTo)),
$showRes, failState,
depFilter = '.departments';
if (!$innerRes.length) {
return;
}
$innerRes.slideUp(duration);
if (this._viewState === 0) {
$showRes = $innerRes.not(depFilter);
failState = 1;
} else {
$showRes = $innerRes.filter(depFilter);
failState = 0;
}
if (!$showRes.length && typeof duration !== "undefined") {
this.changeViewState(failState);
return;
}
$showRes.slideDown();
};
Directory.prototype.cancelSearch = function() {
this._searchCanceled = true;
if (this._xhr) {
this._xhr.abort();
}
};
Directory.prototype.execute = function(q) {
var cacheData = this._cache.get(q),
self = this;
this._searchCanceled = false;
if (this._xhr) {
this._xhr.abort();
}
if (cacheData) {
this._render(cacheData);
} else {
this._xhr = $.get(this._server + '/service.php?q=' + encodeURIComponent(q), function(data) {
self._cache.save(q, data);
self._render(data);
});
}
};
Directory.prototype.changeViewState = function(state) {
if (this._viewState == state) {
return;
}
this._viewState = state;
this._renderState();
var prevState = this._viewState;
$(this._renderTo).trigger(evtStateChange, [state, prevState]);
};
Directory.prototype.clearAllResults = function() {
$(this._renderTo).empty();
};
this._viewState = state;
this._renderState();
var
// query related
query = '',
firstQ = window['INITIAL_QUERY'],
$(this._renderTo).trigger(evtStateChange, [state, prevState]);
};
Directory.prototype.clearAllResults = function() {
$(this._renderTo).empty();
};
actCls = 'active',
return {
initialize: function(firstQ, localContext) {
// query related
var query = '';
var actCls = 'active';
// CustomSearchControl instances and config
unlSearch,
localSearch,
activeSearch,
directorySearch,
localContext = window['LOCAL_SEARCH_CONTEXT'],
drawOp = new google.search.DrawOptions(),
searchToggleLock = false,
trackQuery = function(q) {
var unlSearch;
var localSearch;
var activeSearch;
var directorySearch;
var drawOp = new google.search.DrawOptions();
var searchToggleLock = false;
var trackQuery = function(q) {
var loc = window.location,
qs = loc.search.replace(/(?:(\?)|&)q=[^&]*(?:&|$)/, '$1'),
page = [
......@@ -167,8 +162,9 @@
if (window.history.pushState) {
window.history.pushState({query: q}, '', page);
}
},
queryComplete = function(control) {
};
var queryComplete = function(control) {
var $root = $(control.root);
// a11y patching
......@@ -188,21 +184,22 @@
$root.closest(googleSel).slideDown();
searchToggleLock = false;
},
fullQuery = function(q, track) {
};
var fullQuery = function(q, track) {
if (track !== false) {
trackQuery(q);
}
try {
activeSearch.execute(q, undefined, {});
} catch (e) {
console && console.log(e);
queryComplete(activeSearch);
}
directorySearch.execute(q);
$(wrapperMain).fadeIn();
},
fullStop = function() {
};
var fullStop = function() {
activeSearch.cancelSearch();
directorySearch.cancelSearch();
$(resultSel).removeClass(actCls);
......@@ -211,14 +208,15 @@
activeSearch.clearAllResults();
directorySearch.clearAllResults();
}, transitionDelay);
},
queryStart = function(control, searcher, q) {
};
var queryStart = function(control, searcher, q) {
$(control.root).closest(googleSel).slideUp(0);
if (q !== query) {
trackQuery(q);
directorySearch.execute(q);
}
};
};
drawOp.enableSearchResultsOnly();
......@@ -361,7 +359,7 @@
passiveQuery(q, false);
});
if (window.parent != undefined) {
if (window.parent) {
$(document).on('keydown', function(e) {
if (e.keyCode === 27) {
window.parent.postMessage('wdn.search.close', "*");
......@@ -369,11 +367,6 @@
});
}
});
});
};
window['pf_getUID'] = function() {
return true;
}
};
}(window));
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment