author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 09 Jul 2009 12:14:02 +0200 | |
branch | stable |
changeset 2345 | 16e3d0e47ee6 |
parent 2338 | 3f7c7fbae94e |
child 2355 | 359903ef980a |
permissions | -rw-r--r-- |
0 | 1 |
CubicWeb.require('python.js'); |
2338
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
2 |
CubicWeb.require('jquery.corner.js'); |
0 | 3 |
|
4 |
/* returns the document's baseURI. (baseuri() uses document.baseURI if |
|
5 |
* available and inspects the <base> tag manually otherwise.) |
|
6 |
*/ |
|
7 |
function baseuri() { |
|
8 |
var uri = document.baseURI; |
|
9 |
if (uri) { // some browsers don't define baseURI |
|
10 |
return uri; |
|
11 |
} |
|
12 |
var basetags = document.getElementsByTagName('base'); |
|
13 |
if (basetags.length) { |
|
14 |
return getNodeAttribute(basetags[0], 'href'); |
|
15 |
} |
|
16 |
return ''; |
|
17 |
} |
|
18 |
||
19 |
function insertText(text, areaId) { |
|
20 |
var textarea = jQuery('#' + areaId); |
|
21 |
if (document.selection) { // IE |
|
22 |
var selLength; |
|
23 |
textarea.focus(); |
|
24 |
sel = document.selection.createRange(); |
|
25 |
selLength = sel.text.length; |
|
26 |
sel.text = text; |
|
27 |
sel.moveStart('character', selLength-text.length); |
|
28 |
sel.select(); |
|
29 |
} else if (textarea.selectionStart || textarea.selectionStart == '0') { // mozilla |
|
30 |
var startPos = textarea.selectionStart; |
|
31 |
var endPos = textarea.selectionEnd; |
|
32 |
// insert text so that it replaces the [startPos, endPos] part |
|
33 |
textarea.value = textarea.value.substring(0,startPos) + text + textarea.value.substring(endPos,textarea.value.length); |
|
34 |
// set cursor pos at the end of the inserted text |
|
35 |
textarea.selectionStart = textarea.selectionEnd = startPos+text.length; |
|
36 |
textarea.focus(); |
|
37 |
} else { // safety belt for other browsers |
|
38 |
textarea.value += text; |
|
39 |
} |
|
40 |
} |
|
41 |
||
42 |
/* taken from dojo toolkit */ |
|
43 |
function setCaretPos(element, start, end){ |
|
44 |
if(!end){ end = element.value.length; } // NOTE: Strange - should be able to put caret at start of text? |
|
45 |
// Mozilla |
|
46 |
// parts borrowed from http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130 |
|
47 |
if(element.setSelectionRange){ |
|
48 |
element.focus(); |
|
49 |
element.setSelectionRange(start, end); |
|
50 |
} else if(element.createTextRange){ // IE |
|
51 |
var range = element.createTextRange(); |
|
52 |
with(range){ |
|
53 |
collapse(true); |
|
54 |
moveEnd('character', end); |
|
55 |
moveStart('character', start); |
|
56 |
select(); |
|
57 |
} |
|
58 |
} else { //otherwise try the event-creation hack (our own invention) |
|
59 |
// do we need these? |
|
60 |
element.value = element.value; |
|
61 |
element.blur(); |
|
62 |
element.focus(); |
|
63 |
// figure out how far back to go |
|
64 |
var dist = parseInt(element.value.length)-end; |
|
65 |
var tchar = String.fromCharCode(37); |
|
66 |
var tcc = tchar.charCodeAt(0); |
|
67 |
for(var x = 0; x < dist; x++){ |
|
68 |
var te = document.createEvent("KeyEvents"); |
|
69 |
te.initKeyEvent("keypress", true, true, null, false, false, false, false, tcc, tcc); |
|
70 |
element.dispatchEvent(te); |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
function setProgressMessage(label) { |
|
76 |
var body = document.getElementsByTagName('body')[0]; |
|
77 |
body.appendChild(DIV({id: 'progress'}, label)); |
|
78 |
jQuery('#progress').show(); |
|
79 |
} |
|
80 |
||
81 |
function resetProgressMessage() { |
|
82 |
var body = document.getElementsByTagName('body')[0]; |
|
83 |
jQuery('#progress').hide(); |
|
84 |
} |
|
85 |
||
86 |
||
87 |
/* set body's cursor to 'progress' |
|
88 |
*/ |
|
89 |
function setProgressCursor() { |
|
90 |
var body = document.getElementsByTagName('body')[0]; |
|
91 |
body.style.cursor = 'progress'; |
|
92 |
} |
|
93 |
||
94 |
/* |
|
95 |
* reset body's cursor to default (mouse cursor). The main |
|
96 |
* purpose of this function is to be used as a callback in the |
|
97 |
* deferreds' callbacks chain. |
|
98 |
*/ |
|
99 |
function resetCursor(result) { |
|
100 |
var body = document.getElementsByTagName('body')[0]; |
|
101 |
body.style.cursor = 'default'; |
|
102 |
// pass result to next callback in the callback chain |
|
103 |
return result; |
|
104 |
} |
|
105 |
||
106 |
function updateMessage(msg) { |
|
107 |
var msgdiv = DIV({'class':'message'}); |
|
108 |
// don't pass msg to DIV() directly because DIV will html escape it |
|
109 |
// and msg should alreay be html escaped at this point. |
|
110 |
msgdiv.innerHTML = msg; |
|
111 |
jQuery('#appMsg').removeClass('hidden').empty().append(msgdiv); |
|
112 |
} |
|
113 |
||
114 |
/* builds an url from an object (used as a dictionnary) |
|
1419 | 115 |
* Notable difference with MochiKit's queryString: asURL does not |
0 | 116 |
* *url_quote* each value found in the dictionnary |
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
117 |
* |
1419 | 118 |
* >>> asURL({'rql' : "RQL", 'x': [1, 2], 'itemvid' : "oneline"}) |
0 | 119 |
* rql=RQL&vid=list&itemvid=oneline&x=1&x=2 |
120 |
*/ |
|
1419 | 121 |
function asURL(props) { |
0 | 122 |
var chunks = []; |
123 |
for(key in props) { |
|
124 |
var value = props[key]; |
|
125 |
// generate a list of couple key=value if key is multivalued |
|
126 |
if (isArrayLike(value)) { |
|
127 |
for (var i=0; i<value.length;i++) { |
|
128 |
chunks.push(key + '=' + value[i]); |
|
129 |
} |
|
130 |
} else { |
|
131 |
chunks.push(key + '=' + value); |
|
132 |
} |
|
133 |
} |
|
134 |
return chunks.join('&'); |
|
135 |
} |
|
136 |
||
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
137 |
/* return selected value of a combo box if any |
0 | 138 |
*/ |
139 |
function firstSelected(selectNode) { |
|
140 |
var selection = filter(attrgetter('selected'), selectNode.options); |
|
141 |
return (selection.length>0) ? getNodeAttribute(selection[0], 'value'):null; |
|
142 |
} |
|
143 |
||
144 |
/* toggle visibility of an element by its id |
|
145 |
*/ |
|
146 |
function toggleVisibility(elemId) { |
|
147 |
jqNode(elemId).toggleClass('hidden'); |
|
148 |
} |
|
149 |
||
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
150 |
|
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
151 |
/* toggles visibility of login popup div */ |
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
152 |
function popupLoginBox() { |
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
153 |
toggleVisibility('popupLoginBox'); |
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
154 |
jQuery('#__login:visible').focus(); |
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
155 |
} |
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
156 |
|
0 | 157 |
/* |
158 |
* return true (resp. false) if <element> (resp. doesn't) matches <properties> |
|
159 |
*/ |
|
160 |
function elementMatches(properties, element) { |
|
161 |
for (prop in properties) { |
|
162 |
if (getNodeAttribute(element, prop) != properties[prop]) { |
|
163 |
return false; |
|
164 |
} |
|
165 |
} |
|
166 |
return true; |
|
167 |
} |
|
168 |
||
169 |
/* returns the list of elements in the document matching the tag name |
|
170 |
* and the properties provided |
|
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
171 |
* |
0 | 172 |
* @param tagName the tag's name |
173 |
* @param properties a js Object used as a dict |
|
174 |
* @return an iterator (if a *real* array is needed, you can use the |
|
175 |
* list() function) |
|
176 |
*/ |
|
177 |
function getElementsMatching(tagName, properties, /* optional */ parent) { |
|
178 |
var filterfunc = partial(elementMatches, properties); |
|
179 |
parent = parent || document; |
|
180 |
return filter(filterfunc, parent.getElementsByTagName(tagName)); |
|
181 |
} |
|
182 |
||
183 |
/* |
|
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
184 |
* sets checked/unchecked status of checkboxes |
0 | 185 |
*/ |
186 |
function setCheckboxesState(nameprefix, checked){ |
|
187 |
// XXX: this looks in *all* the document for inputs |
|
188 |
var elements = getElementsMatching('input', {'type': "checkbox"}); |
|
189 |
filterfunc = function(cb) { return nameprefix && cb.name.startsWith(nameprefix); }; |
|
190 |
forEach(filter(filterfunc, elements), function(cb) {cb.checked=checked;}); |
|
191 |
} |
|
192 |
||
193 |
function setCheckboxesState2(nameprefix, value, checked){ |
|
194 |
// XXX: this looks in *all* the document for inputs |
|
195 |
var elements = getElementsMatching('input', {'type': "checkbox"}); |
|
196 |
filterfunc = function(cb) { return nameprefix && cb.name.startsWith(nameprefix) && cb.value == value; }; |
|
197 |
forEach(filter(filterfunc, elements), function(cb) {cb.checked=checked;}); |
|
198 |
} |
|
199 |
||
200 |
/* |
|
201 |
* centers an HTML element on the screen |
|
202 |
*/ |
|
203 |
function centerElement(obj){ |
|
204 |
var vpDim = getViewportDimensions(); |
|
205 |
var elemDim = getElementDimensions(obj); |
|
206 |
setElementPosition(obj, {'x':((vpDim.w - elemDim.w)/2), |
|
207 |
'y':((vpDim.h - elemDim.h)/2)}); |
|
208 |
} |
|
209 |
||
210 |
/* this function is a hack to build a dom node from html source */ |
|
211 |
function html2dom(source) { |
|
212 |
var tmpNode = SPAN(); |
|
213 |
tmpNode.innerHTML = source; |
|
214 |
if (tmpNode.childNodes.length == 1) { |
|
215 |
return tmpNode.firstChild; |
|
216 |
} |
|
217 |
else { |
|
218 |
// we leave the span node when `source` has no root node |
|
219 |
// XXX This is cleary not the best solution, but css/html-wise, |
|
220 |
/// a span not should not be too much disturbing |
|
221 |
return tmpNode; |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
||
226 |
// *** HELPERS **************************************************** // |
|
227 |
function rql_for_eid(eid) { return 'Any X WHERE X eid ' + eid; } |
|
228 |
function isTextNode(domNode) { return domNode.nodeType == 3; } |
|
229 |
function isElementNode(domNode) { return domNode.nodeType == 1; } |
|
230 |
||
231 |
function changeLinkText(link, newText) { |
|
232 |
jQuery(link).text(newText); |
|
233 |
// for (var i=0; i<link.childNodes.length; i++) { |
|
234 |
// var node = link.childNodes[i]; |
|
235 |
// if (isTextNode(node)) { |
|
236 |
// swapDOM(node, document.createTextNode(newText)); |
|
237 |
// break; |
|
238 |
// } |
|
239 |
// } |
|
240 |
} |
|
241 |
||
242 |
||
243 |
function autogrow(area) { |
|
244 |
if (area.scrollHeight > area.clientHeight && !window.opera) { |
|
245 |
if (area.rows < 20) { |
|
246 |
area.rows += 2; |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
||
2130
caa5acbecc08
[javascript] provide a simple function to limit textarea size (+minor cosmetic changes)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
251 |
function limitTextAreaSize(textarea, size) { |
caa5acbecc08
[javascript] provide a simple function to limit textarea size (+minor cosmetic changes)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
252 |
var $area = jQuery(textarea); |
caa5acbecc08
[javascript] provide a simple function to limit textarea size (+minor cosmetic changes)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
253 |
$area.val($area.val().slice(0, size)); |
caa5acbecc08
[javascript] provide a simple function to limit textarea size (+minor cosmetic changes)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
254 |
} |
caa5acbecc08
[javascript] provide a simple function to limit textarea size (+minor cosmetic changes)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
255 |
|
0 | 256 |
//============= page loading events ==========================================// |
2338
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
257 |
|
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
258 |
CubicWeb.rounded = [ |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
259 |
['div.sideBoxBody', 'bottom 6px'], |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
260 |
['div.boxTitle, div.boxPrefTitle, div.sideBoxTitle, th.month', 'top 6px'], |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
261 |
]; |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
262 |
|
0 | 263 |
function roundedCornersOnLoad() { |
2338
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
264 |
roundedCorners(); |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
265 |
} |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
266 |
|
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
267 |
function roundedCorners(node) { |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
268 |
node == node || document.body; |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
269 |
node = jQuery(node); |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
270 |
for(var r=0; r<CubicWeb.rounded.length;r++){ |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
271 |
node.find(CubicWeb.rounded[r][0]).corner(CubicWeb.rounded[r][1]); |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2232
diff
changeset
|
272 |
} |
0 | 273 |
} |
274 |
||
30
25ef1dddaab8
round corners are back
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
29
diff
changeset
|
275 |
jQuery(document).ready(roundedCornersOnLoad); |
0 | 276 |
|
277 |
CubicWeb.provide('htmlhelpers.js'); |
|
278 |