fng-jq-upload 0.12.0-beta.21 → 0.12.0-beta.211

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 CHANGED
@@ -52,14 +52,23 @@ 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');
@@ -67,9 +76,10 @@ module.exports = function (grunt) {
67
76
  grunt.loadNpmTasks('grunt-contrib-uglify');
68
77
  grunt.loadNpmTasks('grunt-angular-templates');
69
78
  grunt.loadNpmTasks('grunt-mocha-test');
79
+ grunt.loadNpmTasks('grunt-karma');
70
80
 
71
- grunt.registerTask('test', ['jshint', 'mochaTest']);
81
+ grunt.registerTask('test', ['jshint', 'mochaTest', 'karma']);
72
82
  grunt.registerTask('build', ['ngtemplates', 'concat', 'uglify']);
73
83
  grunt.registerTask('default', ['test']);
74
84
 
75
- };
85
+ };
@@ -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
- 'use strict';
7
+ 'use strict';
3
8
 
4
- var app = angular.module('uploadModule', [
5
- 'blueimp.fileupload'
6
- ]);
9
+ var app = angular.module('uploadModule', [
10
+ 'blueimp.fileupload'
11
+ ]);
7
12
 
8
- var getIdFromUrl = function (url) {
9
- var breakdown = /\/file\/(.+?)\/([0-9a-f]{24})/.exec(url);
10
- return breakdown[2];
11
- };
13
+ var getIdFromUrl = function (url) {
14
+ var breakdown = /\/file\/(.+?)\/([0-9a-f]{24})/.exec(url);
15
+ return breakdown[2];
16
+ };
12
17
 
