Untitled diff

Created Diff never expires
0 removals
204 lines
1 addition
205 lines
/**
/**
* Plugin: jquery.zGlossary
* Plugin: jquery.zGlossary
*
*
* Version: 1.0.2
* Version: 1.0.2
* (c) Copyright 2011-2013, Zazar Ltd
* (c) Copyright 2011-2013, Zazar Ltd
*
*
* Description: jQuery plugin to find and display term definitions in HTML text
* Description: jQuery plugin to find and display term definitions in HTML text
*
*
* History:
* History:
* 1.0.2 - Added show once option
* 1.0.2 - Added show once option
* 1.0.1 - Correct mistype to _addTerm function (Thanks Aldopaolo)
* 1.0.1 - Correct mistype to _addTerm function (Thanks Aldopaolo)
* 1.0.0 - Initial release
* 1.0.0 - Initial release
*
*
**/
**/
(function($){
(function($){
$.fn.glossary = function(url, options) {
$.fn.glossary = function(url, options) {
// Set plugin defaults
// Set plugin defaults
var defaults = {
var defaults = {
ignorecase: false,
ignorecase: false,
tiptag: 'h6',
tiptag: 'h6',
excludetags: [],
excludetags: [],
linktarget: '_blank',
linktarget: '_blank',
showonce: false
showonce: false
};
};
var options = $.extend(defaults, options);
var options = $.extend(defaults, options);
var id = 1;
var id = 1;
// Functions
// Functions
return this.each(function(i, e) {
return this.each(function(i, e) {
// Ensure any exclude tags are uppercase for comparisons
// Ensure any exclude tags are uppercase for comparisons
$.each(options.excludetags, function(i,e) { options.excludetags[i] = e.toUpperCase(); });
$.each(options.excludetags, function(i,e) { options.excludetags[i] = e.toUpperCase(); });
// Function to find and add term
// Function to find and add term
var _addTerm = function(e, term, type, def) {
var _addTerm = function(e, term, type, def) {
var patfmt = term;
var patfmt = term;
var skip = 0;
var skip = 0;
// Check the element is a text node
// Check the element is a text node
if (e.nodeType == 3) {
if (e.nodeType == 3) {
// Case insensistive matching option
// Case insensistive matching option
if (options.ignorecase) {
if (options.ignorecase) {
var pos = e.data.toLowerCase().indexOf(patfmt.toLowerCase());
var pos = e.data.toLowerCase().indexOf(patfmt.toLowerCase());
} else {
} else {
var pos = e.data.indexOf(patfmt);
var pos = e.data.indexOf(patfmt);
}
}
// Check if the term is found
// Check if the term is found
if (pos >= 0) {
if (pos >= 0) {
// Check for excluded tags
// Check for excluded tags
if (jQuery.inArray($(e).parent().get(0).tagName,options.excludetags) > -1) {
if (jQuery.inArray($(e).parent().get(0).tagName,options.excludetags) > -1) {
} else {
} else {
// Create link element
// Create link element
var spannode = document.createElement('a');
var spannode = document.createElement('a');
spannode.className = 'glossaryTerm';
spannode.className = 'glossaryTerm';
if (type == '0') {
if (type == '0') {
// Popup definition
// Popup definition
spannode.id = "glossaryID" + id;
spannode.id = "glossaryID" + id;
spannode.href = '#';
spannode.href = '#';
spannode.title = 'Click for \''+ term +'\' definition';
spannode.title = 'Click for \''+ term +'\' definition';
spannode.className = 'glossaryTerm';
spannode.className = 'glossaryTerm';
$(spannode).click(function(e) {
$(spannode).click(function(e) {
$.glossaryTip('<'+ options.tiptag +'>'+ term + '</'+ options.tiptag +'><p>'+ def +'</p>', {mouse_event: e})
$.glossaryTip('<'+ options.tiptag +'>'+ term + '</'+ options.tiptag +'><p>'+ def +'</p>', {mouse_event: e})
return false;
return false;
});
});
} else if (type == '1') {
} else if (type == '1') {
// Wikipedia definition
// Wikipedia definition
spannode.title = 'Click to look up \''+ term+'\' in Wikipedia';
spannode.title = 'Click to look up \''+ term+'\' in Wikipedia';
term = term.replace(/ /g, "_");
term = term.replace(/ /g, "_");
spannode.href = 'http://en.wikipedia.org/wiki/'+term;
spannode.href = 'http://en.wikipedia.org/wiki/'+term;
spannode.target = options.linktarget;
spannode.target = options.linktarget;
} else if (type == '2') {
} else if (type == '2') {
// Google search
// Google search
spannode.title = 'Click to search for \''+ term +'\' in Google';
spannode.title = 'Click to search for \''+ term +'\' in Google';
term = term.replace(/ /g, "+");
term = term.replace(/ /g, "+");
spannode.href = 'http://www.google.co.uk/search?q='+term;
spannode.href = 'http://www.google.co.uk/search?q='+term;
spannode.target = options.linktarget;
spannode.target = options.linktarget;
} else if (type == '3') {
} else if (type == '3') {
// Custom external link
// Custom external link
spannode.title = 'Click to view \''+ term +'\' definition';
spannode.title = 'Click to view \''+ term +'\' definition';
spannode.href = def;
spannode.href = def;
spannode.target = options.linktarget;
spannode.target = options.linktarget;
}
}
var middlebit = e.splitText(pos);
var middlebit = e.splitText(pos);
var endbit = middlebit.splitText(patfmt.length);
var endbit = middlebit.splitText(patfmt.length);
var middleclone = middlebit.cloneNode(true);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
skip = 1;
id += 1;
id += 1;
}
}
}
}
}
}
else if (e.nodeType == 1 && e.childNodes && !/(script|style)/i.test(e.tagName)) {
else if (e.nodeType == 1 && e.childNodes && !/(script|style)/i.test(e.tagName)) {
// Search child nodes
// Search child nodes
for (var i = 0; i < e.childNodes.length; ++i) {
for (var i = 0; i < e.childNodes.length; ++i) {
var ret = _addTerm(e.childNodes[i], term, type, def);
var ret = _addTerm(e.childNodes[i], term, type, def);
// If term found and show once option go to next term
// If term found and show once option go to next term
if (options.showonce && ret == 1) {
if (options.showonce && ret == 1) {
i = e.childNodes.length;
i = e.childNodes.length;
skip = 1;
skip = 1;
} else {
} else {
i += ret;
i += ret;
}
}
}
}
}
}
return skip;
return skip;
};
};
// Get glossary list items
// Get glossary list items
$.ajax({
$.ajax({
type: 'GET',
type: 'GET',
url: url,
url: url,
dataType: 'json',
dataType: 'json',
success: function(data) {
success: function(data) {
if (data) {
if (data) {
var count = data.length;
var count = data.length;
for (var i=0; i<count; i++) {
for (var i=0; i<count; i++) {
// Find term in text
// Find term in text
var item = data[i];
var item = data[i];
_addTerm(e, item.term, item.type, item.definition);
_addTerm(e, item.term, item.type, item.definition);
}
}
}
}
typeof options.callback == 'function' && options.callback();
},
},
error: function() {}
error: function() {}
});
});
});
});
};
};
// Glossary tip popup
// Glossary tip popup
var glossaryTip = function() {}
var glossaryTip = function() {}
$.extend(glossaryTip.prototype, {
$.extend(glossaryTip.prototype, {
setup: function(){
setup: function(){
if ($('#glossaryTip').length) {
if ($('#glossaryTip').length) {
$('#glossaryTip').remove();
$('#glossaryTip').remove();
}
}
glossaryTip.holder = $('<div id="glossaryTip" style="max-width:260px;"><div id="glossaryClose"></div></div>');
glossaryTip.holder = $('<div id="glossaryTip" style="max-width:260px;"><div id="glossaryClose"></div></div>');
glossaryTip.content = $('<div id="glossaryContent"></div>');
glossaryTip.content = $('<div id="glossaryContent"></div>');
$('body').append(glossaryTip.holder.append(glossaryTip.content));
$('body').append(glossaryTip.holder.append(glossaryTip.content));
},
},
show: function(content, event){
show: function(content, event){
glossaryTip.content.html(content);
glossaryTip.content.html(content);
// Display tip at mouse cursor
// Display tip at mouse cursor
var x = parseInt(event.mouse_event.pageX) + 15
var x = parseInt(event.mouse_event.pageX) + 15
var y = parseInt(event.mouse_event.pageY) + 5
var y = parseInt(event.mouse_event.pageY) + 5
glossaryTip.holder.css({top: y, left: x});
glossaryTip.holder.css({top: y, left: x});
// Display the tip
// Display the tip
if (glossaryTip.holder.is(':hidden')) {
if (glossaryTip.holder.is(':hidden')) {
glossaryTip.holder.show();
glossaryTip.holder.show();
}
}
// Add click handler to close
// Add click handler to close
glossaryTip.holder.bind('click', function(){
glossaryTip.holder.bind('click', function(){
glossaryTip.holder.stop(true).fadeOut(200);
glossaryTip.holder.stop(true).fadeOut(200);
return false;
return false;
});
});
}
}
});
});
$.glossaryTip = function(content, event) {
$.glossaryTip = function(content, event) {
var tip = $.glossaryTip.instance;
var tip = $.glossaryTip.instance;
if (!tip) { tip = $.glossaryTip.instance = new glossaryTip(); }
if (!tip) { tip = $.glossaryTip.instance = new glossaryTip(); }
tip.setup();
tip.setup();
tip.show(content, event);
tip.show(content, event);
return tip;
return tip;
}
}
})(jQuery);
})(jQuery);