fng-jq-upload 0.12.0-beta.2 → 0.12.0-beta.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Gruntfile.js +14 -5
- package/app/fng-jq-upload.js +250 -223
- package/dist/fng-jq-upload.js +244 -223
- package/dist/fng-jq-upload.min.js +1 -1
- package/lib/fng-jq-upload.js +110 -40
- package/lib/fng-jq-upload.ts +76 -38
- package/lib/index.d.ts +1 -0
- package/package.json +39 -32
- package/templates/fileform.html +9 -9
- package/test/api/apiSpec.js +11 -11
- package/test/karma.conf.js +9 -2
package/Gruntfile.js
CHANGED
|
@@ -52,25 +52,34 @@ module.exports = function (grunt) {
|
|
|
52
52
|
|
|
53
53
|
mochaTest: {
|
|
54
54
|
options: {
|
|
55
|
-
reporter: 'dot'
|
|
55
|
+
reporter: 'dot',
|
|
56
|
+
'forbid-only': true,
|
|
56
57
|
},
|
|
57
58
|
all : {
|
|
58
59
|
src: [
|
|
59
60
|
'test/api/**/*.js'
|
|
60
61
|
]
|
|
61
62
|
}
|
|
62
|
-
}
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
karma: {
|
|
66
|
+
unit: {
|
|
67
|
+
configFile: 'test/karma.conf.js',
|
|
68
|
+
singleRun: true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
63
72
|
});
|
|
64
73
|
|
|
65
74
|
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
66
|
-
grunt.loadNpmTasks('grunt-bump-build-git');
|
|
67
75
|
grunt.loadNpmTasks('grunt-contrib-concat');
|
|
68
76
|
grunt.loadNpmTasks('grunt-contrib-uglify');
|
|
69
77
|
grunt.loadNpmTasks('grunt-angular-templates');
|
|
70
78
|
grunt.loadNpmTasks('grunt-mocha-test');
|
|
79
|
+
grunt.loadNpmTasks('grunt-karma');
|
|
71
80
|
|
|
72
|
-
grunt.registerTask('test', ['jshint', 'mochaTest']);
|
|
81
|
+
grunt.registerTask('test', ['jshint', 'mochaTest', 'karma']);
|
|
73
82
|
grunt.registerTask('build', ['ngtemplates', 'concat', 'uglify']);
|
|
74
83
|
grunt.registerTask('default', ['test']);
|
|
75
84
|
|
|
76
|
-
};
|
|
85
|
+
};
|
package/app/fng-jq-upload.js
CHANGED
|
@@ -1,235 +1,262 @@
|
|
|
1
|
+
// TODO Check the filesize https://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation
|
|
2
|
+
// and suggest they resize if using an appropriate tool https://www.google.com/search?q=compress+photo+online+to+200kb
|
|
3
|
+
// for example https://www.img2go.com/compress-image or https://www.imgonline.com.ua/eng/compress-image-size.php
|
|
4
|
+
// or https://sqooush.app
|
|
5
|
+
|
|
1
6
|
(function () {
|
|
2
|
-
|
|
7
|
+
'use strict';
|
|
3
8
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
var app = angular.module('uploadModule', [
|
|
10
|
+
'blueimp.fileupload'
|
|
11
|
+
]);
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
var getIdFromUrl = function (url) {
|
|
14
|
+
var breakdown = /\/file\/(.+?)\/([0-9a-f]{24})/.exec(url);
|
|
15
|
+
return breakdown[2];
|
|
16
|
+
};
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
app
|
|
19
|
+
.controller('FngUploadAdditFieldsCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
|
|
20
|
+
$scope.record = {};
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function doSetUp() {
|
|
34
|
-
if (!$scope.formScope.newRecord) {
|
|
35
|
-
var watchDeregister = $scope.formScope.$watch('phase', function (newVal) {
|
|
36
|
-
if (newVal === 'ready') {
|
|
37
|
-
setUpAdditFields();
|
|
38
|
-
$scope.$on('fngCancel', function () {
|
|
39
|
-
setUpAdditFields();
|
|
40
|
-
});
|
|
41
|
-
watchDeregister();
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
} else {
|
|
45
|
-
$scope.record = {};
|
|
46
|
-
// setUpWatch();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
$timeout(doSetUp);
|
|
51
|
-
|
|
52
|
-
}])
|
|
53
|
-
.directive('fngUploadAdditFields', function() {
|
|
54
|
-
return {
|
|
55
|
-
link: function (scope, element, attrs) {
|
|
56
|
-
scope.uploadForm = scope.$parent.$parent.$parent;
|
|
57
|
-
scope.formScope = scope.uploadForm.formScope;
|
|
58
|
-
scope.schema = scope.uploadForm.directiveOptions.additFields;
|
|
59
|
-
scope.file = parseInt(attrs.file);
|
|
60
|
-
},
|
|
61
|
-
scope: {},
|
|
62
|
-
controller: 'FngUploadAdditFieldsCtrl',
|
|
63
|
-
template: '<form-input formstyle="inline" schema="schema" model="record" forceform="true"></form-input>'
|
|
64
|
-
};
|
|
65
|
-
})
|
|
66
|
-
.controller('fngJqUploadCtrl', ['$scope', function ($scope) {
|
|
67
|
-
$scope.loadingFiles = false;
|
|
68
|
-
$scope.formScope = $scope.$parent;
|
|
69
|
-
|
|
70
|
-
$scope.dataField = function (initialise) {
|
|
71
|
-
var retVal;
|
|
72
|
-
if ($scope.info.name.indexOf('.') === -1) {
|
|
73
|
-
retVal = $scope.formScope.record[$scope.info.name];
|
|
74
|
-
if (!retVal && initialise) {
|
|
75
|
-
retVal = $scope.formScope.record[$scope.info.name] = [];
|
|
76
|
-
}
|
|
77
|
-
} else if ($scope.options.subschema) {
|
|
78
|
-
var modelBase = $scope.formScope.record;
|
|
79
|
-
var compoundName = $scope.info.name;
|
|
80
|
-
var root = $scope.options.subschemaroot;
|
|
81
|
-
var lastPart = compoundName.slice(root.length + 1);
|
|
82
|
-
|
|
83
|
-
if ($scope.options.index) {
|
|
84
|
-
retVal = modelBase[root][$scope.options.index][lastPart];
|
|
85
|
-
} else {
|
|
86
|
-
retVal = modelBase;
|
|
87
|
-
var rootParts = root.split('.');
|
|
88
|
-
for (var i = 0, l = rootParts.length; i < l; i++) {
|
|
89
|
-
retVal = retVal[rootParts[i]];
|
|
22
|
+
$scope.$watch('record', function (newVal, oldVal) {
|
|
23
|
+
if (newVal !== oldVal) {
|
|
24
|
+
$scope.uploadForm.formScope[$scope.uploadForm.formScope.topLevelFormName].$setDirty();
|
|
25
|
+
angular.extend($scope.uploadForm.dataField()[$scope.file], newVal);
|
|
26
|
+
}
|
|
27
|
+
}, true);
|
|
28
|
+
|
|
29
|
+
function setUpAdditFields() {
|
|
30
|
+
if ($scope.schema) {
|
|
31
|
+
$scope.schema.forEach(function (field) {
|
|
32
|
+
var objVal = $scope.uploadForm.dataField()[$scope.file];
|
|
33
|
+
$scope.record[field.name] = objVal ? objVal[field.name] : undefined;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
90
36
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
37
|
+
|
|
38
|
+
function doSetUp() {
|
|
39
|
+
if (!$scope.formScope.newRecord) {
|
|
40
|
+
var watchDeregister = $scope.formScope.$watch('phase', function (newVal) {
|
|
41
|
+
if (newVal === 'ready') {
|
|
42
|
+
setUpAdditFields();
|
|
43
|
+
$scope.$on('fngCancel', function () {
|
|
44
|
+
setUpAdditFields();
|
|
45
|
+
});
|
|
46
|
+
watchDeregister();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
$scope.record = {};
|
|
51
|
+
// setUpWatch();
|
|
52
|
+
}
|
|
103
53
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
54
|
+
|
|
55
|
+
$timeout(doSetUp);
|
|
56
|
+
|
|
57
|
+
}])
|
|
58
|
+
.directive('fngUploadAdditFields', function () {
|
|
59
|
+
return {
|
|
60
|
+
link: function (scope, element, attrs) {
|
|
61
|
+
scope.uploadForm = scope.$parent.$parent.$parent;
|
|
62
|
+
scope.formScope = scope.uploadForm.formScope;
|
|
63
|
+
scope.schema = scope.uploadForm.directiveOptions.additFields;
|
|
64
|
+
scope.file = parseInt(attrs.file);
|
|
65
|
+
},
|
|
66
|
+
scope: {},
|
|
67
|
+
controller: 'FngUploadAdditFieldsCtrl',
|
|
68
|
+
template: '<form-input formstyle="inline" schema="schema" model="record" forceform="true"></form-input>'
|
|
69
|
+
};
|
|
70
|
+
})
|
|
71
|
+
.controller('fngJqUploadCtrl', ['$scope', function ($scope) {
|
|
72
|
+
$scope.loadingFiles = false;
|
|
73
|
+
$scope.formScope = $scope.$parent;
|
|
74
|
+
|
|
75
|
+
$scope.dataField = function (initialise) {
|
|
76
|
+
var retVal;
|
|
77
|
+
if ($scope.info.name.indexOf('.') === -1) {
|
|
78
|
+
retVal = $scope.formScope.record[$scope.info.name];
|
|
79
|
+
if (!retVal && initialise) {
|
|
80
|
+
retVal = $scope.formScope.record[$scope.info.name] = [];
|
|
81
|
+
}
|
|
82
|
+
} else if ($scope.options.subschema) {
|
|
83
|
+
var modelBase = $scope.formScope.record;
|
|
84
|
+
var compoundName = $scope.info.name;
|
|
85
|
+
var root = $scope.options.subschemaroot;
|
|
86
|
+
var lastPart = compoundName.slice(root.length + 1);
|
|
87
|
+
|
|
88
|
+
retVal = modelBase;
|
|
89
|
+
var rootParts = root.split('.');
|
|
90
|
+
for (var i = 0, l = rootParts.length; i < l; i++) {
|
|
91
|
+
retVal = retVal[rootParts[i]];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var index = $scope.options.index;
|
|
95
|
+
if (index === undefined) {
|
|
96
|
+
index = $scope.$parent.$index; // Works for use case where file obj sits in an array
|
|
97
|
+
}
|
|
98
|
+
if (index !== undefined) {
|
|
99
|
+
if (!retVal[index]) {
|
|
100
|
+
// when setUpAttachments() is called from $scope.$on('fngCancel'...), index can refer to an array
|
|
101
|
+
// element that was added, and then cancelled before ever being saved, and therefore no longer exists.
|
|
102
|
+
// in this case, there's nothing we can or should be doing, so should just...
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!retVal[index][lastPart]) {
|
|
106
|
+
retVal[index][lastPart] = [];
|
|
107
|
+
}
|
|
108
|
+
retVal = retVal[index][lastPart];
|
|
109
|
+
} else {
|
|
110
|
+
if ($scope.options.subkey) {
|
|
111
|
+
var arrayIndex = $scope.formScope['$_arrayOffset_' + root.replace(/\./g, '_') + '_' + $scope.options.subkeyno];
|
|
112
|
+
if (arrayIndex != null && arrayIndex !== undefined && arrayIndex !== -1) {
|
|
113
|
+
retVal = retVal[arrayIndex][lastPart];
|
|
114
|
+
} else {
|
|
115
|
+
retVal = undefined;
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
console.log('No support for this yet');
|
|
119
|
+
//modelString += '[$index].' + lastPart;
|
|
120
|
+
//idString = null;
|
|
121
|
+
//nameString = compoundName.replace(/\./g, '-');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
console.log('No support for this yet either');
|
|
126
|
+
}
|
|
127
|
+
return retVal;
|
|
123
128
|
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
|
|
130
|
+
function setUpAttachments() {
|
|
131
|
+
var storedData = $scope.dataField();
|
|
132
|
+
if (storedData) {
|
|
133
|
+
for (var i = 0; i < storedData.length; i++) {
|
|
134
|
+
var storedElement = storedData[i];
|
|
135
|
+
var storedName = storedElement.filename;
|
|
136
|
+
var queueElement = {
|
|
137
|
+
'name': storedName,
|
|
138
|
+
'size': storedElement.size,
|
|
139
|
+
'url': '/api/file/' + $scope.formScope.modelName + '/' + storedElement._id,
|
|
140
|
+
'deleteUrl': '/api/file/' + $scope.formScope.modelName + '/' + storedElement._id,
|
|
141
|
+
'deleteType': 'DELETE'
|
|
142
|
+
};
|
|
143
|
+
if (storedName) {
|
|
144
|
+
switch (storedName.slice(storedName.length - 4, storedName.length).toLowerCase()) { // extension
|
|
145
|
+
case '.gif':
|
|
146
|
+
case '.png':
|
|
147
|
+
case '.jpg':
|
|
148
|
+
case 'jpeg':
|
|
149
|
+
queueElement.thumbnailUrl = '/api/file/' + $scope.formScope.modelName + '/thumbnail/' + storedElement._id;
|
|
150
|
+
break;
|
|
151
|
+
default:
|
|
152
|
+
queueElement.thumbnailUrl = 'https://upload.wikimedia.org/wikipedia/commons/7/77/Icon_New_File_256x256.png';
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
queueElement.thumbnailUrl = 'https://upload.wikimedia.org/wikipedia/commons/7/77/Icon_New_File_256x256.png';
|
|
156
|
+
}
|
|
157
|
+
$scope.$$childHead.queue.push(queueElement);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
132
160
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
161
|
+
|
|
162
|
+
if (!$scope.formScope.newRecord) {
|
|
163
|
+
var watchDeregister = $scope.formScope.$watch('phase', function (newVal) {
|
|
164
|
+
if (newVal === 'ready') {
|
|
165
|
+
$scope.$$childHead.queue = $scope.$$childHead.queue || [];
|
|
166
|
+
setUpAttachments();
|
|
167
|
+
$scope.$on('fngCancel', function () {
|
|
168
|
+
$scope.$$childHead.queue = [];
|
|
169
|
+
setUpAttachments();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
watchDeregister();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
$scope.$on('fileuploaddone', function (event, data) {
|
|
178
|
+
var field = $scope.dataField(true);
|
|
179
|
+
var fileDetails = data.result.files[0];
|
|
180
|
+
field.push(
|
|
181
|
+
{
|
|
182
|
+
_id: getIdFromUrl(fileDetails.url),
|
|
183
|
+
filename: fileDetails.name,
|
|
184
|
+
size: fileDetails.size
|
|
185
|
+
});
|
|
186
|
+
$scope.ngModel.$setDirty();
|
|
146
187
|
});
|
|
147
188
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
function () {
|
|
219
|
-
state = 'resolved';
|
|
220
|
-
removeFromRecord(file);
|
|
221
|
-
},
|
|
222
|
-
function () {
|
|
223
|
-
state = 'rejected';
|
|
224
|
-
removeFromRecord(file);
|
|
189
|
+
}])
|
|
190
|
+
.directive('fngJqUploadForm', ['pluginHelper', '$rootScope', function (pluginHelper, $rootScope) {
|
|
191
|
+
return {
|
|
192
|
+
link: function (scope, element, attrs, ngModel) {
|
|
193
|
+
angular.extend(scope, pluginHelper.extractFromAttr(attrs, 'fngJqUploadForm'));
|
|
194
|
+
// Pick up options from the mongoose schema
|
|
195
|
+
scope.passedParams = scope.formScope[attrs.schema];
|
|
196
|
+
angular.extend(scope.options, scope.passedParams.fngJqUploadForm);
|
|
197
|
+
if (scope.options.additFields) {
|
|
198
|
+
scope.directiveOptions.additFields = JSON.parse(scope.options.additFields);
|
|
199
|
+
}
|
|
200
|
+
scope.directiveOptions.url = '/api/file/upload/' + scope.formScope.modelName;
|
|
201
|
+
scope.directiveOptions.sizeLimit = scope.directiveOptions.sizelimit;
|
|
202
|
+
scope.directiveOptions.autoUpload = scope.directiveOptions.autoupload;
|
|
203
|
+
scope.isDisabled = typeof $rootScope.isSecurelyDisabled === 'function' ? $rootScope.isSecurelyDisabled(scope.info.id) : false;
|
|
204
|
+
scope.name = scope.passedParams.name;
|
|
205
|
+
scope.ngModel = ngModel;
|
|
206
|
+
},
|
|
207
|
+
restrict: 'E',
|
|
208
|
+
require: '?ngModel',
|
|
209
|
+
templateUrl: 'templates/fileform.html',
|
|
210
|
+
scope: {},
|
|
211
|
+
controller: 'fngJqUploadCtrl'
|
|
212
|
+
};
|
|
213
|
+
}])
|
|
214
|
+
.controller('FileDestroyController', ['$scope', '$http', function ($scope, $http) {
|
|
215
|
+
var file = $scope.file,
|
|
216
|
+
state;
|
|
217
|
+
|
|
218
|
+
$scope.uploadScope = $scope.$parent.$parent.$parent;
|
|
219
|
+
|
|
220
|
+
var removeFromRecord = function (file) {
|
|
221
|
+
var id = getIdFromUrl(file.url);
|
|
222
|
+
var array = $scope.uploadScope.dataField();
|
|
223
|
+
for (var i = array.length - 1; i >= 0; i--) {
|
|
224
|
+
if (array[i]._id === id) {
|
|
225
|
+
array.splice(i, 1);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
$scope.clear(file);
|
|
229
|
+
$scope.ngModel.$setDirty();
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
if (file.url) {
|
|
233
|
+
file.$state = function () {
|
|
234
|
+
return state;
|
|
235
|
+
};
|
|
236
|
+
file.$destroy = function ($event) {
|
|
237
|
+
if (!$event || !$event.target.className.includes('ng-hide')) {
|
|
238
|
+
$scope.$parent.$parent.mouseIn = false;
|
|
239
|
+
state = 'pending';
|
|
240
|
+
return $http({
|
|
241
|
+
url: file.deleteUrl,
|
|
242
|
+
method: file.deleteType
|
|
243
|
+
}).then(
|
|
244
|
+
function () {
|
|
245
|
+
state = 'resolved';
|
|
246
|
+
removeFromRecord(file);
|
|
247
|
+
},
|
|
248
|
+
function () {
|
|
249
|
+
state = 'rejected';
|
|
250
|
+
removeFromRecord(file);
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
} else if (!file.$cancel && !file._index) {
|
|
256
|
+
file.$cancel = function () {
|
|
257
|
+
removeFromRecord(file);
|
|
258
|
+
};
|
|
225
259
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
file.$cancel = function () {
|
|
230
|
-
removeFromRecord(file);
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
]);
|
|
235
|
-
})();
|
|
260
|
+
}
|
|
261
|
+
]);
|
|
262
|
+
})();
|