web/data/cubicweb.htmlhelpers.js
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 /* in CW 3.10, we should move these functions in this namespace */
       
     2 cw.htmlhelpers = new Namespace('cw.htmlhelpers');
       
     3 
       
     4 jQuery.extend(cw.htmlhelpers, {
       
     5     popupLoginBox: function(loginboxid, focusid) {
       
     6         $('#'+loginboxid).toggleClass('hidden');
       
     7         jQuery('#' + focusid +':visible').focus();
       
     8     }
       
     9 });
       
    10 
       
    11 
       
    12 /**
       
    13  * .. function:: baseuri()
       
    14  *
       
    15  * returns the document's baseURI.
       
    16  */
       
    17 baseuri = cw.utils.deprecatedFunction(
       
    18     "[3.20] baseuri() is deprecated, use BASE_URL instead",
       
    19     function () {
       
    20         return BASE_URL;
       
    21     });
       
    22 
       
    23 /**
       
    24  * .. function:: setProgressCursor()
       
    25  *
       
    26  * set body's cursor to 'progress'
       
    27  */
       
    28 function setProgressCursor() {
       
    29     var body = document.getElementsByTagName('body')[0];
       
    30     body.style.cursor = 'progress';
       
    31 }
       
    32 
       
    33 /**
       
    34  * .. function:: resetCursor(result)
       
    35  *
       
    36  * reset body's cursor to default (mouse cursor). The main
       
    37  * purpose of this function is to be used as a callback in the
       
    38  * deferreds' callbacks chain.
       
    39  */
       
    40 function resetCursor(result) {
       
    41     var body = document.getElementsByTagName('body')[0];
       
    42     body.style.cursor = '';
       
    43     // pass result to next callback in the callback chain
       
    44     return result;
       
    45 }
       
    46 
       
    47 function updateMessage(msg) {
       
    48     var msgdiv = DIV({
       
    49         'class': 'message'
       
    50     });
       
    51     // don't pass msg to DIV() directly because DIV will html escape it
       
    52     // and msg should alreay be html escaped at this point.
       
    53     msgdiv.innerHTML = msg;
       
    54     jQuery('#appMsg').removeClass('hidden').empty().append(msgdiv);
       
    55 }
       
    56 
       
    57 /**
       
    58  * .. function:: asURL(props)
       
    59  *
       
    60  * builds a URL from an object (used as a dictionary)
       
    61  *
       
    62  * >>> asURL({'rql' : "RQL", 'x': [1, 2], 'itemvid' : "oneline"})
       
    63  * rql=RQL&vid=list&itemvid=oneline&x=1&x=2
       
    64  * >>> asURL({'rql' : "a&b", 'x': [1, 2], 'itemvid' : "oneline"})
       
    65  * rql=a%26b&x=1&x=2&itemvid=oneline
       
    66  */
       
    67 function asURL(props) {
       
    68     var chunks = [];
       
    69     for (key in props) {
       
    70         var value = props[key];
       
    71         // generate a list of couple key=value if key is multivalued
       
    72         if (cw.utils.isArrayLike(value)) {
       
    73             for (var i = 0; i < value.length; i++) {
       
    74                 chunks.push(key + '=' + cw.urlEncode(value[i]));
       
    75             }
       
    76         } else {
       
    77             chunks.push(key + '=' + cw.urlEncode(value));
       
    78         }
       
    79     }
       
    80     return chunks.join('&');
       
    81 }
       
    82 
       
    83 /**
       
    84  * .. function:: firstSelected(selectNode)
       
    85  *
       
    86  * return selected value of a combo box if any
       
    87  */
       
    88 function firstSelected(selectNode) {
       
    89     var $selection = $(selectNode).find('option:selected:first');
       
    90     return ($selection.length > 0) ? $selection[0] : null;
       
    91 }
       
    92 
       
    93 /**
       
    94  * .. function:: toggleVisibility(elemId)
       
    95  *
       
    96  * toggle visibility of an element by its id
       
    97  */
       
    98 function toggleVisibility(elemId) {
       
    99     cw.jqNode(elemId).toggleClass('hidden');
       
   100 }
       
   101 
       
   102 /**
       
   103  * .. function getElementsMatching(tagName, properties, \/* optional \*\/ parent)
       
   104  *
       
   105  * returns the list of elements in the document matching the tag name
       
   106  * and the properties provided
       
   107  *
       
   108  * * `tagName`, the tag's name
       
   109  *
       
   110  * * `properties`, a js Object used as a dict
       
   111  *
       
   112  * Return an iterator (if a *real* array is needed, you can use the
       
   113  *                      list() function)
       
   114  */
       
   115 function getElementsMatching(tagName, properties, /* optional */ parent) {
       
   116     parent = parent || document;
       
   117     return jQuery.grep(parent.getElementsByTagName(tagName), function elementMatches(element) {
       
   118         for (prop in properties) {
       
   119             if (jQuery(element).attr(prop) != properties[prop]) {
       
   120                 return false;
       
   121             }
       
   122         }
       
   123         return true;
       
   124     });
       
   125 }
       
   126 
       
   127 /**
       
   128  * .. function:: setCheckboxesState(nameprefix, value, checked)
       
   129  *
       
   130  * sets checked/unchecked status of checkboxes
       
   131  */
       
   132 
       
   133 function setCheckboxesState(nameprefix, value, checked) {
       
   134     // XXX: this looks in *all* the document for inputs
       
   135     jQuery('input:checkbox[name^=' + nameprefix + ']').each(function() {
       
   136         if (value == null || this.value == value) {
       
   137             this.checked = checked;
       
   138         }
       
   139     });
       
   140 }
       
   141 
       
   142 /**
       
   143  * .. function:: html2dom(source)
       
   144  *
       
   145  * this function is a hack to build a dom node from html source
       
   146  */
       
   147 function html2dom(source) {
       
   148     var tmpNode = SPAN();
       
   149     tmpNode.innerHTML = source;
       
   150     if (tmpNode.childNodes.length == 1) {
       
   151         return tmpNode.firstChild;
       
   152     }
       
   153     else {
       
   154         // we leave the span node when `source` has no root node
       
   155         // XXX This is cleary not the best solution, but css/html-wise,
       
   156         ///    a span not should not be too  much disturbing
       
   157         return tmpNode;
       
   158     }
       
   159 }
       
   160 
       
   161 // *** HELPERS **************************************************** //
       
   162 function rql_for_eid(eid) {
       
   163     return 'Any X WHERE X eid ' + eid;
       
   164 }
       
   165 function isTextNode(domNode) {
       
   166     return domNode.nodeType == 3;
       
   167 }
       
   168 function isElementNode(domNode) {
       
   169     return domNode.nodeType == 1;
       
   170 }
       
   171 
       
   172 function autogrow(area) {
       
   173     if (area.scrollHeight > area.clientHeight && ! window.opera) {
       
   174         if (area.rows < 20) {
       
   175             area.rows += 2;
       
   176         }
       
   177     }
       
   178 }
       
   179