From 9b5abab9a0a29e47585c1e1042bb9f5a259cc063 Mon Sep 17 00:00:00 2001 From: buddh4 Date: Mon, 13 Mar 2017 22:40:42 +0100 Subject: [PATCH] Enh: Added xhr to client response instances Enh: Added response.header for receiving response header from xhr Enh: Added post action to client module for data-action-click="client.post" Fix: Try using options url as fallback in client calls if the action instance does not provide an url. e.g. client.post(evt, {url:...} Enh: Enable setting ajax dataType from trigger e.g. data-action-click="modal.load" data-action-data-type="json" --- protected/humhub/docs/CHANGELOG.md | 5 +++++ static/js/humhub/humhub.client.js | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index d550930ec2..e136048fd4 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -3,6 +3,11 @@ HumHub Change Log 1.2.0-beta.3 under developement -------------------------------- +- Enh: Added xhr to client response instances +- Enh: Added response.header for receiving response header from xhr +- Enh: Added post action to client module for data-action-click="client.post" +- Fix: Try using options url as fallback in client calls if the action instance does not provide an url. e.g. client.post(evt, {url:...} +- Enh: Enable setting ajax dataType from trigger e.g. data-action-click="modal.load" data-action-data-type="json" - Enh: Added action event.data for receiving action specific data options - Enh: Added default run for JsWidget - Enh: Added File::findByRecord for searching all attached files of a given ActiveRecord diff --git a/static/js/humhub/humhub.client.js b/static/js/humhub/humhub.client.js index c3c06b689d..0190689b77 100644 --- a/static/js/humhub/humhub.client.js +++ b/static/js/humhub/humhub.client.js @@ -17,8 +17,9 @@ humhub.module('client', function (module, require, $) { //Textstatus = "timeout", "error", "abort", "parsererror", "application" this.textStatus = textStatus; this.dataType = dataType; + this.xhr = xhr; - var responseType = xhr.getResponseHeader('content-type'); + var responseType = this.header('content-type'); // If we expect json and received json we merge the json result with our response object. if ((!dataType || dataType === 'json') && responseType && responseType.indexOf('json') > -1) { @@ -27,6 +28,10 @@ humhub.module('client', function (module, require, $) { this[dataType] = this.response; } }; + + Response.prototype.header = function (key) { + return this.xhr.getResponseHeader(key); + }; Response.prototype.setSuccess = function (data) { this.data = data; @@ -56,13 +61,11 @@ humhub.module('client', function (module, require, $) { result.response = this.response.substr(0, 500) result.response += (this.response.length > 500) ? '...' : ''; } - ; if (this.html && object.isString(this.html)) { result.html = this.html.substr(0, 500) result.html += (this.html.length > 500) ? '...' : ''; } - ; return result; }; @@ -73,7 +76,7 @@ humhub.module('client', function (module, require, $) { } else { location.reload(true); } - } + }; var submit = function ($form, cfg, originalEvent) { if ($form instanceof $.Event && $form.$form && $form.length) { @@ -99,11 +102,17 @@ humhub.module('client', function (module, require, $) { var url = cfg.url || originalEvent.url || $form.attr('action'); return ajax(url, cfg, originalEvent); }; + + var actionPost = function (evt) { + post(evt).catch(function(e) { + module.log.error(e, true); + }); + }; var post = function (url, cfg, originalEvent) { if (url instanceof $.Event) { originalEvent = url; - url = originalEvent.url; + url = originalEvent.url || cfg.url; } else if (cfg instanceof $.Event) { originalEvent = cfg; cfg = {}; @@ -120,7 +129,7 @@ humhub.module('client', function (module, require, $) { var html = function (url, cfg, originalEvent) { if (url instanceof $.Event) { originalEvent = url; - url = originalEvent.url; + url = originalEvent.url || cfg.url; } else if (cfg instanceof $.Event) { originalEvent = cfg; cfg = {}; @@ -138,7 +147,7 @@ humhub.module('client', function (module, require, $) { var get = function (url, cfg, originalEvent) { if (url instanceof $.Event) { originalEvent = url; - url = originalEvent.url; + url = originalEvent.url || cfg.url; } else if (cfg instanceof $.Event) { originalEvent = cfg; cfg = {}; @@ -162,9 +171,13 @@ humhub.module('client', function (module, require, $) { cfg = {'success': cfg}; } - var promise = new Promise(function (resolve, reject) { cfg = cfg || {}; + + // allows data-action-data-type="json" on $trigger + if(originalEvent && object.isFunction(originalEvent.data)) { + cfg.dataType = originalEvent.data('data-type', cfg.dataType); + } var errorHandler = cfg.error; var error = function (xhr, textStatus, errorThrown) { @@ -285,6 +298,7 @@ humhub.module('client', function (module, require, $) { module.export({ ajax: ajax, + actionPost: actionPost, post: post, get: get, html: html,