From a77f5cf42faa75a2dd3931d83ad8ea86648248c0 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Tue, 25 Aug 2015 21:50:31 +0200 Subject: [ADD] website_form: generic form controller Contains a new ajax post function that comes from mdo's original code and that I was not able to get rid of. --- addons/web/static/src/js/framework/ajax.js | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'addons/web/static/src/js/framework/ajax.js') diff --git a/addons/web/static/src/js/framework/ajax.js b/addons/web/static/src/js/framework/ajax.js index 444784ef0fd..f8392287b26 100644 --- a/addons/web/static/src/js/framework/ajax.js +++ b/addons/web/static/src/js/framework/ajax.js @@ -2,6 +2,7 @@ odoo.define('web.ajax', function (require) { "use strict"; var time = require('web.time'); +var utils = require('web.utils'); function genericJsonRpc (fct_name, params, fct) { var data = { @@ -274,6 +275,51 @@ function get_file(options) { timer = setTimeout(waitLoop, CHECK_INTERVAL); }; +function post (controller_url, data) { + + var progressHandler = function (deferred) { + return function (state) { + if(state.lengthComputable) { + deferred.notify({ + h_loaded: utils.human_size(state.loaded), + h_total : utils.human_size(state.total), + loaded : state.loaded, + total : state.total, + pcent : Math.round((state.loaded/state.total)*100) + }); + } + }; + }; + + var Def = $.Deferred(); + var compatibility = !(typeof(FormData)); + var postData = compatibility ? new FormDataCompatibility() : new FormData(); + + $.each(data, function(i,val) { + postData.append(i, val); + }); + + var xhr = new XMLHttpRequest(); + if(compatibility) { + postData.setContentTypeHeader(xhr); + postData = postData.buildBody(); + } + + if(xhr.upload) xhr.upload.addEventListener('progress', progressHandler(Def), false); + + var ajaxDef = $.ajax(controller_url, { + xhr: function() {return xhr;}, + data: postData, + processData: false, + contentType: false, + type: 'POST' + }).then(function (data) {Def.resolve(data);}) + .fail(function (data) {Def.reject(data);}); + + return Def; +} + + var loadXML = (function () { var loading = false; @@ -315,6 +361,7 @@ return { loadJS: loadJS, loadXML: loadXML, get_file: get_file, + post: post, }; }); -- cgit v1.2.3