-
Notifications
You must be signed in to change notification settings - Fork 180
/
title-ng.js
120 lines (97 loc) · 3.22 KB
/
title-ng.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @name Title Ng
*/
import angular from 'angular';
const angularModule = angular.module('Ring.title', []);
angularModule.directive('rgPageTitle', function rgPageTitleDirective() {
return ({
scope: {
rgPageTitle: '@?',
noTitle: '@?',
delimiter: '@'
},
controller: function controller($rootScope, $scope, $element, $attrs, pageTitle, $injector) {
const element = $element[0];
// Get title prefix from title element
const elementText = element.textContent;
let offScopeWatch = angular.noop;
// Set page title on route change
const offRouteWatch = $rootScope.$on('$routeChangeSuccess', (event, current) => {
//Do nothing if we're being redirected
if (current.$$route && current.$$route.redirectTo) { // eslint-disable-line angular/no-private-call
return;
}
let routeTitle = current.$$route && current.$$route.title; // eslint-disable-line angular/no-private-call
pageTitle.setCurrent($scope.rgPageTitle || elementText);
// Use title: false to prevent title change on route
if (routeTitle !== false) {
if (angular.isArray(routeTitle) || angular.isFunction(routeTitle)) {
//Invoke injector
routeTitle = $injector.invoke(routeTitle);
}
pageTitle.addElement(routeTitle || $scope.noTitle);
}
});
$scope.$on('$destroy', () => {
offRouteWatch();
offScopeWatch();
});
this.$onInit = () => {
pageTitle.setDelimiter($scope.delimiter);
if ($attrs.rgPageTitle) {
offScopeWatch = $scope.$watch('rgPageTitle', newBaseTitle => {
pageTitle.setRootElement(newBaseTitle);
});
}
};
}
});
});
angularModule.service('pageTitle', function service($interpolate, $document) {
let delimiter = ' | ';
let current = $document[0].title;
function setTitle(text) {
current = text && $interpolate(text)();
updateDocumentTitle(current);
}
function updateDocumentTitle(text) {
$document[0].title = text;
}
function prepend(element) {
setTitle(current ? element + delimiter + current : element);
}
function replacePart(element, replaceIndex = 0) {
const titleElements = current.split(delimiter);
titleElements[replaceIndex === -1 ? titleElements.length - 1 : replaceIndex] = element;
setTitle(titleElements.join(delimiter));
}
this.setDelimiter = newDelimiter => {
delimiter = newDelimiter || delimiter;
};
this.setCurrent = newBase => {
current = newBase;
};
this.setText = text => updateDocumentTitle(text);
this.addElement = (element, fieldName) => {
if (element.$promise) {
element.$promise.then(Data => {
this.addElement(Data[fieldName || 'name']);
});
} else {
prepend(fieldName ? element[fieldName] : element);
}
};
this.updateElement = (element, fieldName) => {
if (element.$promise) {
element.$promise.then(Data => {
this.updateElement(Data[fieldName || 'name']);
});
} else {
replacePart(fieldName ? element[fieldName] : element);
}
};
this.setRootElement = element => {
replacePart(element, -1);
};
});
export default angularModule.name;