-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlmenuspy.js
executable file
·128 lines (98 loc) · 4.19 KB
/
urlmenuspy.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
*
* url-menu-spy
* Autor: bichotll
* Web-autor: bic.cat
* Web script: http://bichotll.github.io/url-menu-spy/
* Llicència Apache
*
*/
// jquery plugin boilerplate: http://stefangabos.ro/jquery/jquery-plugin-boilerplate-revisited/
// remember to change every instance of "url_menu_spy" to the name of your plugin!
(function($) {
// here we go!
$.url_menu_spy = function(element, options) {
// plugin's default options
// this is private property and is accessible only from inside the plugin
var defaults = {
data_urlspy_name: 'urlspy',
sub_element_path: '',
class_select: 'active'
}
// to avoid confusions, use "plugin" to reference the current
// instance of the object
var plugin = this;
// this will hold the merged default, and user-provided options
// plugin's properties will be available through this object like:
// plugin.settings.propertyName from inside the plugin or
// element.data('url_menu_spy').settings.propertyName from outside
// the plugin, where "element" is the element the plugin is
// attached to;
plugin.settings = {}
// reference to the jQuery version of DOM element the plugin is attached to
var $element = $(element),
element = element; // reference to the actual DOM element
// the "constructor" method that gets called when the object is created
plugin.init = function() {
// the plugin's final properties are the merged default
// and user-provided options (if any)
plugin.settings = $.extend({}, defaults, options)
plugin.findspymenu();
//on url changes...
window.onhashchange = function(){
plugin.hashchanged();
}
}
// public methods
// these methods can be called like:
// plugin.methodName(arg1, arg2, ... argn) from inside the plugin or
// element.data('url_menu_spy').publicMethod(arg1, arg2, ... argn)
// from outside the plugin, where "element"
// is the element the plugin is attached to;
plugin.findspymenu = function() {
$('[data-urlmenuspy]').each(function() {
var patt = new RegExp($(this).data('urlmenuspy'));
if (patt.test(document.URL)) {
active_item($(this));
}
});
}
plugin.hashchanged = function() {
plugin.clear_active();
plugin.findspymenu();
}
plugin.clear_active = function() {
$('[data-urlmenuspy]').removeClass(defaults.class_select);
}
// private methods
// these methods can be called only from inside the plugin like:
// methodName(arg1, arg2, ... argn)
active_item = function(item) {
if (defaults.sub_element_path == '')
item.addClass(defaults.class_select);
else
item.find(defaults.sub_element_path).addClass(defaults.class_select);
}
// fire up the plugin!
// call the "constructor" method
plugin.init();
}
// add the plugin to the jQuery.fn object
$.fn.url_menu_spy = function(options) {
// iterate through the DOM elements we are attaching the plugin to
return this.each(function() {
// if plugin has not already been attached to the element
if (undefined == $(this).data('url_menu_spy')) {
// create a new instance of the plugin
// pass the DOM element and the user-provided options as arguments
var plugin = new $.url_menu_spy(this, options);
// in the jQuery version of the element
// store a reference to the plugin object
// you can later access the plugin and its methods and properties like
// element.data('url_menu_spy').publicMethod(arg1, arg2, ... argn) or
// element.data('url_menu_spy').settings.propertyName
$(this).data('url_menu_spy', plugin);
}
});
}
})(jQuery);