web/data/cubicweb.ajax.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 17 Jun 2010 14:42:41 +0200
branchstable
changeset 5767 1d811df051c2
parent 5506 0ee2edec2d7a
child 5772 4283ee58acaf
permissions -rw-r--r--
[packaging] MANIFEST.in cleanups, update js/css copyright, remove no more used files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
/*
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
 *  :organization: Logilab
5767
1d811df051c2 [packaging] MANIFEST.in cleanups, update js/css copyright, remove no more used files
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5506
diff changeset
     3
 *  :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
CubicWeb.require('python.js');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
CubicWeb.require('htmlhelpers.js');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
var JSON_BASE_URL = baseuri() + 'json?';
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    12
function _loadAjaxHtmlHead(node, head, tag, srcattr) {
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    13
    var loaded = [];
2257
2c9eceeaae6a fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1801
diff changeset
    14
    var jqtagfilter = tag + '[' + srcattr + ']';
2c9eceeaae6a fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1801
diff changeset
    15
    jQuery('head ' + jqtagfilter).each(function(i) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    16
        loaded.push(this.getAttribute(srcattr));
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    17
    });
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    18
    node.find(tag).each(function(i) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    19
        if (this.getAttribute(srcattr)) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    20
            if (!loaded.contains(this.getAttribute(srcattr))) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    21
                jQuery(this).appendTo(head);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    22
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    23
        } else {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    24
            jQuery(this).appendTo(head);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    25
        }
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    26
    });
2257
2c9eceeaae6a fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1801
diff changeset
    27
    node.find(jqtagfilter).remove();
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    28
}
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    29
642
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
    30
/*
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    31
 * inspect dom response (as returned by getDomFromResponse), search for
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    32
 * a <div class="ajaxHtmlHead"> node and put its content into the real
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    33
 * document's head.
642
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
    34
 * This enables dynamic css and js loading and is used by replacePageChunk
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
    35
 */
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    36
function loadAjaxHtmlHead(response) {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    37
    var $head = jQuery('head');
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    38
    var $responseHead = jQuery(response).find('div.ajaxHtmlHead');
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    39
    // no ajaxHtmlHead found, no processing required
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    40
    if (!$responseHead.length) {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    41
        return response;
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    42
    }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    43
    _loadAjaxHtmlHead($responseHead, $head, 'script', 'src');
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    44
    _loadAjaxHtmlHead($responseHead, $head, 'link', 'href');
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    45
    // add any remaining children (e.g. meta)
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    46
    $responseHead.children().appendTo($head);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    47
    // remove original container, which is now empty
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    48
    $responseHead.remove();
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    49
    // if there was only one actual node in the reponse besides
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    50
    // the ajaxHtmlHead, then remove the wrapper created by
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    51
    // getDomFromResponse() and return this single element
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    52
    // For instance :
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    53
    // 1/ CW returned the following content :
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    54
    //    <div>the-actual-content</div><div class="ajaxHtmlHead">...</div>
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    55
    // 2/ getDomFromReponse() wrapped this into a single DIV to hold everything
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    56
    //    in one, unique, dom element
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    57
    // 3/ now that we've removed the ajaxHtmlHead div, the only
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    58
    //    node left in the wrapper if the 'real' node built by the view,
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    59
    //    we can safely return this node. Otherwise, the view itself
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    60
    //    returned several 'root' nodes and we need to keep the wrapper
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    61
    //    created by getDomFromResponse()
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    62
    if (response.childNodes.length == 1 &&
5506
0ee2edec2d7a [ajax] missing html heads processing in reloadComponent (sic\!)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5186
diff changeset
    63
        response.getAttribute('cubicweb:type') == 'cwResponseWrapper') {
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    64
        return response.firstChild;
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    65
    }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    66
    return response;
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    67
}
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    68
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    69
function preprocessAjaxLoad(node, newdomnode) {
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
    70
    return loadAjaxHtmlHead(newdomnode);
642
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
    71
}
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
    72
1407
75863d3ffd9b cleanup (undue req param)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1320
diff changeset
    73
