Skip to content

Commit 204a396

Browse files
committed
MOBILE-1554 content: Download embedded big files when opened or played
1 parent 08a2805 commit 204a396

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

www/core/directives/external_content.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ angular.module('mm.core')
3131
* Attributes accepted:
3232
* - siteid: Reference to the site ID if different than the site the user is connected to.
3333
*/
34-
.directive('mmExternalContent', function($log, $mmFilepool, $mmSite, $mmSitesManager, $mmUtil, $q) {
34+
.directive('mmExternalContent', function($log, $mmFilepool, $mmSite, $mmSitesManager, $mmUtil, $q, $mmApp) {
3535
$log = $log.getInstance('mmExternalContent');
3636

3737
/**
@@ -99,6 +99,27 @@ angular.module('mm.core')
9999
} else {
100100
dom.setAttribute(targetAttr, finalUrl);
101101
}
102+
103+
// Set events to download big files (not downloaded automatically).
104+
if (finalUrl.indexOf('http') === 0 &&
105+
(dom.tagName == 'VIDEO' || dom.tagName == 'AUDIO' || dom.tagName == 'A' || dom.tagName == 'SOURCE')) {
106+
var eventName = dom.tagName == 'A' ? 'click' : 'play';
107+
108+
if (dom.tagName == 'SOURCE') {
109+
dom = $mmUtil.closest(dom, 'video,audio');
110+
if (!dom) {
111+
return;
112+
}
113+
}
114+
115+
angular.element(dom).on(eventName, function() {
116+
// User played media or opened a downloadable link.
117+
// Download the file if in wifi and it hasn't been downloaded already (for big files).
118+
if (!$mmApp.isNetworkAccessLimited()) {
119+
fn(siteId, url, component, componentId, undefined, false);
120+
}
121+
});
122+
}
102123
});
103124
});
104125
}

www/core/lib/util.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ angular.module('mm.core')
7070

7171
$log = $log.getInstance('$mmUtil');
7272

73-
var self = {}; // Use 'self' to be coherent with the rest of services.
73+
var self = {}, // Use 'self' to be coherent with the rest of services.
74+
matchesFn;
7475

7576
/**
7677
* Formats a URL, trim, lowercase, etc...
@@ -1163,6 +1164,48 @@ angular.module('mm.core')
11631164
return urls;
11641165
};
11651166

1167+
/**
1168+
* Equivalent to element.closest(). If the browser doesn't support element.closest, it will
1169+
* traverse the parents to achieve the same functionality.
1170+
* Returns the closest ancestor of the current element (or the current element itself) which matches the selector.
1171+
*
1172+
* @module mm.core
1173+
* @ngdoc method
1174+
* @name $mmUtil#closest
1175+
* @param {Object} element DOM Element.
1176+
* @param {String} selector Selector to search.
1177+
* @return {Object} Closest ancestor.
1178+
*/
1179+
self.closest = function(element, selector) {
1180+
// Try to use closest if the browser supports it.
1181+
if (typeof element.closest == 'function') {
1182+
return element.closest(selector);
1183+
}
1184+
1185+
if (!matchesFn) {
1186+
// Find the matches function supported by the browser.
1187+
['matches','webkitMatchesSelector','mozMatchesSelector','msMatchesSelector','oMatchesSelector'].some(function(fn) {
1188+
if (typeof document.body[fn] == 'function') {
1189+
matchesFn = fn;
1190+
return true;
1191+
}
1192+
return false;
1193+
});
1194+
1195+
if (!matchesFn) {
1196+
return;
1197+
}
1198+
}
1199+
1200+
// Traverse parents.
1201+
while (element) {
1202+
if (element[matchesFn](selector)) {
1203+
return element;
1204+
}
1205+
element = element.parentElement;
1206+
}
1207+
};
1208+
11661209
return self;
11671210
};
11681211
});

0 commit comments

Comments
 (0)