-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.bp.contextualize.js
73 lines (61 loc) · 1.82 KB
/
jquery.bp.contextualize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Contextualize Plugin - Give your links some context
* @author Ben Plum
* @version 0.4.2
*
* Copyright © 2013 Ben Plum <[email protected]>
* Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
if (jQuery) (function($) {
// Default Options
var options = {
exclude: "",
prefix: "icon_",
types: ["pdf", "doc", "xls", "ppt", "swf", "zip", "mp3"]
};
// Public Methods
var pub = {
// Set / Update defaults
defaults: function(opts) {
options = jQuery.extend(options, opts);
return (typeof this === 'object') ? $(this) : true;
},
// Remove some context
remove: function(opts) {
return $(this).filter('[context="on"]')
.removeClass( options.prefix + "external " + options.prefix + options.types.join(" " + options.prefix) )
.attr("context", null);
}
};
// Add some context
function _init(opts) {
options = jQuery.extend(options, opts);
var filter = (options.exclude != "") ? ", " + options.exclude : "",
$items = $(this).not('[context="on"]' + filter),
counter = $items.lenth;
for (var i = 0, count = $items.length; i < count; i++) {
var $anchor = $items.eq(i);
if ($anchor.attr("href") !== undefined) {
var classes = "",
ext = $anchor.attr("href").substr(-3).toLowerCase();
if ($.inArray(ext, options.types) > -1) {
classes += " " + options.prefix + ext;
}
if ($anchor.attr("target") == "_blank") {
classes += " " + options.prefix + "external";
}
$anchor.addClass(classes).attr("context", "on");
}
}
return $items;
}
// Define Plugin
$.fn.contextualize = function(method) {
if (pub[method]) {
return pub[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return _init.apply(this, arguments);
}
return this;
};
})(jQuery);