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