-
Notifications
You must be signed in to change notification settings - Fork 1
/
locker.js
161 lines (142 loc) · 4.83 KB
/
locker.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
/**
* Copyright 2012 Audax Health
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* locker.js - Locking Mechanism for Lift-Based AJAX Requests
*
* This creates a locking mechanism specifically for use with Lift Ajax calls.
*
* Every time we invoke the Lift AJAX handler, we will add the call to the locker to prevent multiple requests going to
* the server.
*
* TO USE: Simply add one of the following classes on to the item that will call the AJAX handler.
* - l_callOnce: Make the ajax request one time only (useful for forms or other posts)
* - l_callAndWait: Lock the request until it returns, then unlock it. *This should be used for most things*
*/
/**
* Invoke the AjaxLocker only if the Lift Ajax Handler is loaded.
*/
$('document').ready( function() {
try {
if (liftAjax.lift_ajaxHandler !== undefined) {
LiftAjaxLocker.init();
}
}
catch(e) {
// Do nothing
}
});
/**
* LiftAjaxLocker contains the locker hash as well as the functions to lock / unlock / hijack the Lift AJAX calls
*/
(function(LiftAjaxLocker) {
LiftAjaxLocker.locker = {};
LiftAjaxLocker.isLocked = function(uid) {
if ( this.locker[uid] === true ) {
return true;
}
return false;
};
LiftAjaxLocker.lock = function(ajaxUid) {
if (this.isLocked(ajaxUid)) {
return 0;
}
this.locker[ajaxUid] = true;
return 1;
};
LiftAjaxLocker.unlock = function(ajaxUid) {
if (!this.isLocked(ajaxUid)) {
return 0;
}
this.locker[ajaxUid] = false;
return 1;
};
/**
* Set up the locker
*/
LiftAjaxLocker.init = function() {
var _this = this;
// We will backup the original ajaxHandler method so that we can insert our own method into the reference.
this._lift_ajaxHandler = liftAjax.lift_ajaxHandler;
liftAjax.lift_ajaxHandler = function(theData, theSuccess, theFailure, responseType) {
if (_this.isLocked(theData)) {
return;
}
/**
* Checks the target and the source element for a certain class.
* We specify both the target and source to cover SHtml.a
* IE / FF / Chrome safe
*/
function targetsHaveClass(whichClass) {
var target, event, source = null;
try {
if (window.event !== undefined) {
event = window.event;
}
else {
event = arguments.callee.caller.caller.arguments[0];
}
}
catch(e) {
event = arguments.callee.caller.caller.arguments[0];
}
try {
if (event.srcElement !== undefined) {
source = event.srcElement;
}
else {
source = event.target;
}
}
catch(e) {
source = event.target;
}
target = event.currentTarget;
if ( jQuery(target).hasClass(whichClass) || jQuery(source).hasClass(whichClass) ) {
return true;
}
return false;
}
/**
* Rules for different types of locks go here:
*/
/**
* callAndWait
*/
if ( targetsHaveClass('l_callAndWait') ) {
var __theSuccess = theSuccess;
var __theFailure = theFailure;
_this.lock(theData);
theSuccess = function() {
_this.unlock(theData);
__theSuccess();
};
theFailure = function() {
_this.unlock(theData);
__theFailure();
};
}
/**
* callOnce
*/
else if( targetsHaveClass('l_callOnce') ) {
_this.lock(theData);
}
// Make the initial AJAX request
_this._lift_ajaxHandler(theData, theSuccess, theFailure, responseType);
};
};
})
(window.LiftAjaxLocker = window.LiftAjaxLocker || {});