web/data/cubicweb.ajax.js
branchtls-sprint
changeset 644 5e5b296a657e
parent 642 2cf7b79d8e77
child 985 6a25c58a1c23
equal deleted inserted replaced
640:8e64f12be69c 644:5e5b296a657e
     7 CubicWeb.require('python.js');
     7 CubicWeb.require('python.js');
     8 CubicWeb.require('htmlhelpers.js');
     8 CubicWeb.require('htmlhelpers.js');
     9 
     9 
    10 var JSON_BASE_URL = baseuri() + 'json?';
    10 var JSON_BASE_URL = baseuri() + 'json?';
    11 
    11 
    12 function postAjaxLoad(node) {
    12 /*
       
    13  * inspect dom response, search for a <div class="ajaxHtmlHead"> node and
       
    14  * put its content into the real document's head.
       
    15  * This enables dynamic css and js loading and is used by replacePageChunk
       
    16  */
       
    17 function loadAjaxHtmlHead(node) {
       
    18     jQuery(node).find('div.ajaxHtmlHead').appendTo(jQuery('head'));
       
    19 }
       
    20 
       
    21 function postAjaxLoad(node, req) {
       
    22     // addStylesheets(evalJSON(req.getResponseHeader('X-Cubicweb-Stylesheets') || '[]'));
       
    23     loadAjaxHtmlHead(node);
    13     // find sortable tables if there are some
    24     // find sortable tables if there are some
    14     if (typeof(Sortable) != 'undefined') {
    25     if (typeof(Sortable) != 'undefined') {
    15 	Sortable.sortTables(node);
    26 	Sortable.sortTables(node);
    16     }
    27     }
    17     // find textareas and wrap them if there are some
    28     // find textareas and wrap them if there are some
    26     }
    37     }
    27 }
    38 }
    28 
    39 
    29 // cubicweb loadxhtml plugin to make jquery handle xhtml response
    40 // cubicweb loadxhtml plugin to make jquery handle xhtml response
    30 jQuery.fn.loadxhtml = function(url, data, reqtype, mode) {
    41 jQuery.fn.loadxhtml = function(url, data, reqtype, mode) {
    31     var ajax = null;
       
    32     if (reqtype == 'post') {
       
    33 	ajax = jQuery.post;
       
    34     } else {
       
    35 	ajax = jQuery.get;
       
    36     }
       
    37     if (this.size() > 1) {
    42     if (this.size() > 1) {
    38 	log('loadxhtml was called with more than one element');
    43 	log('loadxhtml was called with more than one element');
    39     }
    44     }
       
    45     var node = this.get(0); // only consider the first element
    40     mode = mode || 'replace';
    46     mode = mode || 'replace';
    41     var callback = null;
    47     var callback = null;
    42     if (data && data.callback) {
    48     if (data && data.callback) {
    43 	callback = data.callback;
    49 	callback = data.callback;
    44 	delete data.callback;
    50 	delete data.callback;
    45     }
    51     }
    46     var node = this.get(0); // only consider the first element
    52     var deferred = loadJSON(url, data, reqtype);
    47     ajax(url, data, function(response) {
    53     deferred.addCallback(function(response, req) {
    48 	var domnode = getDomFromResponse(response);
    54 	var domnode = getDomFromResponse(response);
    49 	if (mode == 'swap') {
    55 	if (mode == 'swap') {
    50 	    var origId = node.id;
    56 	    var origId = node.id;
    51 	    node = swapDOM(node, domnode);
    57 	    node = swapDOM(node, domnode);
    52 	    if (!node.id) {
    58 	    if (!node.id) {
    55 	} else if (mode == 'replace') {
    61 	} else if (mode == 'replace') {
    56 	    jQuery(node).empty().append(domnode);
    62 	    jQuery(node).empty().append(domnode);
    57 	} else if (mode == 'append') {
    63 	} else if (mode == 'append') {
    58 	    jQuery(node).append(domnode);
    64 	    jQuery(node).append(domnode);
    59 	}
    65 	}
    60 	postAjaxLoad(node);
    66 	postAjaxLoad(node, req);
    61 	while (jQuery.isFunction(callback)) {
    67 	while (jQuery.isFunction(callback)) {
    62 	    callback = callback.apply(this, [domnode]);
    68 	    callback = callback.apply(this, [domnode]);
    63 	}
    69 	}
    64     });
    70     });
    65 };
    71 };