web/data/cubicweb.ajax.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 13 Jul 2010 17:22:15 +0200
changeset 5971 6109bdb8ac06
parent 5947 e13e7833a57a
child 5977 5b1ce013ae32
permissions -rw-r--r--
[ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     1
/* copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     2
 * contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     3
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     4
 * This file is part of CubicWeb.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     5
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     6
 * CubicWeb is free software: you can redistribute it and/or modify it under the
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     7
 * terms of the GNU Lesser General Public License as published by the Free
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     8
 * Software Foundation, either version 2.1 of the License, or (at your option)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
     9
 * any later version.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    10
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    11
 * CubicWeb is distributed in the hope that it will be useful, but WITHOUT
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    13
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    14
 * details.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    15
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    16
 * You should have received a copy of the GNU Lesser General Public License along
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    17
 * with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
5698
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    20
/**
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    21
 * .. function:: Deferred
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    22
 *
5727
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
    23
 * dummy ultra minimalist implementation of deferred for jQuery
5698
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    24
 */
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    25
function Deferred() {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    26
    this.__init__(this);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    27
}
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    28
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    29
jQuery.extend(Deferred.prototype, {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    30
    __init__: function() {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    31
        this._onSuccess = [];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    32
        this._onFailure = [];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    33
        this._req = null;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    34
        this._result = null;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    35
        this._error = null;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    36
    },
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    37
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    38
    addCallback: function(callback) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    39
        if (this._req.readyState == 4) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    40
            if (this._result) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    41
                var args = [this._result, this._req];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    42
                jQuery.merge(args, cw.utils.sliceList(arguments, 1));
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    43
                callback.apply(null, args);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    44
            }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    45
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    46
        else {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    47
            this._onSuccess.push([callback, cw.utils.sliceList(arguments, 1)]);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    48
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    49
        return this;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    50
    },
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    51
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    52
    addErrback: function(callback) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    53
        if (this._req.readyState == 4) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    54
            if (this._error) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    55
                callback.apply(null, [this._error, this._req]);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    56
            }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    57
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    58
        else {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    59
            this._onFailure.push([callback, cw.utils.sliceList(arguments, 1)]);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    60
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    61
        return this;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    62
    },
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    63
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    64
    success: function(result) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    65
        this._result = result;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    66
        try {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    67
            for (var i = 0; i < this._onSuccess.length; i++) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    68
                var callback = this._onSuccess[i][0];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    69
                var args = [result, this._req];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    70
                jQuery.merge(args, this._onSuccess[i][1]);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    71
                callback.apply(null, args);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    72
            }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    73
        } catch(error) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    74
            this.error(this.xhr, null, error);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    75
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    76
    },
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    77
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    78
    error: function(xhr, status, error) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    79
        this._error = error;
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    80
        for (var i = 0; i < this._onFailure.length; i++) {
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    81
            var callback = this._onFailure[i][0];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    82
            var args = [error, this._req];
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    83
            jQuery.merge(args, this._onFailure[i][1]);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    84
            callback.apply(null, args);
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    85
        }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    86
    }
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    87
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    88
});
5c8fa1650299 [javascript] move Deferred implementation to cubicweb.ajax.js
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5658
diff changeset
    89
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
var JSON_BASE_URL = baseuri() + 'json?';
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
    93
//============= utility function handling remote calls responses. ==============//
5947
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
    94
