/
jquery.updates.uncompressed.js
173 lines (148 loc) · 5.64 KB
/
jquery.updates.uncompressed.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* jquery.updates plugin 1.04
*
* Copyright (c) 2011-2017 Foswiki Contributors http://foswiki.org
*
* http://www.gnu.org/licenses/gpl.html
*
*/
"use strict";
(function($) {
// global defaults
var defaults = {
configureUrl: undefined,
endpointUrl: undefined,
debug: false,
delay: 1000, // number of seconds to delay contacting f.o.
timeout: 5000, // number of seconds a jsonp call is considered failure
cookieName: "FOSWIKI_UPDATESPLUGIN", // name of the cookie
cookieExpires: 7, // number of days the cookie takes to expire
cookieSecure: '0', // If secure cookies are needed (https)
cookieDomain: '' // Override domain if requested.
}, foswikiUpdates; // singleton
// class constructor
function FoswikiUpdates(options) {
var self = this;
self.options = $.extend({}, defaults, options);
if (self.options.debug) {
console.log("called new FoswikiUpdates"); // eslint-disable-line no-console
}
self.init();
self.loadPluginInfo(0);
return self;
}
// init method
FoswikiUpdates.prototype.init = function() {
var self = this;
//console.log("init FoswikiUpdates");
if (typeof(self.options.configureUrl) === 'undefined') {
self.options.configureUrl = foswiki.getPreference("UPDATESPLUGIN::CONFIGUREURL");
}
if (typeof(self.options.endpointUrl) === 'undefined') {
self.options.endpointUrl = foswiki.getScriptUrl("rest", "UpdatesPlugin", "check");
}
// Don't override the domain. Each host most likely needs it's own check
//self.options.cookieDomain = foswiki.getPreference('COOKIEREALM'); // Allow domain override
self.options.cookieSecure = foswiki.getPreference('URLHOST').indexOf('https://') === 0;
// events
$(document).on("refresh.foswikiUpdates", function() {
//console.log("BIND refresh.foswikiUpdates calling loadPluginInfo.");
self.loadPluginInfo(0);
});
$(document).on("forceRefresh.foswikiUpdates", function() {
//console.log("BIND forceRefresh.foswikiUpdates calling loadPluginInfo.");
$.cookie(self.options.cookieName, null, {
expires: -1,
path:'/',
domain:self.options.cookieDomain,
secure:self.options.cookieSecure
});
self.loadPluginInfo(1);
});
$(document).on("display.foswikiUpdates", function() {
//console.log("BIND display.foswikiUpdates calling loadPluginInfo.");
self.displayPluginInfo();
});
$(document).on("click", "#foswikiUpdatesIgnore", function() {
// setting the cookie to zero...means ignore and don't search again
//console.log("BIND click entered ");
$.cookie(self.options.cookieName, [], {
expires: self.options.cookieExpires,
path: "/",
domain:self.options.cookieDomain,
secure:self.options.cookieSecure
});
$(".foswikiUpdatesMessage").fadeOut();
return false;
});
};
// pull info from f.o and refresh internal state
FoswikiUpdates.prototype.loadPluginInfo = function(forced) {
var self = this;
//console.log("called loadPluginInfo forced: " + forced );
self.outdatedPlugins = $.cookie(self.options.cookieName);
if (typeof(self.outdatedPlugins) === 'undefined') {
// collect remote info
window.setTimeout(function() {
//console.log("...loading");
$.ajax({
type:"get",
url: self.options.endpointUrl,
dataType: "json",
timeout: self.options.timeout,
success: function(data) {
//console.log("success: data=",data);
self.outdatedPlugins = data;
// remember findings: sets cookie to the number of outdated plugins. setting it to
// zero explicitly can either mean: everything up-to-date or ignore pending updates
$.cookie(self.options.cookieName, self.outdatedPlugins, {
expires: self.options.cookieExpires,
path: "/",
domain:self.options.cookieDomain,
secure:self.options.cookieSecure
});
//console.log("Forced: " + forced);
if (self.outdatedPlugins.length > 0 || forced) {
$(document).trigger("display.foswikiUpdates");
}
},
error: function() {
//console.log("got an error: status=",status,"msg=",msg)
// remember the error state
$.cookie(self.options.cookieName, -1, {
expires: self.options.cookieExpires,
path: "/",
domain:self.options.cookieDomain,
secure:self.options.cookieSecure
});
}
});
}, self.options.delay);
} else {
if (typeof(self.outdatedPlugins) === 'string' && self.outdatedPlugins.length > 0) {
self.outdatedPlugins = self.outdatedPlugins.split(/\s*,\s*/);
}
if (self.outdatedPlugins.length > 0) {
// we already know. so only trigger the display again
$(document).trigger("display.foswikiUpdates");
}
}
};
// displays internal state using a nice info box at the top of the page
FoswikiUpdates.prototype.displayPluginInfo = function() {
var self = this, elem;
$(".foswikiUpdatesMessage").remove(); // ... the old one if there
// ... and add a new one
elem = $("#foswikiUpdatesTmpl").render([{
nrPlugins:self.outdatedPlugins.length,
outdatedPlugins:self.outdatedPlugins.sort().join(", "),
cookieExpires:self.options.cookieExpires,
configureUrl:self.options.configureUrl
}]);
$(elem).prependTo("body").fadeIn();
};
// document ready things
$(function() {
foswikiUpdates = foswikiUpdates || new FoswikiUpdates();
});
})(jQuery);