13
- app
14
- .controller('FngUploadAdditFieldsCtrl', ['$scope', '$timeout', function($scope, $timeout){
15
- $scope.record = {};
18
+ app
19
+ .controller('FngUploadAdditFieldsCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
20
+ $scope.record = {};
16
21
 
17
- $scope.$watch('record', function (newVal, oldVal) {
18
- if (newVal !== oldVal) {
19
- $scope.uploadForm.formScope[$scope.uploadForm.formScope.topLevelFormName].$setDirty();
20
- angular.extend($scope.uploadForm.dataField()[$scope.file], newVal);
21
- }
22
- }, true);
23
-
24
- function setUpAdditFields() {
25
- if ($scope.schema) {
26
- $scope.schema.forEach(function (field) {
27
- var objVal = $scope.uploadForm.dataField()[$scope.file];
28
- $scope.record[field.name] = objVal ? objVal[field.name] : undefined;
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
- if ($scope.options.subkey) {
92
- var arrayIndex = $scope.formScope['$_arrayOffset_' + root.replace(/\./g, '_') + '_' + $scope.options.subkeyno];
93
- if (arrayIndex != null && arrayIndex !== undefined && arrayIndex !== -1) {
94
- retVal = retVal[arrayIndex][lastPart];
95
- } else {
96
- retVal = undefined;
97
- }
98
- } else {
99
- console.log('No support for this yet');
100
- //modelString += '[$index].' + lastPart;
101
- //idString = null;
102
- //nameString = compoundName.replace(/\./g, '-');
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
- } else {
106
- console.log('No support for this yet either');
107
- }
108
- return retVal;
109
- };
110
-
111
- function setUpAttachments() {
112
- var storedData = $scope.dataField();
113
- if (storedData) {
114
- for (var i = 0; i < storedData.length; i++) {
115
- var storedElement = storedData[i];
116
- var storedName = storedElement.filename;
117
- var queueElement = {
118
- 'name': storedName,
119
- 'size': storedElement.size,
120
- 'url': '/api/file/' + $scope.formScope.modelName + '/' + storedElement._id,
121
- 'deleteUrl': '/api/file/' + $scope.formScope.modelName + '/' + storedElement._id,
122
- 'deleteType': 'DELETE'
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
- switch (storedName.slice(storedName.length - 4, storedName.length)) { // extension
125
- case '.gif':
126
- case '.png':
127
- case '.jpg':
128
- queueElement.thumbnailUrl = '/api/file/' + $scope.formScope.modelName + '/thumbnail/' + storedElement._id;
129
- break;
130
- default:
131
- queueElement.thumbnailUrl = 'https://upload.wikimedia.org/wikipedia/commons/7/77/Icon_New_File_256x256.png';
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
- $scope.$$childHead.queue.push(queueElement);
134
- }
135
- }
136
- }
137
-
138
- if (!$scope.formScope.newRecord) {
139
- var watchDeregister = $scope.formScope.$watch('phase', function (newVal) {
140
- if (newVal === 'ready') {
141
- $scope.$$childHead.queue = $scope.$$childHead.queue || [];
142
- setUpAttachments();
143
- $scope.$on('fngCancel', function () {
144
- $scope.$$childHead.queue = [];
145
- setUpAttachments();
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
- watchDeregister();
149
- }
150
- });
151
- }
152
-
153
- $scope.$on('fileuploaddone', function (event, data) {
154
- var field = $scope.dataField(true);
155
- var fileDetails = data.result.files[0];
156
- field.push(
157
- {
158
- _id: getIdFromUrl(fileDetails.url),
159
- filename: fileDetails.name,
160
- size: fileDetails.size
161
- });
162
- $scope.ngModel.$setDirty();
163
- });
164
-
165
- }])
166
- .directive('fngJqUploadForm', ['pluginHelper', function (pluginHelper) {
167
- return {
168
- link: function (scope, element, attrs, ngModel) {
169
- angular.extend(scope, pluginHelper.extractFromAttr(attrs, 'fngJqUploadForm'));
170
- // Pick up options from the mongoose schema
171
- scope.passedParams = scope.formScope[attrs.schema];
172
- angular.extend(scope.options, scope.passedParams.fngJqUploadForm);
173
- if (scope.options.additFields) {
174
- scope.directiveOptions.additFields = JSON.parse(scope.options.additFields);
175
- }
176
- scope.directiveOptions.url = '/api/file/upload/' + scope.formScope.modelName;
177
- scope.directiveOptions.sizeLimit = scope.directiveOptions.sizelimit;
178
- scope.directiveOptions.autoUpload = scope.directiveOptions.autoupload;
179
- scope.name = scope.passedParams.name;
180
- scope.ngModel = ngModel;
181
- },
182
- restrict: 'E',
183
- require: '?ngModel',
184
- templateUrl: 'templates/fileform.html',
185
- scope: {},
186
- controller: 'fngJqUploadCtrl'
187
- };
188
- }])
189
- .controller('FileDestroyController', ['$scope', '$http', function ($scope, $http) {
190
- var file = $scope.file,
191
- state;
192
-
193
- $scope.uploadScope = $scope.$parent.$parent.$parent;
194
-
195
- var removeFromRecord = function (file) {
196
- var id = getIdFromUrl(file.url);
197
- var array = $scope.uploadScope.dataField();
198
- for (var i = array.length - 1; i >= 0; i--) {
199
- if (array[i]._id === id) {
200
- array.splice(i, 1);
201
- }
202
- }
203
- $scope.clear(file);
204
- $scope.ngModel.$setDirty();
205
- };
206
-
207
- if (file.url) {
208
- file.$state = function () {
209
- return state;
210
- };
211
- file.$destroy = function () {
212
- $scope.$parent.$parent.mouseIn = false;
213
- state = 'pending';
214
- return $http({
215
- url: file.deleteUrl,
216
- method: file.deleteType
217
- }).then(
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
- } else if (!file.$cancel && !file._index) {
229
- file.$cancel = function () {
230
- removeFromRecord(file);
231
- };
232
- }
233
- }
234
- ]);
235
- })();
260
+ }
261
+ ]);
262
+ })();