function _loadAjaxHtmlHead($node, $head, tag, srcattr) {
2257
2c9eceeaae6a fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1801
diff changeset
    95
    var jqtagfilter = tag + '[' + srcattr + ']';
5971
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
    96
    if (cw['loaded_'+srcattr] === undefined) {
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
    97
	cw['loaded_'+srcattr] = [];
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
    98
	var loaded = cw['loaded_'+srcattr];
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
    99
	jQuery('head ' + jqtagfilter).each(function(i) {
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   100
		loaded.push(this.getAttribute(srcattr));
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   101
	    });
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   102
    } else {
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   103
	var loaded = cw['loaded_'+srcattr];
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   104
    }
5947
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   105
    $node.find(tag).each(function(i) {
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   106
	 var url = this.getAttribute(srcattr);
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   107
        if (url) {
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   108
            if (jQuery.inArray(url, loaded) == -1) {
5971
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   109
		// take care to <script> tags: jQuery append method script nodes
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   110
		// don't appears in the DOM (See comments on
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   111
		// http://api.jquery.com/append/), which cause undesired
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   112
		// duplicated load in our case. After trying to use bare DOM api
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   113
		// to avoid this, we switched to handle a list of already loaded
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   114
		// stuff ourselves, since bare DOM api gives bug with the
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   115
		// server-response event, since we loose control on when the
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   116
		// script is loaded (jQuery load it immediatly).
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   117
		loaded.push(url);
6109bdb8ac06 [ajax] fix the fix introduced in 3.9.1 to avoid duplicated loads: it cause some other errors since the browser may load the script *after* the server-response event is emited, causing pb if the script interact with this event
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5947
diff changeset
   118
		jQuery(this).appendTo($head);
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   119
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   120
        } else {
5947
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   121
            jQuery(this).appendTo($head);
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   122
        }
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   123
    });
5947
e13e7833a57a [test] remove deprecated test file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5937
diff changeset
   124
    $node.find(jqtagfilter).remove();
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   125
}
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   126
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   127
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   128
 * .. function:: function loadAjaxHtmlHead(response)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   129
 *
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   130
 * 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
   131
 * 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
   132
 * 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
   133
 * 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
   134
 */
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   135
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
   136
    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
   137
    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
   138
    // 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
   139
    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
   140
        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
   141
    }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   142
    _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
   143
    _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
   144
    // 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
   145
    $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
   146
    // 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
   147
    $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
   148
    // 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
   149
    // 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
   150
    // 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
   151
    // 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
   152
    // 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
   153
    //    <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
   154
    // 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
   155
    //    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
   156
    // 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
   157
    //    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
   158
    //    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
   159
    //    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
   160
    //    created by getDomFromResponse()
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   161
    if (response.childNodes.length == 1 && 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
   162
        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
   163
    }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   164
    return response;
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   165
}
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   166
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   167
function _postAjaxLoad(node) {
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   168
    // 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
   169
    if (typeof(Sortable) != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   170
        Sortable.sortTables(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   171
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   172
    // 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
   173
    if (typeof(FCKeditor) != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   174
        buildWysiwygEditors();
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   175
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   176
    if (typeof initFacetBoxEvents != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   177
        initFacetBoxEvents(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   178
    }
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   179
    if (typeof buildWidgets != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   180
        buildWidgets(node);
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   181
    }
2338
3f7c7fbae94e call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2257
diff changeset
   182
    if (typeof roundedCorners != 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   183
        roundedCorners(node);
955
3017a858bbc2 round corders on ajax loads
sylvain.thenault@logilab.fr
parents: 540
diff changeset
   184
    }
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
   185
    if (typeof setFormsTarget != 'undefined') {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   186
        setFormsTarget(node);
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
   187
    }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   188
    _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
   189
    // 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
   190
    // 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
   191
    jQuery(CubicWeb).trigger('server-response', [true, node]);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   192
    jQuery(node).trigger('server-response', [true, node]);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   193
}
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   194
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   195
function remoteCallFailed(err, req) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   196
    cw.log(err);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   197
    if (req.status == 500) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   198
        updateMessage(err);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   199
    } else {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   200
        updateMessage(_("an error occured while processing your request"));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   201
    }
540
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   202
}
e5c97f6f119d [javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 492
diff changeset
   203
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   204
//============= base AJAX functions to make remote calls =====================//
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   205
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   206
 * .. function:: ajaxFuncArgs(fname, form, *args)
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   207
 *
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   208
 * extend `form` parameters to call the js_`fname` function of the json
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   209
 * controller with `args` arguments.
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   210
 */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   211
function ajaxFuncArgs(fname, form /* ... */) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   212
    form = form || {};
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   213
    $.extend(form, {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   214
        'fname': fname,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   215
        'pageid': pageid,
5727
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   216
        'arg': $.map(cw.utils.sliceList(arguments, 2), jQuery.toJSON)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   217
    });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   218
    return form;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   219
}
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   220
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   221
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   222
 * .. function:: loadxhtml(url, form, reqtype='get', mode='replace', cursor=true)
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   223
 *
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   224
 * build url given by absolute or relative `url` and `form` parameters
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   225
 * (dictionary), fetch it using `reqtype` method, then evaluate the
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   226
 * returned XHTML and insert it according to `mode` in the
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   227
 * document. Possible modes are :
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   228
 *
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   229
 *    - 'replace' to replace the node's content with the generated HTML
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   230
 *    - 'swap' to replace the node itself with the generated HTML
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   231
 *    - 'append' to append the generated HTML to the node's content
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   232
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   233
 * If `cursor`, turn mouse cursor into 'progress' cursor until the remote call
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   234
 * is back.
3011
657dfe374b08 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2338
diff changeset
   235
 */
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   236
jQuery.fn.loadxhtml = function(url, form, reqtype, mode, cursor) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   237
    if (this.size() > 1) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   238
        cw.log('loadxhtml was called with more than one element');
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   240
    var callback = null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   241
    if (form && form.callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   242
        cw.log('[3.9] callback given through form.callback is deprecated, add ' + 'callback on the defered');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   243
        callback = form.callback;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   244
        delete form.callback;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
    }
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
   246
    var node = this.get(0); // only consider the first element
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   247
    if (cursor) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   248
        setProgressCursor();
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
    }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   250
    var d = loadRemote(url, form, reqtype);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   251
    d.addCallback(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
   252
        var domnode = getDomFromResponse(response);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   253
        domnode = loadAjaxHtmlHead(domnode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   254
        mode = mode || 'replace';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   255
        // make sure the component is visible
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   256
        $(node).removeClass("hidden");
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   257
        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
   258
            var origId = node.id;
5937
82cac9383dd8 [javascript] more cleanups and deprecation warnings removal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5909
diff changeset
   259
            node = cw.swapDOM(node, domnode);
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   260
            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
   261
                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
   262
            }
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   263
        } 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
   264
            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
   265
        } 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
   266
            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
   267
        }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   268
        _postAjaxLoad(node);
