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