function postAjaxLoad(node) {
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    74
    // find sortable tables if there are some
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    75
    if (typeof(Sortable) != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    76
        Sortable.sortTables(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    77
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    78
    // find textareas and wrap them if there are some
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    79
    if (typeof(FCKeditor) != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    80
        buildWysiwygEditors();
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    81
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    82
    if (typeof initFacetBoxEvents != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    83
        initFacetBoxEvents(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    84
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    85
    if (typeof buildWidgets != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    86
        buildWidgets(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    87
    }
2338
3f7c7fbae94e call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2257
diff changeset
    88
    if (typeof roundedCorners != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
    89
        roundedCorners(node);
955
3017a858bbc2 round corders on ajax loads
sylvain.thenault@logilab.fr
parents: 540
diff changeset
    90
    }
3863
fe22502d4ab1 [forms] add setFormsTarget() to the list of postAjaxLoad callbacks to have iframe on ajax-rendered forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3784
diff changeset
    91
    if (typeof setFormsTarget != 'undefined') {
fe22502d4ab1 [forms] add setFormsTarget() to the list of postAjaxLoad callbacks to have iframe on ajax-rendered forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3784
diff changeset
    92
       setFormsTarget(node);
fe22502d4ab1 [forms] add setFormsTarget() to the list of postAjaxLoad callbacks to have iframe on ajax-rendered forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3784
diff changeset
    93
    }
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
    94
    loadDynamicFragments(node);
4830
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4208
diff changeset
    95
    // XXX [3.7] jQuery.one is now used instead jQuery.bind,
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4208
diff changeset
    96
    // jquery.treeview.js can be unpatched accordingly.
10e8bc190695 [javascript] fix #736185: add_onload / jQuery.bind() vs. jQuery.one()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4208
diff changeset
    97
    jQuery(CubicWeb).trigger('server-response', [true, node]);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    98
}
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
    99
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   100
/* cubicweb loadxhtml plugin to make jquery handle xhtml response
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   101
 *
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   102
 * fetches `url` and replaces this's content with the result
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   103
 *
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   104
 * @param mode how the replacement should be done (default is 'replace')
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   105
 *  Possible values are :
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   106
 *    - 'replace' to replace the node's content with the generated HTML
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   107
 *    - 'swap' to replace the node itself with the generated HTML
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   108
 *    - 'append' to append the generated HTML to the node's content
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   109
 */
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
jQuery.fn.loadxhtml = function(url, data, reqtype, mode) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    var ajax = null;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
    if (reqtype == 'post') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   113
        ajax = jQuery.post;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    } else {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   115
        ajax = jQuery.get;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    if (this.size() > 1) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   118
        log('loadxhtml was called with more than one element');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    }
642
2cf7b79d8e77 [javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 540
diff changeset
   120
    var node = this.get(0); // only consider the first element
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    mode = mode || 'replace';
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
    var callback = null;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
    if (data && data.callback) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   124
        callback = data.callback;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   125
        delete data.callback;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
    ajax(url, data, function(response) {
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   128
        var domnode = getDomFromResponse(response);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   129
        domnode = preprocessAjaxLoad(node, domnode);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   130
        if (mode == 'swap') {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   131
            var origId = node.id;
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   132
            node = swapDOM(node, domnode);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   133
            if (!node.id) {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   134
                node.id = origId;
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   135
            }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   136
        } else if (mode == 'replace') {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   137
            jQuery(node).empty().append(domnode);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   138
        } else if (mode == 'append') {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   139
            jQuery(node).append(domnode);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   140
        }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   141
        postAjaxLoad(node);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   142
        while (jQuery.isFunction(callback)) {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   143
            callback = callback.apply(this, [domnode]);
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   144
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    });
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   146
};
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
/* finds each dynamic fragment in the page and executes the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
 * the associated RQL to build them (Async call)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
 */
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   153
function loadDynamicFragments(node) {
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   154
    if (node) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   155
        var fragments = jQuery(node).find('div.dynamicFragment');
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   156
    } else {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   157
        var fragments = jQuery('div.dynamicFragment');
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   158
    }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
    if (fragments.length == 0) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   160
        return;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
    if (typeof LOADING_MSG == 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   163
        LOADING_MSG = 'loading'; // this is only a safety belt, it should not happen
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
    for(var i=0; i<fragments.length; i++) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   166
        var fragment = fragments[i];
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   167
        fragment.innerHTML = '<h3>' + LOADING_MSG + ' ... <img src="data/loading.gif" /></h3>';
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   168
        // if cubicweb:loadurl is set, just pick the url et send it to loadxhtml
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   169
        var url = getNodeAttribute(fragment, 'cubicweb:loadurl');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   170
        if (url) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   171
            jQuery(fragment).loadxhtml(url);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   172
            continue;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   173
        }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   174
        // else: rebuild full url by fetching cubicweb:rql, cubicweb:vid, etc.
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   175
        var rql = getNodeAttribute(fragment, 'cubicweb:rql');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   176
        var items = getNodeAttribute(fragment, 'cubicweb:vid').split('&');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   177
        var vid = items[0];
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        var extraparams = {};
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   179
        // case where vid='myvid&param1=val1&param2=val2': this is a deprecated abuse-case
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   180
        if (items.length > 1) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   181
            console.log("[3.5] you're using extraargs in cubicweb:vid attribute, this is deprecated, consider using loadurl instead");
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   182
            for (var j=1; j<items.length; j++) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   183
                var keyvalue = items[j].split('=');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   184
                extraparams[keyvalue[0]] = keyvalue[1];
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   185
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   186
        }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   187
        var actrql = getNodeAttribute(fragment, 'cubicweb:actualrql');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   188
        if (actrql) { extraparams['actualrql'] = actrql; }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   189
        var fbvid = getNodeAttribute(fragment, 'cubicweb:fallbackvid');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   190
        if (fbvid) { extraparams['fallbackvid'] = fbvid; }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   191
        replacePageChunk(fragment.id, rql, vid, extraparams);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   195
jQuery(document).ready(function() {loadDynamicFragments();});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
//============= base AJAX functions to make remote calls =====================//
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
function remoteCallFailed(err, req) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    if (req.status == 500) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   201
        updateMessage(err);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    } else {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   203
        updateMessage(_("an error occured while processing your request"));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
/*
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   209
 * This function will call **synchronously** a remote method on the cubicweb server
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   210
 * @param fname: the function name to call (as exposed by the JSONController)
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   211
 *
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   212
 * additional arguments will be directly passed to the specified function
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
 *
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   214
 * It looks at http headers to guess the response type.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
 */
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   216
function remoteExec(fname /* ... */) {
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   217
    setProgressCursor();
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   218
    var props = {'fname' : fname, 'pageid' : pageid,
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   219
                      'arg': map(jQuery.toJSON, sliceList(arguments, 1))};
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   220
    var result  = jQuery.ajax({url: JSON_BASE_URL, data: props, async: false}).responseText;
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   221
    if (result) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   222
        result = evalJSON(result);
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   223
    }
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   224
    resetCursor();
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   225
    return result;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
/*
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   229
 * This function will call **asynchronously** a remote method on the json
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   230
 * controller of the cubicweb http server
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   231
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
 * @param fname: the function name to call (as exposed by the JSONController)
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   233
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
 * additional arguments will be directly passed to the specified function
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   235
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
 * It looks at http headers to guess the response type.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
 */
1563
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   238
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   239
function asyncRemoteExec(fname /* ... */) {
1563
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   240
    setProgressCursor();
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   241
    var props = {'fname' : fname, 'pageid' : pageid,
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4830
diff changeset
   242
                 'arg': map(jQuery.toJSON, sliceList(arguments, 1))};
5186
f3c2cb460ad9 [doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5157
diff changeset
   243
    // XXX we should inline the content of loadRemote here
1563
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   244
    var deferred = loadRemote(JSON_BASE_URL, props, 'POST');
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   245
    deferred = deferred.addErrback(remoteCallFailed);
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   246
    deferred = deferred.addErrback(resetCursor);
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   247
    deferred = deferred.addCallback(resetCursor);
b130c6cec8c2 loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1512
diff changeset
   248
    return deferred;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
/* emulation of gettext's _ shortcut
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
function _(message) {
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   255
    return remoteExec('i18n', [message])[0];
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
function userCallback(cbname) {
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   259
    asyncRemoteExec('user_callback', cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
function unloadPageData() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
    // NOTE: do not make async calls on unload if you want to avoid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
    //       strange bugs
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   265
    remoteExec('unload_page_data');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
function openHash() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
    if (document.location.hash) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   270
        var nid = document.location.hash.replace('#', '');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   271
        var node = jQuery('#' + nid);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   272
        if (node) { removeElementClass(node, "hidden"); }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
    };
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
jQuery(document).ready(openHash);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
function reloadComponent(compid, rql, registry, nodeid, extraargs) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
    registry = registry || 'components';
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
    rql = rql || '';
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
    nodeid = nodeid || (compid + 'Component');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
    extraargs = extraargs || {};
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
    var node = getNode(nodeid);
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   283
    var d = asyncRemoteExec('component', compid, rql, registry, extraargs);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    d.addCallback(function(result, req) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   285
        var domnode = getDomFromResponse(result);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   286
        if (node) {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   287
            // make sure the component is visible
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   288
            removeElementClass(node, "hidden");
5506
0ee2edec2d7a [ajax] missing html heads processing in reloadComponent (sic\!)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5186
diff changeset
   289
            domnode = preprocessAjaxLoad(node, domnode);
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   290
            swapDOM(node, domnode);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   291
            postAjaxLoad(domnode);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   292
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
    d.addCallback(resetCursor);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
    d.addErrback(function(xxx) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   296
        updateMessage(_("an error occured"));
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   297
        log(xxx);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
  return d;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
/* XXX: HTML architecture of cubicweb boxes is a bit strange */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
function reloadBox(boxid, rql) {
225
b90904dc3069 [javascript] reloadBox: return deferred for further callback chaining
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 12
diff changeset
   304
    return reloadComponent(boxid, rql, 'boxes', boxid);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
function userCallbackThenUpdateUI(cbname, compid, rql, msg, registry, nodeid) {
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   308
    var d = asyncRemoteExec('user_callback', cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
    d.addCallback(function() {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   310
        reloadComponent(compid, rql, registry, nodeid);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   311
        if (msg) { updateMessage(msg); }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
    d.addCallback(resetCursor);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
    d.addErrback(function(xxx) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   315
        updateMessage(_("an error occured"));
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   316
        log(xxx);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   317
        return resetCursor();
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
function userCallbackThenReloadPage(cbname, msg) {
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   322
    var d = asyncRemoteExec('user_callback', cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    d.addCallback(function() {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   324
        window.location.reload();
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   325
        if (msg) { updateMessage(msg); }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
    d.addCallback(resetCursor);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
    d.addErrback(function(xxx) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   329
        updateMessage(_("an error occured"));
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   330
        log(xxx);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   331
        return resetCursor();
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
/*
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
 * unregisters the python function registered on the server's side
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
 * while the page was generated.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
function unregisterUserCallback(cbname) {
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   340
    var d = asyncRemoteExec('unregister_user_callback', cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
    d.addCallback(function() {resetCursor();});
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
    d.addErrback(function(xxx) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   343
        updateMessage(_("an error occured"));
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   344
        log(xxx);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   345
        return resetCursor();
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
/* executes an async query to the server and replaces a node's
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
 * content with the query result
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
 * @param nodeId the placeholder node's id
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
 * @param rql the RQL query
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
 * @param vid the vid to apply to the RQL selection (default if not specified)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
 * @param extraparmas table of additional query parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
function replacePageChunk(nodeId, rql, vid, extraparams, /* ... */ swap, callback) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
    var params = null;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
    if (callback) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   361
        params = {callback: callback};
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
    var node = jQuery('#' + nodeId)[0];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
    var props = {};
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
    if (node) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   367
        props['rql'] = rql;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   368
        props['fname'] = 'view';
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   369
        props['pageid'] = pageid;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   370
        if (vid) { props['vid'] = vid; }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   371
        if (extraparams) { jQuery.extend(props, extraparams); }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   372
        // FIXME we need to do asURL(props) manually instead of
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   373
        // passing `props` directly to loadxml because replacePageChunk
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   374
        // is sometimes called (abusively) with some extra parameters in `vid`
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   375
        var mode = swap?'swap':'replace';
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   376
        var url = JSON_BASE_URL + asURL(props);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   377
        jQuery(node).loadxhtml(url, params, 'get', mode);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
    } else {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   379
        log('Node', nodeId, 'not found');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   383
/* XXX deprecates?
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   384
 * fetches `url` and replaces `nodeid`'s content with the result
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   385
 * @param replacemode how the replacement should be done (default is 'replace')
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   386
 *  Possible values are :
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   387
 *    - 'replace' to replace the node's content with the generated HTML
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   388
 *    - 'swap' to replace the node itself with the generated HTML
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   389
 *    - 'append' to append the generated HTML to the node's content
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   390
 */
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   391
function loadxhtml(nodeid, url, /* ... */ replacemode) {
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   392
    jQuery('#' + nodeid).loadxhtml(url, null, 'post', replacemode);
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   393
}
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1563
diff changeset
   394
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
/* XXX: this function should go in edition.js but as for now, htmlReplace
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
 * references it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
 * replace all textareas with fckeditors.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
function buildWysiwygEditors(parent) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
    jQuery('textarea').each(function () {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   402
        if (this.getAttribute('cubicweb:type') == 'wysiwyg') {
3060
ea967a4f6ccb avoid installing editor n times for n ajax loads
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3011
diff changeset
   403
            // mark editor as instanciated, we may be called a number of times
ea967a4f6ccb avoid installing editor n times for n ajax loads
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3011
diff changeset
   404
            // (see postAjaxLoad)
ea967a4f6ccb avoid installing editor n times for n ajax loads
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3011
diff changeset
   405
            this.setAttribute('cubicweb:type', 'fckeditor');
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   406
            if (typeof FCKeditor != "undefined") {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   407
                var fck = new FCKeditor(this.id);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   408
                fck.Config['CustomConfigurationsPath'] = fckconfigpath;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   409
                fck.Config['DefaultLanguage'] = fcklang;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   410
                fck.BasePath = "fckeditor/";
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   411
                fck.ReplaceTextarea();
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   412
            } else {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   413
                log('fckeditor could not be found.');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   414
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   415
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   418
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
jQuery(document).ready(buildWysiwygEditors);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   422
/*
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   423
 * takes a list of DOM nodes and removes all empty text nodes
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   424
 */
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   425
function stripEmptyTextNodes(nodelist) {
4208
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   426
    /* this DROPS empty text nodes */
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   427
    var stripped = [];
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   428
    for (var i=0; i < nodelist.length; i++) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   429
        var node = nodelist[i];
4208
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   430
        if (isTextNode(node)) {
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   431
             /* all browsers but FF -> innerText, FF -> textContent  */
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   432
             var text = node.innerText || node.textContent;
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   433
             if (text && !text.strip()) {
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   434
               continue;
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   435
             }
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   436
        } else {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   437
            stripped.push(node);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   438
        }
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   439
    }
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   440
    return stripped;
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   441
}
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   442
3104
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3102
diff changeset
   443
/* convenience function that returns a DOM node based on req's result.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3102
diff changeset
   444
 * XXX clarify the need to clone
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3102
diff changeset
   445
 * */
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   446
function getDomFromResponse(response) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
    if (typeof(response) == 'string') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   448
        var doc = html2dom(response);
3073
09c6fe0db381 do postprocessing as in the xhtml case fix #345293
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3060
diff changeset
   449
    } else {
09c6fe0db381 do postprocessing as in the xhtml case fix #345293
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3060
diff changeset
   450
        var doc = response.documentElement;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
    var children = doc.childNodes;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
    if (!children.length) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   454
        // no child (error cases) => return the whole document
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   455
        return jQuery(doc).clone().context;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
    }
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   457
    children = stripEmptyTextNodes(children);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
    if (children.length == 1) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   459
        // only one child => return it
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   460
        return jQuery(children[0]).clone().context;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
    // several children => wrap them in a single node and return the wrap
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   463
    return DIV({'cubicweb:type': "cwResponseWrapper"},
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   464
               map(function(node) {
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   465
                    return jQuery(node).clone().context;
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   466
            }, children));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
function postJSON(url, data, callback) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
    return jQuery.post(url, data, callback, 'json');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   472
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
function getJSON(url, data, callback){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
    return jQuery.get(url, data, callback, 'json');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
CubicWeb.provide('ajax.js');