4174
860f622a31aa [javascript] remove ajaxHtmlHead from dom response once it has been processed.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3863
diff changeset
   269
        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
   270
            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
   271
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
    });
5937
82cac9383dd8 [javascript] more cleanups and deprecation warnings removal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5909
diff changeset
   273
    d.addErrback(remoteCallFailed);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   274
    if (cursor) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   275
        d.addCallback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   276
        d.addErrback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   277
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   278
    return d;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   279
}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   281
/**
5937
82cac9383dd8 [javascript] more cleanups and deprecation warnings removal
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5909
diff changeset
   282
 * .. function:: loadRemote(url, form, reqtype='GET', sync=false)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   283
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   284
 * Asynchronously (unless `async` argument is set to false) load an url or path
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   285
 * and return a deferred whose callbacks args are decoded according to the
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   286
 * Content-Type response header. `form` should be additional form params
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   287
 * dictionary, `reqtype` the HTTP request type (get 'GET' or 'POST').
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   288
 */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   289
function loadRemote(url, form, reqtype, sync) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   290
    if (!url.startswith(baseuri())) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   291
        url = baseuri() + url;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   292
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   293
    if (!sync) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   294
        var deferred = new Deferred();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   295
        jQuery.ajax({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   296
            url: url,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   297
            type: (reqtype || 'GET').toUpperCase(),
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   298
            data: form,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   299
            async: true,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   300
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   301
            beforeSend: function(xhr) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   302
                deferred._req = xhr;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   303
            },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   304
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   305
            success: function(data, status) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   306
                if (deferred._req.getResponseHeader("content-type") == 'application/json') {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   307
                    data = cw.evalJSON(data);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   308
                }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   309
                deferred.success(data);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   310
            },
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   312
            error: function(xhr, status, error) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   313
                try {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   314
                    if (xhr.status == 500) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   315
                        var reason_dict = cw.evalJSON(xhr.responseText);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   316
                        deferred.error(xhr, status, reason_dict['reason']);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   317
                        return;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   318
                    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   319
                } catch(exc) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   320
                    cw.log('error with server side error report:' + exc);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   321
                }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   322
                deferred.error(xhr, status, null);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   323
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   324
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   325
        return deferred;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   326
    } else {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   327
        var result = jQuery.ajax({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   328
            url: url,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   329
            type: (reqtype || 'GET').toUpperCase(),
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   330
            data: form,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   331
            async: false
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   332
        });
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   333
        if (result) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   334
            // XXX no good reason to force json here, 
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   335
            // it should depends on request content-type
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   336
            result = cw.evalJSON(result.responseText);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   337
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   338
        return result
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   339
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   340
}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   342
//============= higher level AJAX functions using remote calls ===============//
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   343
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   344
 * .. function:: _(message)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   345
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   346
 * emulation of gettext's _ shortcut
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   347
 */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   348
