-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-resource-picker.js
59 lines (54 loc) · 2.77 KB
/
angular-resource-picker.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
(function () {
'use strict';
angular
.module('ui.resourcePicker', [])
.directive('btnResourcePicker', [
function () {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
parameters : '='
},
template: '<a href="" role="button" data-ng-click="resourcePickerOpen(pickerName)" data-ng-transclude=""></a>',
link: function ($scope, el, attrs) {
$scope.pickerName = 'picker-' + Math.floor(Math.random() * 10000);
// Watch for parameters change
$scope.$watch('parameters', function (newValue) {
if (Claroline.ResourceManager.hasPicker($scope.pickerName)) {
var picker = Claroline.ResourceManager.get($scope.pickerName);
if (angular.isObject(picker)) {
// Update picker parameters
for (var parameter in newValue) {
if (newValue.hasOwnProperty(parameter)) {
picker.parameters[parameter] = newValue[parameter];
}
}
}
}
});
$scope.resourcePickerOpen = function (pickerName) {
// Initialize resource picker object
if (!Claroline.ResourceManager.hasPicker($scope.pickerName)) {
Claroline.ResourceManager.createPicker($scope.pickerName, $scope.parameters, true);
} else {
console.log('picker use directive');
// Open existing picker
Claroline.ResourceManager.picker(pickerName, 'open');
}
};
$scope.resourcePickerClose = function (pickerName) {
Claroline.ResourceManager.picker(pickerName, 'close');
};
// Destroy instance of picker when directive is destroyed
$scope.$on('$destroy', function handleDestroyEvent() {
if (Claroline.ResourceManager.hasPicker($scope.pickerName)) {
Claroline.ResourceManager.destroy($scope.pickerName);
}
});
}
};
}
]);
})();