|
| 1 | +// (C) Copyright 2015 Martin Dougiamas |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +angular.module('mm.core.contentlinks') |
| 16 | + |
| 17 | +/** |
| 18 | + * Service to provide some helper functionalities for the contentlinks component. |
| 19 | + * |
| 20 | + * @module mm.core.contentlinks |
| 21 | + * @ngdoc service |
| 22 | + * @name $mmContentLinksHelper |
| 23 | + */ |
| 24 | +.factory('$mmContentLinksHelper', function($log, $ionicHistory, $state, $mmSite, $mmSitesManager, $mmContentLinksDelegate, $q, |
| 25 | + $mmUtil, $translate) { |
| 26 | + |
| 27 | + $log = $log.getInstance('$mmContentLinksHelper'); |
| 28 | + |
| 29 | + var self = {}; |
| 30 | + |
| 31 | + /** |
| 32 | + * Go to the view to choose a site. |
| 33 | + * |
| 34 | + * @module mm.core.contentlinks |
| 35 | + * @ngdoc method |
| 36 | + * @name $mmContentLinksHelper#goToChooseSite |
| 37 | + * @param {String} url URL to treat. |
| 38 | + * @return {Promise} Promise resolved when the state changes. |
| 39 | + */ |
| 40 | + self.goToChooseSite = function(url) { |
| 41 | + $ionicHistory.nextViewOptions({ |
| 42 | + disableBack: true |
| 43 | + }); |
| 44 | + return $state.go('mm_contentlinks.choosesite', {url: url}); |
| 45 | + }; |
| 46 | + |
| 47 | + /** |
| 48 | + * Handle a link. |
| 49 | + * |
| 50 | + * @module mm.core.contentlinks |
| 51 | + * @ngdoc method |
| 52 | + * @name $mmContentLinksHelper#handleLink |
| 53 | + * @param {String} url URL to handle. |
| 54 | + * @return {Promise} Promise resolved with a boolean: true if URL was treated, false otherwise. |
| 55 | + */ |
| 56 | + self.handleLink = function(url) { |
| 57 | + |
| 58 | + // Check if the link should be treated by some component/addon. |
| 59 | + // We perform this check first because it's synchronous. |
| 60 | + var actions = $mmContentLinksDelegate.getActionsFor(url); |
| 61 | + if (actions && actions.length) { |
| 62 | + for (var i = 0; i < actions.length; i++) { |
| 63 | + var action = actions[i]; |
| 64 | + if (action && angular.isFunction(action.action)) { |
| 65 | + |
| 66 | + // We found a valid action. We need to check if the link belongs to any site stored. |
| 67 | + return $mmSitesManager.getSiteIdsFromUrl(url, true).then(function(ids) { |
| 68 | + if (!ids.length) { |
| 69 | + // URL doesn't belong to any site. |
| 70 | + return false; |
| 71 | + } else if (ids.length == 1 && ids[0] == $mmSite.getId()) { |
| 72 | + // Current site. |
| 73 | + action.action(ids[0]); |
| 74 | + } else { |
| 75 | + // Not current site. Ask for confirmation. |
| 76 | + $mmUtil.showConfirm($translate('mm.contentlinks.confirmurlothersite')).then(function() { |
| 77 | + if (ids.length == 1) { |
| 78 | + action.action(ids[0]); |
| 79 | + } else { |
| 80 | + self.goToChooseSite(url); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + return true; |
| 85 | + }).catch(function() { |
| 86 | + return false; |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // No valid actions found. |
| 93 | + return $q.when(false); |
| 94 | + }; |
| 95 | + |
| 96 | + return self; |
| 97 | +}); |
0 commit comments