function _(message) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   349
    return loadRemote('json', ajaxFuncArgs('i18n', null, [message]), 'GET', true)[0];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   350
}
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   351
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   352
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   353
 * .. function:: _loadDynamicFragments(node)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   354
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   355
 * finds each dynamic fragment in the page and executes the
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
 * the associated RQL to build them (Async call)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
 */
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   358
function _loadDynamicFragments(node) {
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   359
    if (node) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   360
        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
   361
    } else {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   362
        var fragments = jQuery('div.dynamicFragment');
1512
c51ca5f49a78 fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents: 1498
diff changeset
   363
    }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
    if (fragments.length == 0) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   365
        return;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
    if (typeof LOADING_MSG == 'undefined') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   368
        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
   369
    }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   370
    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
   371
        var fragment = fragments[i];
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   372
        fragment.innerHTML = '<h3>' + LOADING_MSG + ' ... <img src="data/loading.gif" /></h3>';
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   373
        var $fragment = jQuery(fragment);
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   374
        // if cubicweb:loadurl is set, just pick the url et send it to loadxhtml
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   375
        var url = $fragment.attr('cubicweb:loadurl');
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   376
        if (url) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   377
            $fragment.loadxhtml(url);
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   378
            continue;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   379
        }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   380
        // else: rebuild full url by fetching cubicweb:rql, cubicweb:vid, etc.
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   381
        var rql = $fragment.attr('cubicweb:rql');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   382
        var items = $fragment.attr('cubicweb:vid').split('&');
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   383
        var vid = items[0];
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
        var extraparams = {};
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   385
        // 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
   386
        if (items.length > 1) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   387
            cw.log("[3.5] you're using extraargs in cubicweb:vid " +
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   388
                   "attribute, this is deprecated, consider using " +
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   389
                   "loadurl instead");
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   390
            for (var j = 1; j < items.length; j++) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   391
                var keyvalue = items[j].split('=');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   392
                extraparams[keyvalue[0]] = keyvalue[1];
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   393
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   394
        }
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   395
        var actrql = $fragment.attr('cubicweb:actualrql');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   396
        if (actrql) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   397
            extraparams['actualrql'] = actrql;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   398
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   399
        var fbvid = $fragment.attr('cubicweb:fallbackvid');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   400
        if (fbvid) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   401
            extraparams['fallbackvid'] = fbvid;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   402
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   403
        extraparams['rql'] = rql;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   404
        extraparams['vid'] = vid;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   405
        $(fragment.id).loadxhtml('json', ajaxFuncArgs('view', extraparams));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
}
5820
b176ac0e3aaa fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5774
diff changeset
   408
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   409
jQuery(document).ready(function() {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   410
    _loadDynamicFragments();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   411
});
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
function unloadPageData() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
    // 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
   415
    //       strange bugs
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   416
    loadRemote('json', ajaxFuncArgs('unload_page_data'), 'GET', true);
