Commit ded44247 authored by Eric Rasmussen's avatar Eric Rasmussen
Browse files

[gh-546] Rewrite unl.js to use suggested Drupal jQuery pattern

parent cf614a1b
......@@ -7,3 +7,5 @@ configure = admin/config/system/unl
files[] = unl.install
files[] = unl.module
scripts[] = unl.js
/**
* On IMCE pages, modify IMCE to return relative paths when a base tag is being used.
* @file unl.js
*
* Some basic behaviors and utility functions for Views.
*/
jQuery('document').ready(function() {
// If this isn't an IMCE page, we don't need to do anything.
if (typeof(imce) == 'undefined') {
return;
}
// If we aren't using a base tag, we don't need to do anything.
if (!Drupal.settings.unl.use_base_tag) {
return;
}
// Change imce's base url to the current directory.
Drupal.settings.imce.furl = ".";
// Override imce's getURL method to remove a trailing ./ from URLs.
imce.realGetURL = imce.getURL;
imce.getURL = function(fid) {
var url = imce.realGetURL(fid);
if (url.substr(0, 2) == './') {
url = url.substr(2);
}
return url;
};
});
(function ($) {
jQuery('document').ready(function() {
// if there's no base tag on the page, we don't have to worry about this
if (jQuery('base').length == 0) {
return;
}
jQuery('a').click(function(e) {
// if this link has a hash tag
if (!this.href.split('#')[1]) {
return;
}
// and it is specifically for this page
if (this.getAttribute('href').split('#')[0] != '') {
return;
}
// and something else hasn't already customized the link
if (e.isDefaultPrevented()) {
return;
}
Drupal.behaviors.unl = {
attach: function (context, settings) {
if (!Drupal.settings.unl.use_base_tag) {
return;
}
// fix clicking the link so that it ignores the base tag
e.preventDefault();
document.location.hash = this.href.split('#')[1];
});
});
// Modify IMCE to return relative paths when a base tag is present.
if (typeof(imce) !== 'undefined') {
// Change IMCE base url to the current directory.
Drupal.settings.imce.furl = ".";
// Override IMCE getURL method to remove a trailing ./ from URLs.
imce.realGetURL = imce.getURL;
imce.getURL = function(fid) {
var url = imce.realGetURL(fid);
if (url.substr(0, 2) == './') {
url = url.substr(2);
}
return url;
};
}
// Make links ignore the base tag.
$('a').click(function(e) {
// If this link has a hash tag,
if (!this.href.split('#')[1]) {
return;
}
// and it is specifically for this page,
if (this.getAttribute('href').split('#')[0] != '') {
return;
}
// and something else hasn't already customized the link,
if (e.isDefaultPrevented()) {
return;
}
e.preventDefault();
document.location.hash = this.href.split('#')[1];
});
}
};
})(jQuery);
......@@ -886,7 +886,7 @@ function unl_init() {
drupal_add_js(array('unl' => array('use_base_tag' => variable_get('unl_use_base_tag', TRUE))), 'setting');
// Add JS
drupal_add_js('sites/all/modules/unl/unl.js');
drupal_add_js(drupal_get_path('module', 'unl') . '/unl.js', array('weight' => 50));
// Handle unneeded index.html references
_unl_handle_directory_index();
......
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