From 27d580c4f731c39439f8236db08f62768af9eb93 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 19 Feb 2016 13:55:04 +0100 Subject: [PATCH 1/2] MOBILE-1160 media: Fix media in book, page and resource --- www/addons/mod_book/services/book.js | 18 ++++++++++-------- www/addons/mod_page/services/page.js | 19 +++++++++++-------- www/addons/mod_resource/services/resource.js | 15 +++++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/www/addons/mod_book/services/book.js b/www/addons/mod_book/services/book.js index ea467b9dcd9..32a5cc7093f 100644 --- a/www/addons/mod_book/services/book.js +++ b/www/addons/mod_book/services/book.js @@ -262,7 +262,6 @@ angular.module('mm.addons.mod_book') // Promise handling when we are in a browser. promise = (function() { - var deferred; if (!indexUrl) { // If ever that happens. $log.debug('Could not locate the index chapter'); @@ -272,9 +271,7 @@ angular.module('mm.addons.mod_book') return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModBookComponent, moduleId); } else { // We return the live URL. - deferred = $q.defer(); - deferred.resolve($mmSite.fixPluginfileURL(indexUrl)); - return deferred.promise; + return $q.when($mmSite.fixPluginfileURL(indexUrl)); } })(); @@ -286,12 +283,16 @@ angular.module('mm.addons.mod_book') } else { // Now that we have the content, we update the SRC to point back to // the external resource. That will be caught by mm-format-text. - var html = angular.element('
'); + var html = angular.element('
'), + media; html.html(response.data); - angular.forEach(html.find('img'), function(img) { - var src = paths[decodeURIComponent(img.getAttribute('src'))]; + + // Treat img, audio, video and source. + media = html[0].querySelectorAll('img, video, audio, source'); + angular.forEach(media, function(el) { + var src = paths[decodeURIComponent(el.getAttribute('src'))]; if (typeof src !== 'undefined') { - img.setAttribute('src', src); + el.setAttribute('src', src); } }); // We do the same for links. @@ -301,6 +302,7 @@ angular.module('mm.addons.mod_book') anchor.setAttribute('href', href); } }); + return html.html(); } }); diff --git a/www/addons/mod_page/services/page.js b/www/addons/mod_page/services/page.js index 6a0055f43d0..d100ed61847 100644 --- a/www/addons/mod_page/services/page.js +++ b/www/addons/mod_page/services/page.js @@ -164,9 +164,7 @@ angular.module('mm.addons.mod_page') return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModPageComponent, moduleId); } else { // We return the live URL. - deferred = $q.defer(); - deferred.resolve($mmSite.fixPluginfileURL(indexUrl)); - return deferred.promise; + return $q.when($mmSite.fixPluginfileURL(indexUrl)); } })(); @@ -177,13 +175,17 @@ angular.module('mm.addons.mod_page') return $q.reject(); } else { // Now that we have the content, we update the SRC to point back to - // the external resource. That will be caught by mm-format-text. - var html = angular.element('
'); + // the external resource. That will b caught by mm-format-text. + var html = angular.element('
'), + media; html.html(response.data); - angular.forEach(html.find('img'), function(img) { - var src = paths[decodeURIComponent(img.getAttribute('src'))]; + + // Treat img, audio, video and source. + media = html[0].querySelectorAll('img, video, audio, source'); + angular.forEach(media, function(el) { + var src = paths[decodeURIComponent(el.getAttribute('src'))]; if (typeof src !== 'undefined') { - img.setAttribute('src', src); + el.setAttribute('src', src); } }); // We do the same for links. @@ -193,6 +195,7 @@ angular.module('mm.addons.mod_page') anchor.setAttribute('href', href); } }); + return html.html(); } }); diff --git a/www/addons/mod_resource/services/resource.js b/www/addons/mod_resource/services/resource.js index 430c38a74ac..eaaa8153bcb 100644 --- a/www/addons/mod_resource/services/resource.js +++ b/www/addons/mod_resource/services/resource.js @@ -227,13 +227,16 @@ angular.module('mm.addons.mod_resource') } else { // Now that we have the content, we update the SRC to point back to // the external resource. That will be caught by mm-format-text. - var html = angular.element('
'); - html.append(response.data); - - angular.forEach(html.find('img'), function(img) { - var src = paths[decodeURIComponent(img.getAttribute('src'))]; + var html = angular.element('
'), + media; + html.html(response.data); + + // Treat img, audio, video and source. + media = html[0].querySelectorAll('img, video, audio, source'); + angular.forEach(media, function(el) { + var src = paths[decodeURIComponent(el.getAttribute('src'))]; if (typeof src !== 'undefined') { - img.setAttribute('src', src); + el.setAttribute('src', src); } }); // We do the same for links. From 8b561b315ff43c2d9b414bc970afb560da845dc0 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 23 Feb 2016 11:43:39 +0100 Subject: [PATCH 2/2] MOBILE-1160 media: Move some common code to mmUtil --- www/addons/mod_book/services/book.js | 24 +---------- www/addons/mod_page/services/page.js | 24 +---------- www/addons/mod_resource/services/resource.js | 30 +++----------- www/core/lib/util.js | 42 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 69 deletions(-) diff --git a/www/addons/mod_book/services/book.js b/www/addons/mod_book/services/book.js index 32a5cc7093f..825c904b289 100644 --- a/www/addons/mod_book/services/book.js +++ b/www/addons/mod_book/services/book.js @@ -21,7 +21,7 @@ angular.module('mm.addons.mod_book') * @ngdoc service * @name $mmaModBook */ -.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModBookComponent) { +.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModBookComponent) { $log = $log.getInstance('$mmaModBook'); var self = {}; @@ -283,27 +283,7 @@ angular.module('mm.addons.mod_book') } else { // Now that we have the content, we update the SRC to point back to // the external resource. That will be caught by mm-format-text. - var html = angular.element('
'), - media; - html.html(response.data); - - // Treat img, audio, video and source. - media = html[0].querySelectorAll('img, video, audio, source'); - angular.forEach(media, function(el) { - var src = paths[decodeURIComponent(el.getAttribute('src'))]; - if (typeof src !== 'undefined') { - el.setAttribute('src', src); - } - }); - // We do the same for links. - angular.forEach(html.find('a'), function(anchor) { - var href = paths[decodeURIComponent(anchor.getAttribute('href'))]; - if (typeof href !== 'undefined') { - anchor.setAttribute('href', href); - } - }); - - return html.html(); + return $mmUtil.restoreSourcesInHtml(response.data, paths); } }); }); diff --git a/www/addons/mod_page/services/page.js b/www/addons/mod_page/services/page.js index d100ed61847..2ffda451264 100644 --- a/www/addons/mod_page/services/page.js +++ b/www/addons/mod_page/services/page.js @@ -21,7 +21,7 @@ angular.module('mm.addons.mod_page') * @ngdoc service * @name $mmaModPage */ -.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModPageComponent) { +.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModPageComponent) { $log = $log.getInstance('$mmaModPage'); var self = {}; @@ -176,27 +176,7 @@ angular.module('mm.addons.mod_page') } else { // Now that we have the content, we update the SRC to point back to // the external resource. That will b caught by mm-format-text. - var html = angular.element('
'), - media; - html.html(response.data); - - // Treat img, audio, video and source. - media = html[0].querySelectorAll('img, video, audio, source'); - angular.forEach(media, function(el) { - var src = paths[decodeURIComponent(el.getAttribute('src'))]; - if (typeof src !== 'undefined') { - el.setAttribute('src', src); - } - }); - // We do the same for links. - angular.forEach(html.find('a'), function(anchor) { - var href = paths[decodeURIComponent(anchor.getAttribute('href'))]; - if (typeof href !== 'undefined') { - anchor.setAttribute('href', href); - } - }); - - return html.html(); + return $mmUtil.restoreSourcesInHtml(response.data, paths); } }); }); diff --git a/www/addons/mod_resource/services/resource.js b/www/addons/mod_resource/services/resource.js index eaaa8153bcb..f1e27bc041d 100644 --- a/www/addons/mod_resource/services/resource.js +++ b/www/addons/mod_resource/services/resource.js @@ -227,33 +227,13 @@ angular.module('mm.addons.mod_resource') } else { // Now that we have the content, we update the SRC to point back to // the external resource. That will be caught by mm-format-text. - var html = angular.element('
'), - media; - html.html(response.data); - - // Treat img, audio, video and source. - media = html[0].querySelectorAll('img, video, audio, source'); - angular.forEach(media, function(el) { - var src = paths[decodeURIComponent(el.getAttribute('src'))]; - if (typeof src !== 'undefined') { - el.setAttribute('src', src); + return $mmUtil.restoreSourcesInHtml(response.data, paths, function(anchor, href) { + var ext = $mmFS.getFileExtension(href); + if (ext == 'html' || ext == 'html') { + anchor.setAttribute('mma-mod-resource-html-link', 1); + anchor.setAttribute('data-href', href); } }); - // We do the same for links. - angular.forEach(html.find('a'), function(anchor) { - var href = decodeURIComponent(anchor.getAttribute('href')), - url = paths[href], - ext = $mmFS.getFileExtension(href); - if (typeof url !== 'undefined') { - anchor.setAttribute('href', url); - if (ext == 'html' || ext == 'html') { - anchor.setAttribute('mma-mod-resource-html-link', 1); - anchor.setAttribute('data-href', href); - } - } - }); - - return html.html(); } }); }); diff --git a/www/core/lib/util.js b/www/core/lib/util.js index 13ec86ae03f..27afede5662 100644 --- a/www/core/lib/util.js +++ b/www/core/lib/util.js @@ -943,6 +943,48 @@ angular.module('mm.core') return params; }; + /** + * Given an HTML, searched all links and media and tries to restore original sources using the paths object. + * + * @module mm.core + * @ngdoc method + * @name $mmUtil#restoreSourcesInHtml + * @param {String} html HTML code. + * @param {Object} paths Object linking URLs in the html code with the real URLs to use. + * @param {Function} [anchorFn] Function to call with each anchor. Optional. + * @return {String} Treated HTML code. + */ + self.restoreSourcesInHtml = function(html, paths, anchorFn) { + var div = angular.element('
'), + media; + div.html(html); + + // Treat img, audio, video and source. + media = div[0].querySelectorAll('img, video, audio, source'); + angular.forEach(media, function(el) { + var src = paths[decodeURIComponent(el.getAttribute('src'))]; + if (typeof src !== 'undefined') { + el.setAttribute('src', src); + } + }); + + // We do the same for links. + angular.forEach(div.find('a'), function(anchor) { + var href = decodeURIComponent(anchor.getAttribute('href')), + url = paths[href]; + + if (typeof url !== 'undefined') { + anchor.setAttribute('href', url); + + if (angular.isFunction(anchorFn)) { + anchorFn(anchor, href); + } + } + }); + + return div.html(); + }; + return self; }; });