Valid HTML test
[weblabels.fsf.org.git] / www.fsf.org / 20131028 / files / unlockOnFormUnload.js
CommitLineData
5a920362 1/*
2 Plone unlock handler; attaches an unlock handler to $('form.enableUnlockProtection')
3
4 provides global plone
5*/
6
7
8/*global plone:true, setInterval:false, clearInterval:false */
9/*jslint nomen:false */
10
11/* Unlock the object on form unload, and refresh the lock every 5 minutes */
12if (typeof(plone) === 'undefined') {
13 var plone = {};
14}
15
16(function($){
17plone.UnlockHandler = {
18 init: function() {
19 // set up the handler, if there are any forms
20 if ($('form.enableUnlockProtection').length) {
21 $(window).unload(plone.UnlockHandler.execute);
22 plone.UnlockHandler._refresher = setInterval(plone.UnlockHandler.refresh, 300000);
23 }
24 },
25
26 cleanup: function() {
27 $(window).unbind('unload', plone.UnlockHandler.execute);
28 clearInterval(plone.UnlockHandler._refresher);
29 },
30
31 execute: function() {
32 // this.submitting is set from the form unload handler
33 // (formUnload.js) and signifies that we are in the
34 // form submit process. This means: no unlock needed,
35 // and it also would be harmful (ConflictError)
36 if (this.submitting) {return;}
37 $.get(plone.UnlockHandler._baseUrl() + '/@@plone_lock_operations/safe_unlock');
38 },
39
40 refresh: function() {
41 if (this.submitting) {return;}
42 $.get(plone.UnlockHandler._baseUrl() + '/@@plone_lock_operations/refresh_lock');
43 },
44
45 _baseUrl: function() {
46 var baseUrl, pieces;
47
48 baseUrl = $('base').attr('href');
49 if (!baseUrl) {
50 pieces = window.location.href.split('/');
51 pieces.pop();
52 baseUrl = pieces.join('/');
53 }
54 return baseUrl;
55 }
56};
57
58$(plone.UnlockHandler.init);
59
60})(jQuery);