Skip to content

Commit 6afdc3d

Browse files
committed
Merge pull request #391 from dpalou/MOBILE-1160
Mobile 1160
2 parents 969c927 + 8b561b3 commit 6afdc3d

File tree

4 files changed

+54
-64
lines changed

4 files changed

+54
-64
lines changed

www/addons/mod_book/services/book.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_book')
2121
* @ngdoc service
2222
* @name $mmaModBook
2323
*/
24-
.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModBookComponent) {
24+
.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModBookComponent) {
2525
$log = $log.getInstance('$mmaModBook');
2626

2727
var self = {};
@@ -262,7 +262,6 @@ angular.module('mm.addons.mod_book')
262262

263263
// Promise handling when we are in a browser.
264264
promise = (function() {
265-
var deferred;
266265
if (!indexUrl) {
267266
// If ever that happens.
268267
$log.debug('Could not locate the index chapter');
@@ -272,9 +271,7 @@ angular.module('mm.addons.mod_book')
272271
return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModBookComponent, moduleId);
273272
} else {
274273
// We return the live URL.
275-
deferred = $q.defer();
276-
deferred.resolve($mmSite.fixPluginfileURL(indexUrl));
277-
return deferred.promise;
274+
return $q.when($mmSite.fixPluginfileURL(indexUrl));
278275
}
279276
})();
280277

@@ -286,22 +283,7 @@ angular.module('mm.addons.mod_book')
286283
} else {
287284
// Now that we have the content, we update the SRC to point back to
288285
// the external resource. That will be caught by mm-format-text.
289-
var html = angular.element('<div>');
290-
html.html(response.data);
291-
angular.forEach(html.find('img'), function(img) {
292-
var src = paths[decodeURIComponent(img.getAttribute('src'))];
293-
if (typeof src !== 'undefined') {
294-
img.setAttribute('src', src);
295-
}
296-
});
297-
// We do the same for links.
298-
angular.forEach(html.find('a'), function(anchor) {
299-
var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
300-
if (typeof href !== 'undefined') {
301-
anchor.setAttribute('href', href);
302-
}
303-
});
304-
return html.html();
286+
return $mmUtil.restoreSourcesInHtml(response.data, paths);
305287
}
306288
});
307289
});

www/addons/mod_page/services/page.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_page')
2121
* @ngdoc service
2222
* @name $mmaModPage
2323
*/
24-
.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModPageComponent) {
24+
.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModPageComponent) {
2525
$log = $log.getInstance('$mmaModPage');
2626

2727
var self = {};
@@ -164,9 +164,7 @@ angular.module('mm.addons.mod_page')
164164
return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModPageComponent, moduleId);
165165
} else {
166166
// We return the live URL.
167-
deferred = $q.defer();
168-
deferred.resolve($mmSite.fixPluginfileURL(indexUrl));
169-
return deferred.promise;
167+
return $q.when($mmSite.fixPluginfileURL(indexUrl));
170168
}
171169
})();
172170

@@ -177,23 +175,8 @@ angular.module('mm.addons.mod_page')
177175
return $q.reject();
178176
} else {
179177
// Now that we have the content, we update the SRC to point back to
180-
// the external resource. That will be caught by mm-format-text.
181-
var html = angular.element('<div>');
182-
html.html(response.data);
183-
angular.forEach(html.find('img'), function(img) {
184-
var src = paths[decodeURIComponent(img.getAttribute('src'))];
185-
if (typeof src !== 'undefined') {
186-
img.setAttribute('src', src);
187-
}
188-
});
189-
// We do the same for links.
190-
angular.forEach(html.find('a'), function(anchor) {
191-
var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
192-
if (typeof href !== 'undefined') {
193-
anchor.setAttribute('href', href);
194-
}
195-
});
196-
return html.html();
178+
// the external resource. That will b caught by mm-format-text.
179+
return $mmUtil.restoreSourcesInHtml(response.data, paths);
197180
}
198181
});
199182
});

www/addons/mod_resource/services/resource.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -227,30 +227,13 @@ angular.module('mm.addons.mod_resource')
227227
} else {
228228
// Now that we have the content, we update the SRC to point back to
229229
// the external resource. That will be caught by mm-format-text.
230-
var html = angular.element('<div>');
231-
html.append(response.data);
232-
233-
angular.forEach(html.find('img'), function(img) {
234-
var src = paths[decodeURIComponent(img.getAttribute('src'))];
235-
if (typeof src !== 'undefined') {
236-
img.setAttribute('src', src);
237-
}
238-
});
239-
// We do the same for links.
240-
angular.forEach(html.find('a'), function(anchor) {
241-
var href = decodeURIComponent(anchor.getAttribute('href')),
242-
url = paths[href],
243-
ext = $mmFS.getFileExtension(href);
244-
if (typeof url !== 'undefined') {
245-
anchor.setAttribute('href', url);
246-
if (ext == 'html' || ext == 'html') {
247-
anchor.setAttribute('mma-mod-resource-html-link', 1);
248-
anchor.setAttribute('data-href', href);
249-
}
230+
return $mmUtil.restoreSourcesInHtml(response.data, paths, function(anchor, href) {
231+
var ext = $mmFS.getFileExtension(href);
232+
if (ext == 'html' || ext == 'html') {
233+
anchor.setAttribute('mma-mod-resource-html-link', 1);
234+
anchor.setAttribute('data-href', href);
250235
}
251236
});
252-
253-
return html.html();
254237
}
255238
});
256239
});

www/core/lib/util.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,48 @@ angular.module('mm.core')
943943
return params;
944944
};
945945

946+
/**
947+
* Given an HTML, searched all links and media and tries to restore original sources using the paths object.
948+
*
949+
* @module mm.core
950+
* @ngdoc method
951+
* @name $mmUtil#restoreSourcesInHtml
952+
* @param {String} html HTML code.
953+
* @param {Object} paths Object linking URLs in the html code with the real URLs to use.
954+
* @param {Function} [anchorFn] Function to call with each anchor. Optional.
955+
* @return {String} Treated HTML code.
956+
*/
957+
self.restoreSourcesInHtml = function(html, paths, anchorFn) {
958+
var div = angular.element('<div>'),
959+
media;
960+
div.html(html);
961+
962+
// Treat img, audio, video and source.
963+
media = div[0].querySelectorAll('img, video, audio, source');
964+
angular.forEach(media, function(el) {
965+
var src = paths[decodeURIComponent(el.getAttribute('src'))];
966+
if (typeof src !== 'undefined') {
967+
el.setAttribute('src', src);
968+
}
969+
});
970+
971+
// We do the same for links.
972+
angular.forEach(div.find('a'), function(anchor) {
973+
var href = decodeURIComponent(anchor.getAttribute('href')),
974+
url = paths[href];
975+
976+
if (typeof url !== 'undefined') {
977+
anchor.setAttribute('href', url);
978+
979+
if (angular.isFunction(anchorFn)) {
980+
anchorFn(anchor, href);
981+
}
982+
}
983+
});
984+
985+
return div.html();
986+
};
987+
946988
return self;
947989
};
948990
});

0 commit comments

Comments
 (0)