0
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
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   419
function removeBookmark(beid) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   420
    var d = loadRemote('json', ajaxFuncArgs('delete_bookmark', null, beid));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   421
    d.addCallback(function(boxcontent) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   422
        $('#bookmarks_box').loadxhtml('json',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   423
                                      ajaxFuncArgs('render', null, 'boxes',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   424
                                                   'bookmarks_box'));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   425
        document.location.hash = '#header';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   426
        updateMessage(_("bookmark has been removed"));
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   430
function userCallback(cbname) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   431
    setProgressCursor();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   432
    var d = loadRemote('json', ajaxFuncArgs('user_callback', null, cbname));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   433
    d.addCallback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   434
    d.addErrback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   435
    d.addErrback(remoteCallFailed);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   436
    return d;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
function userCallbackThenUpdateUI(cbname, compid, rql, msg, registry, nodeid) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   440
    var d = userCallback(cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
    d.addCallback(function() {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   442
        $('#' + nodeid).loadxhtml('json', ajaxFuncArgs('render', {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   443
            'rql': rql
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   444
        },
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   445
        registry, compid));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   446
        if (msg) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   447
            updateMessage(msg);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   448
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
}
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
function userCallbackThenReloadPage(cbname, msg) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   453
    var d = userCallback(cbname);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
    d.addCallback(function() {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   455
        window.location.reload();
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   456
        if (msg) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   457
            updateMessage(msg);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   458
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   460
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   462
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   463
 * .. function:: unregisterUserCallback(cbname)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   464
 *
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
 * unregisters the python function registered on the server's side
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
 * while the page was generated.
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
function unregisterUserCallback(cbname) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   469
    setProgressCursor();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   470
    var d = loadRemote('json', ajaxFuncArgs('unregister_user_callback',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   471
                                            null, cbname));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   472
    d.addCallback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   473
    d.addErrback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   474
    d.addErrback(remoteCallFailed);
0
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
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   477
//============= XXX move those functions? ====================================//
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
function openHash() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
    if (document.location.hash) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   480
        var nid = document.location.hash.replace('#', '');
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   481
        var node = jQuery('#' + nid);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   482
        if (node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   483
            $(node).removeClass("hidden");
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   484
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
    };
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
jQuery(document).ready(openHash);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   489
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   490
 * .. function:: buildWysiwygEditors(parent)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
 *
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   492
 *XXX: this function should go in edition.js but as for now, htmlReplace
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
 * references it.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
 * replace all textareas with fckeditors.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
function buildWysiwygEditors(parent) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   498
    jQuery('textarea').each(function() {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   499
        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
   500
            // mark editor as instanciated, we may be called a number of times
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   501
            // (see _postAjaxLoad)
3060
ea967a4f6ccb avoid installing editor n times for n ajax loads
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3011
diff changeset
   502
            this.setAttribute('cubicweb:type', 'fckeditor');
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   503
            if (typeof FCKeditor != "undefined") {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   504
                var fck = new FCKeditor(this.id);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   505
                fck.Config['CustomConfigurationsPath'] = fckconfigpath;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   506
                fck.Config['DefaultLanguage'] = fcklang;
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   507
                fck.BasePath = "fckeditor/";
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   508
                fck.ReplaceTextarea();
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   509
            } else {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   510
                cw.log('fckeditor could not be found.');
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   511
            }
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   512
        }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
jQuery(document).ready(buildWysiwygEditors);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   517
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   518
 * .. function:: stripEmptyTextNodes(nodelist)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   519
 *
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   520
 * takes a list of DOM nodes and removes all empty text nodes
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   521
 */
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   522
function stripEmptyTextNodes(nodelist) {
4208
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   523
    /* this DROPS empty text nodes */
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   524
    var stripped = [];
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   525
    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
   526
        var node = nodelist[i];
4208
b2826afdaf8c fix stripEmptyTextNodes for IE (part of #615337)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4175
diff changeset
   527
        if (isTextNode(node)) {
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   528
            /* all browsers but FF -> innerText, FF -> textContent  */
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   529
            var text = node.innerText || node.textContent;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   530
            if (text && ! text.strip()) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   531
                continue;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   532
            }
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   533
        } else {
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   534
            stripped.push(node);
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   535
        }
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   536
    }
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   537
    return stripped;
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   538
}
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   539
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   540
/**
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   541
 * .. function:: getDomFromResponse(response)
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   542
 *
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   543
 * convenience function that returns a DOM node based on req's result.
3104
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3102
diff changeset
   544
 * XXX clarify the need to clone
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3102
diff changeset
   545
 * */
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
function getDomFromResponse(response) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
    if (typeof(response) == 'string') {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   548
        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
   549
    } else {
09c6fe0db381 do postprocessing as in the xhtml case fix #345293
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3060
diff changeset
   550
        var doc = response.documentElement;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
    var children = doc.childNodes;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   553
    if (!children.length) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   554
        // no child (error cases) => return the whole document
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   555
        return jQuery(doc).clone().context;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   556
    }
1419
7ff24154351d javascript + json refactoring
sylvain.thenault@logilab.fr
parents: 1407
diff changeset
   557
    children = stripEmptyTextNodes(children);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
    if (children.length == 1) {
4175
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   559
        // only one child => return it
92a10d2a09af [javascript] cleanup, remove tabs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4174
diff changeset
   560
        return jQuery(children[0]).clone().context;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
    // 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
   563
    return DIV({'cubicweb:type': "cwResponseWrapper"},
5727
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   564
	       $.map(children, function(node) {
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   565
		       return jQuery(node).clone().context;})
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   566
	       );
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   569
/* DEPRECATED *****************************************************************/
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   570
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   571
preprocessAjaxLoad = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   572
    '[3.9] preprocessAjaxLoad() is deprecated, use loadAjaxHtmlHead instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   573
    function(node, newdomnode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   574
        return loadAjaxHtmlHead(newdomnode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   575
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   576
);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   577
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   578
reloadComponent = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   579
    '[3.9] reloadComponent() is deprecated, use loadxhtml instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   580
    function(compid, rql, registry, nodeid, extraargs) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   581
        registry = registry || 'components';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   582
        rql = rql || '';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   583
        nodeid = nodeid || (compid + 'Component');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   584
        extraargs = extraargs || {};
5909
9af8ac182f53 [javascript] fix some javascript errors and remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5820
diff changeset
   585
        var node = cw.jqNode(nodeid);
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   586
        return node.loadxhtml('json', ajaxFuncArgs('component', null, compid,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   587
                                                   rql, registry, extraargs));
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   588
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   589
);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   590
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   591
reloadBox = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   592
    '[3.9] reloadBox() is deprecated, use loadxhtml instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   593
    function(boxid, rql) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   594
        return reloadComponent(boxid, rql, 'boxes', boxid);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   595
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   596
);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   598
replacePageChunk = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   599
    '[3.9] replacePageChunk() is deprecated, use loadxhtml instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   600
    function(nodeId, rql, vid, extraparams, /* ... */ swap, callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   601
        var params = null;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   602
        if (callback) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   603
            params = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   604
                callback: callback
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   605
            };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   606
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   607
        var node = jQuery('#' + nodeId)[0];
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   608
        var props = {};
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   609
        if (node) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   610
            props['rql'] = rql;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   611
            props['fname'] = 'view';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   612
            props['pageid'] = pageid;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   613
            if (vid) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   614
                props['vid'] = vid;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   615
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   616
            if (extraparams) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   617
                jQuery.extend(props, extraparams);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   618
            }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   619
            // FIXME we need to do asURL(props) manually instead of
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   620
            // passing `props` directly to loadxml because replacePageChunk
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   621
            // is sometimes called (abusively) with some extra parameters in `vid`
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   622
            var mode = swap ? 'swap': 'replace';
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   623
            var url = JSON_BASE_URL + asURL(props);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   624
            jQuery(node).loadxhtml(url, params, 'get', mode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   625
        } else {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   626
            cw.log('Node', nodeId, 'not found');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   627
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   628
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   629
);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   630
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   631
loadxhtml = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   632
    '[3.9] loadxhtml() function is deprecated, use loadxhtml method instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   633
    function(nodeid, url, /* ... */ replacemode) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   634
        jQuery('#' + nodeid).loadxhtml(url, null, 'post', replacemode);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   635
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   636
);
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   637
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   638
remoteExec = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   639
    '[3.9] remoteExec() is deprecated, use loadRemote instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   640
    function(fname /* ... */) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   641
        setProgressCursor();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   642
        var props = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   643
            'fname': fname,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   644
            'pageid': pageid,
5727
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   645
            'arg': $.map(cw.utils.sliceList(arguments, 1), jQuery.toJSON)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   646
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   647
        var result = jQuery.ajax({
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   648
            url: JSON_BASE_URL,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   649
            data: props,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   650
            async: false
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   651
        }).responseText;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   652
        if (result) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   653
            result = cw.evalJSON(result);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   654
        }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   655
        resetCursor();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   656
        return result;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   657
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   658
);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   659
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   660
asyncRemoteExec = cw.utils.deprecatedFunction(
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   661
    '[3.9] asyncRemoteExec() is deprecated, use loadRemote instead',
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   662
    function(fname /* ... */) {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   663
        setProgressCursor();
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   664
        var props = {
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   665
            'fname': fname,
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   666
            'pageid': pageid,
5727
29afb9e715bb [3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5699
diff changeset
   667
            'arg': $.map(cw.utils.sliceList(arguments, 1), jQuery.toJSON)
5658
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   668
        };
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   669
        // XXX we should inline the content of loadRemote here
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   670
        var deferred = loadRemote(JSON_BASE_URL, props, 'POST');
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   671
        deferred = deferred.addErrback(remoteCallFailed);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   672
        deferred = deferred.addErrback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   673
        deferred = deferred.addCallback(resetCursor);
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   674
        return deferred;
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   675
    }
7b9553a9db65 [ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5506
diff changeset
   676
);