web/data/cubicweb.htmlhelpers.js
changeset 5658 7b9553a9db65
parent 5479 6ba3587c5cda
child 5699 f4f6ee3af50b
--- a/web/data/cubicweb.htmlhelpers.js	Thu Jun 03 10:17:44 2010 +0200
+++ b/web/data/cubicweb.htmlhelpers.js	Thu Jun 03 14:51:42 2010 +0200
@@ -1,31 +1,37 @@
 CubicWeb.require('python.js');
 CubicWeb.require('jquery.corner.js');
 
-/* returns the document's baseURI. (baseuri() uses document.baseURI if
+/**
+ * .. function:: baseuri()
+ *
+ * returns the document's baseURI. (baseuri() uses document.baseURI if
  * available and inspects the <base> tag manually otherwise.)
-*/
+ */
 function baseuri() {
     var uri = document.baseURI;
     if (uri) { // some browsers don't define baseURI
-	return uri;
+        return uri;
     }
-    var basetags = document.getElementsByTagName('base');
-    if (basetags.length) {
-	return getNodeAttribute(basetags[0], 'href');
-    }
-    return '';
+    return jQuery('base').attr('href');
 }
 
-
-/* set body's cursor to 'progress' */
+/**
+ * .. function:: setProgressCursor()
+ *
+ * set body's cursor to 'progress'
+ */
 function setProgressCursor() {
     var body = document.getElementsByTagName('body')[0];
     body.style.cursor = 'progress';
 }
 
-/* reset body's cursor to default (mouse cursor). The main
+/**
+ * .. function:: resetCursor(result)
+ *
+ * reset body's cursor to default (mouse cursor). The main
  * purpose of this function is to be used as a callback in the
- * deferreds' callbacks chain. */
+ * deferreds' callbacks chain.
+ */
 function resetCursor(result) {
     var body = document.getElementsByTagName('body')[0];
     body.style.cursor = 'default';
@@ -34,14 +40,19 @@
 }
 
 function updateMessage(msg) {
-    var msgdiv = DIV({'class':'message'});
+    var msgdiv = DIV({
+        'class': 'message'
+    });
     // don't pass msg to DIV() directly because DIV will html escape it
     // and msg should alreay be html escaped at this point.
     msgdiv.innerHTML = msg;
     jQuery('#appMsg').removeClass('hidden').empty().append(msgdiv);
 }
 
