-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathangular-aviary.js
155 lines (127 loc) · 3.79 KB
/
angular-aviary.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
angular-aviary v0.6.0
(c) 2016 Massimiliano Sartoretto <[email protected]>
License: MIT
*/
'format amd';
/* global define */
(function () {
'use strict';
function ngAviary(angular, Aviary) {
ngAviaryDirective.$inject = ['ngAviary'];
function ngAviaryDirective(ngAviary) {
return {
restrict: 'A',
scope: {
targetSelector: '@',
targetSrc: '@',
onSave: '&',
onSaveButtonClicked: '&',
onClose: '&'
},
link: function (scope, element, attrs) {
var targetImage = window.document.querySelector(scope.targetSelector);
element.bind('click', function(e) {
e.preventDefault();
return launchEditor();
});
// Callbacks obj
var cbs = {
onSaveButtonClicked: onSaveButtonClickedCb,
onSave: onSaveCb,
onError: onErrorCb,
onClose: onCloseCb
};
var featherEditor = new Aviary.Feather(
angular.extend({}, ngAviary.configuration, cbs)
);
function launchEditor() {
featherEditor.launch({
image: targetImage,
url: scope.targetSrc || targetImage.src
});
return false;
}
function onSaveButtonClickedCb(imageID) {
// User onSaveButtonClicked callback
(scope.onSaveButtonClicked || angular.noop)({id: imageID});
}
function onSaveCb(imageID, newURL) {
// User onSave callback
(scope.onSave || angular.noop)({
id: imageID,
newURL: newURL
});
if(scope.closeOnSave || ngAviary.configuration.closeOnSave){
featherEditor.close();
}
}
function onErrorCb(errorObj) {
// User errback
(scope.onError || angular.noop)({
error: errorObj
});
}
function onCloseCb(isDirty) {
// User onClose callback
(scope.onClose || angular.noop)({
isDirty: isDirty
});
}
}
};
}
function ngAviaryProvider(){
/* jshint validthis:true */
var defaults = {
apiKey: null
};
var requiredKeys = [
'apiKey'
];
var config;
this.configure = function(params) {
// Can only be configured once
if (config) {
throw new Error('Already configured.');
}
// Check if it is an `object`
if (!(params instanceof Object)) {
throw new TypeError('Invalid argument: `config` must be an `Object`.');
}
// Extend default configuration
config = angular.extend({}, defaults, params);
// Check if all required keys are set
angular.forEach(requiredKeys, function(key) {
if (!config[key]) {
throw new Error('Missing parameter:', key);
}
});
return config;
};
this.$get = function() {
if(!config) {
throw new Error('ngAviary must be configured first.');
}
var getConfig = (function() {
return config;
})();
return {
configuration: getConfig
};
};
}
return angular
.module('ngAviary', [])
.directive('ngAviary', ngAviaryDirective)
.provider('ngAviary', ngAviaryProvider);
}
if (typeof define === 'function' && define.amd) {
define(['angular', 'Aviary'], ngAviary);
} else if (typeof module !== 'undefined' && module && module.exports) {
ngAviary(angular, require('Aviary'));
module.exports = 'ngAviary';
} else {
ngAviary(angular, (typeof global !== 'undefined' ? global : window).Aviary);
}
})();