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