-/* builds an url from an object (used as a dictionnary)
+/**
+ * .. function:: asURL(props)
+ *
+ * builds an url from an object (used as a dictionnary)
  *
  * >>> asURL({'rql' : "RQL", 'x': [1, 2], 'itemvid' : "oneline"})
  * rql=RQL&vid=list&itemvid=oneline&x=1&x=2
@@ -50,122 +61,145 @@
  */
 function asURL(props) {
     var chunks = [];
-    for(key in props) {
-	var value = props[key];
-	// generate a list of couple key=value if key is multivalued
-	if (isArrayLike(value)) {
-	    for (var i=0; i<value.length;i++) {
-		chunks.push(key + '=' + urlEncode(value[i]));
-	    }
-	} else {
-	    chunks.push(key + '=' + urlEncode(value));
-	}
+    for (key in props) {
+        var value = props[key];
+        // generate a list of couple key=value if key is multivalued
+        if (cw.utils.isArrayLike(value)) {
+            for (var i = 0; i < value.length; i++) {
+                chunks.push(key + '=' + urlEncode(value[i]));
+            }
+        } else {
+            chunks.push(key + '=' + urlEncode(value));
+        }
     }
     return chunks.join('&');
 }
 
-/* return selected value of a combo box if any
+/**
+ * .. function:: firstSelected(selectNode)
+ *
+ * return selected value of a combo box if any
  */
 function firstSelected(selectNode) {
-    var selection = filter(attrgetter('selected'), selectNode.options);
-    return (selection.length > 0) ? getNodeAttribute(selection[0], 'value'):null;
+    var $selection = $(selectNode).find('option:selected:first');
+    return ($selection.length > 0) ? $selection[0] : null;
 }
 
-/* toggle visibility of an element by its id
+/**
+ * .. function:: toggleVisibility(elemId)
+ *
+ * toggle visibility of an element by its id
  */
 function toggleVisibility(elemId) {
-    jqNode(elemId).toggleClass('hidden');
+    $('#' + elemId).toggleClass('hidden');
 }
 
-
-/* toggles visibility of login popup div */
+/**
+ * .. function:: popupLoginBox()
+ *
+ * toggles visibility of login popup div
+ */
 // XXX used exactly ONCE in basecomponents
 function popupLoginBox() {
-    toggleVisibility('popupLoginBox');
+    $('#popupLoginBox').toggleClass('hidden');
     jQuery('#__login:visible').focus();
 }
 
-
-/* returns the list of elements in the document matching the tag name
+/**
+ * .. function getElementsMatching(tagName, properties, \/* optional \*\/ parent)
+ *
+ * returns the list of elements in the document matching the tag name
  * and the properties provided
  *
- * @param tagName the tag's name
- * @param properties a js Object used as a dict
- * @return an iterator (if a *real* array is needed, you can use the
+ * * `tagName`, the tag's name
+ *
+ * * `properties`, a js Object used as a dict
+ *
+ * Return an iterator (if a *real* array is needed, you can use the
  *                      list() function)
  */
 function getElementsMatching(tagName, properties, /* optional */ parent) {
     parent = parent || document;
-    return filter(function elementMatches(element) {
-                     for (prop in properties) {
-                       if (getNodeAttribute(element, prop) != properties[prop]) {
-	                 return false;}}
-                    return true;},
-                  parent.getElementsByTagName(tagName));
+    return jQuery.grep(parent.getElementsByTagName(tagName), function elementMatches(element) {
+        for (prop in properties) {
+            if (jQuery(element).attr(prop) != properties[prop]) {
+                return false;
+            }
+        }
+        return true;
+    });
 }
 
-/*
+/**
+ * .. function:: setCheckboxesState(nameprefix, value, checked)
+ *
  * sets checked/unchecked status of checkboxes
  */
-function setCheckboxesState(nameprefix, checked){
+
+function setCheckboxesState(nameprefix, value, checked) {
     // XXX: this looks in *all* the document for inputs
-    var elements = getElementsMatching('input', {'type': "checkbox"});
-    filterfunc = function(cb) { return nameprefix && cb.name.startsWith(nameprefix); };
-    forEach(filter(filterfunc, elements), function(cb) {cb.checked=checked;});
+    jQuery('input:checkbox[name^=' + nameprefix + ']').each(function() {
+        if (value == null || this.value == value) {
+            this.checked = checked;
+        }
+    });
 }
 
-function setCheckboxesState2(nameprefix, value, checked){
-    // XXX: this looks in *all* the document for inputs
-    var elements = getElementsMatching('input', {'type': "checkbox"});
-    filterfunc = function(cb) { return nameprefix && cb.name.startsWith(nameprefix) && cb.value == value; };
-    forEach(filter(filterfunc, elements), function(cb) {cb.checked=checked;});
-}
-
-
-/* this function is a hack to build a dom node from html source */
+/**
+ * .. function:: html2dom(source)
+ *
+ * this function is a hack to build a dom node from html source
+ */
 function html2dom(source) {
     var tmpNode = SPAN();
     tmpNode.innerHTML = source;
     if (tmpNode.childNodes.length == 1) {
-	return tmpNode.firstChild;
+        return tmpNode.firstChild;
     }
     else {
-	// we leave the span node when `source` has no root node
-	// XXX This is cleary not the best solution, but css/html-wise,
-	///    a span not should not be too  much disturbing
-	return tmpNode;
+        // we leave the span node when `source` has no root node
+        // XXX This is cleary not the best solution, but css/html-wise,
+        ///    a span not should not be too  much disturbing
+        return tmpNode;
     }
 }
 
-
 // *** HELPERS **************************************************** //
-function rql_for_eid(eid) { return 'Any X WHERE X eid ' + eid; }
-function isTextNode(domNode) { return domNode.nodeType == 3; }
-function isElementNode(domNode) { return domNode.nodeType == 1; }
+function rql_for_eid(eid) {
+    return 'Any X WHERE X eid ' + eid;
+}
+function isTextNode(domNode) {
+    return domNode.nodeType == 3;
+}
+function isElementNode(domNode) {
+    return domNode.nodeType == 1;
+}
 
 function autogrow(area) {
-    if (area.scrollHeight > area.clientHeight && !window.opera) {
-	if (area.rows < 20) {
-	    area.rows += 2;
-	}
+    if (area.scrollHeight > area.clientHeight && ! window.opera) {
+        if (area.rows < 20) {
+            area.rows += 2;
+        }
     }
 }
 //============= page loading events ==========================================//
-
-CubicWeb.rounded = [
-		    ['div.sideBoxBody', 'bottom 6px'],
-		    ['div.boxTitle, div.sideBoxTitle, th.month', 'top 6px']
-		    ];
+CubicWeb.rounded = [['div.sideBoxBody', 'bottom 6px'],
+                    ['div.boxTitle, div.sideBoxTitle, th.month', 'top 6px']];
 
 function roundedCorners(node) {
-    node = jQuery(node);
-    for(var r=0; r < CubicWeb.rounded.length; r++) {
-       node.find(CubicWeb.rounded[r][0]).corner(CubicWeb.rounded[r][1]);
+    if (jQuery.fn.corner !== undefined) {
+        node = jQuery(node);
+        for (var r = 0; r < CubicWeb.rounded.length; r++) {
+            node.find(CubicWeb.rounded[r][0]).corner(CubicWeb.rounded[r][1]);
+        }
     }
 }
 
-jQuery(document).ready(function () {roundedCorners(this.body);});
+jQuery(document).ready(function() {
+    roundedCorners(this.body);
+});
 
 CubicWeb.provide('corners.js');
 
 CubicWeb.provide('htmlhelpers.js');
+