This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (67 loc) · 2.43 KB
/
app.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
'use strict';
var hippoRestApp = angular.module('hippoRestApp', [ 'ngRoute', 'ngResource', 'ngSanitize', 'ui.bootstrap' ]);
hippoRestApp.constant('apiPrefix', 'http://localhost:8080/site/api/');
hippoRestApp.config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'document-list.html',
controller : 'DocumentsController'
}).when('/:uuid', {
templateUrl : 'detail.html',
controller : 'DocumentsController'
}).otherwise('/');
});
hippoRestApp.factory('DocumentsService', function($resource, apiPrefix) {
return {
getList : function(offset, max, query) {
return $resource(apiPrefix + 'documents/', {
_offset : offset,
_max : max,
_query : query
}).get();
},
getDocumentById : function(uuid) {
return $resource(apiPrefix + 'documents/' + uuid).get();
}
}
});
hippoRestApp.controller('DocumentsController', function($scope, $routeParams, DocumentsService, apiPrefix) {
if (!$routeParams.uuid) {
$scope.currentPage = 1;
$scope.itemsPerPage = 6;
$scope.query = '';
$scope.update = function($scope) {
$scope.offset = ($scope.currentPage - 1) * $scope.itemsPerPage;
DocumentsService.getList($scope.offset, $scope.itemsPerPage, $scope.query).$promise.then(function(response) {
$scope.documents = response;
$scope.totalItems = $scope.documents['total'];
});
}
$scope.update($scope);
$scope.pageChanged = function() {
$scope.update($scope);
};
$scope.search = function() {
$scope.update($scope);
}
} else {
DocumentsService.getDocumentById($routeParams.uuid).$promise.then(function(response) {
$scope.document = response;
// resolve internal links
$scope.content = $scope.resolveLinks(response);
// TODO
// resolve images
});
}
$scope.resolveLinks = function(response) {
var someElement = document.createElement('div');
someElement.innerHTML = response.items['myhippoproject:content'].content;
var links = someElement.querySelectorAll('a[data-hippo-link]');
for (var index = 0; index < links.length; index++) {
if (response.items['myhippoproject:content'].links[links[index].getAttribute('data-hippo-link')]) {
var uuid = response.items['myhippoproject:content'].links[links[index].getAttribute('data-hippo-link')].id;
links[index].href = '#/' + uuid;
}
}
return someElement.innerHTML;
};
});