Skip to content

Commit 8b561b3

Browse files
committed
MOBILE-1160 media: Move some common code to mmUtil
1 parent 27d580c commit 8b561b3

File tree

4 files changed

+51
-69
lines changed

4 files changed

+51
-69
lines changed

www/addons/mod_book/services/book.js

Lines changed: 2 additions & 22 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 = {};
@@ -283,27 +283,7 @@ angular.module('mm.addons.mod_book')
283283
} else {
284284
// Now that we have the content, we update the SRC to point back to
285285
// the external resource. That will be caught by mm-format-text.
286-
var html = angular.element('<div>'),
287-
media;
288-
html.html(response.data);
289-
290-
// Treat img, audio, video and source.
291-
media = html[0].querySelectorAll('img, video, audio, source');
292-
angular.forEach(media, function(el) {
293-
var src = paths[decodeURIComponent(el.getAttribute('src'))];
294-
if (typeof src !== 'undefined') {
295-
el.setAttribute('src', src);
296-
}
297-
});
298-
// We do the same for links.
299-
angular.forEach(html.find('a'), function(anchor) {
300-
var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
301-
if (typeof href !== 'undefined') {
302-
anchor.setAttribute('href', href);
303-
}
304-
});
305-
306-
return html.html();
286+
return $mmUtil.restoreSourcesInHtml(response.data, paths);
307287
}
308288
});
309289
});

www/addons/mod_page/services/page.js

Lines changed: 2 additions & 22 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 = {};
@@ -176,27 +176,7 @@ angular.module('mm.addons.mod_page')
176176
} else {
177177
// Now that we have the content, we update the SRC to point back to
178178
// the external resource. That will b caught by mm-format-text.
179-
var html = angular.element('<div>'),
180-
media;
181-
html.html(response.data);
182-
183-
// Treat img, audio, video and source.
184-
media = html[0].querySelectorAll('img, video, audio, source');
185-
angular.forEach(media, function(el) {
186-
var src = paths[decodeURIComponent(el.getAttribute('src'))];
187-
if (typeof src !== 'undefined') {
188-
el.setAttribute('src', src);
189-
}
190-
});
191-
// We do the same for links.
192-
angular.forEach(html.find('a'), function(anchor) {
193-
var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
194-
if (typeof href !== 'undefined') {
195-
anchor.setAttribute('href', href);
196-
}
197-
});
198-
199-
return html.html();
179+
return $mmUtil.restoreSourcesInHtml(response.data, paths);
200180
}
201181
});
202182
});

www/addons/mod_resource/services/resource.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,33 +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-
media;
232-
html.html(response.data);
233-
234-
// Treat img, audio, video and source.
235-
media = html[0].querySelectorAll('img, video, audio, source');
236-
angular.forEach(media, function(el) {
237-
var src = paths[decodeURIComponent(el.getAttribute('src'))];
238-
if (typeof src !== 'undefined') {
239-
el.setAttribute('src', src);
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);
240235
}
241236
});
242-
// We do the same for links.
243-
angular.forEach(html.find('a'), function(anchor) {
244-
var href = decodeURIComponent(anchor.getAttribute('href')),
245-
url = paths[href],
246-
ext = $mmFS.getFileExtension(href);
247-
if (typeof url !== 'undefined') {
248-
anchor.setAttribute('href', url);
249-
if (ext == 'html' || ext == 'html') {
250-
anchor.setAttribute('mma-mod-resource-html-link', 1);
251-
anchor.setAttribute('data-href', href);
252-
}
253-
}
254-
});
255-
256-
return html.html();
257237
}
258238
});
259239
});

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)