Skip to content

Commit af5b38d

Browse files
committed
v1.0.1
- Overall enhancements - Fix #5
1 parent 077ac16 commit af5b38d

File tree

7 files changed

+81
-50
lines changed

7 files changed

+81
-50
lines changed

.versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mongo-id@1.0.5
4141
npm-mongo@1.5.45
4242
observe-sequence@1.0.12
4343
ordered-dict@1.0.8
44-
ostrio:autoform-files@1.0.0
44+
ostrio:autoform-files@1.0.1
4545
ostrio:cookies@2.0.5
4646
ostrio:files@1.6.9
4747
promise@0.8.3

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ Autoform File
22
=============
33

44
### Description
5-
Upload and manage files with autoForm using ostrio:files.
6-
This was ported from yogiben:autoform-file to use ostiro:files instead of the now deprecated CollectionFS.
5+
Upload and manage files with autoForm via [`ostrio:files`](https://github.com/VeliovGroup/Meteor-Files). This package was ported from `yogiben:autoform-file` to use with [`ostrio:files`](https://github.com/VeliovGroup/Meteor-Files) instead of the already deprecated CollectionFS.
76

8-
### Quick Start
9-
##### 1. Install `meteor add ostrio:autoform-files`
10-
##### 2. Create your Files Collection (See [ostrio:files](https://github.com/VeliovGroup/Meteor-Files.git))
7+
### Quick Start:
8+
9+
- Install `meteor add ostrio:autoform-files`
10+
- Install `meteor add ostrio:files`, *if not yet installed*
11+
- Create your Files Collection (See [`ostrio:files`](https://github.com/VeliovGroup/Meteor-Files)))
1112
```javascript
1213
var Images = new FilesCollection({
1314
collectionName: 'Images',
@@ -32,12 +33,12 @@ if (Meteor.isServer) {
3233
});
3334
}
3435
```
35-
##### 3. Define your schema and set the `autoform` property like in the example below
36-
```javascript
37-
Schemas = {}
38-
39-
Posts = new Meteor.Collection('posts');
36+
__Note:__ `Images` variable must be attached to *Global* scope. And has same name (*case-sensitive*) as `collectionName` option passed into `FilesCollectio#insert({collectionName: 'Images'})` method, `Images` in our case.
4037

38+
- Define your schema and set the `autoform` property like in the example below
39+
```javascript
40+
Schemas = {};
41+
Posts = new Meteor.Collection('posts');
4142
Schemas.Posts = new SimpleSchema({
4243
title: {
4344
type: String,
@@ -48,7 +49,7 @@ Schemas.Posts = new SimpleSchema({
4849
autoform: {
4950
afFieldInput: {
5051
type: 'fileUpload',
51-
collection: 'Images',
52+
collection: 'Images'
5253
}
5354
}
5455
}
@@ -57,17 +58,13 @@ Schemas.Posts = new SimpleSchema({
5758
Posts.attachSchema(Schemas.Posts);
5859
```
5960

60-
The `collection` property is the field name of your files collection.
61+
The `collection` property must be the same as name of your *FilesCollection* (*case-sensitive*), `Images` in our case.
6162

62-
##### 4. Generate the form with `{{> quickform}}` or `{{#autoform}}`
63+
- Generate the form with `{{> quickform}}` or `{{#autoform}}`
6364
e.g.:
64-
```
65+
```html
6566
{{> quickForm collection="Posts" type="insert"}}
66-
```
67-
68-
or
69-
70-
```
67+
<!-- OR -->
7168
{{#autoForm collection="Posts" type="insert"}}
7269
{{> afQuickField name="title"}}
7370
{{> afQuickField name="picture"}}
@@ -121,6 +118,6 @@ picture: {
121118

122119
```html
123120
<template name="myFilePreview">
124-
<a href="{{file.url}}">{{file.original.name}}</a>
121+
<a href="{{fileURL file}}">{{file.original.name}}</a>
125122
</template>
126123
```

lib/client/fileUpload.html

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<template name="afFileUpload">
22
<input value="{{fileId}}" type="hidden" {{this.atts}}>
3-
{{#if $gt uploadedFile.count 0}}
4-
{{#each uploadedFile}}
5-
<div>
3+
{{#with uploadedFile}}
4+
<div>
5+
{{#if isImage}}
66
{{> uploadImageDemo}}
7-
</div>
8-
<a data-action="remove-file">Remove</a>
9-
{{/each}}
7+
{{else}}
8+
{{> uploadFileDemo}}
9+
{{/if}}
10+
</div>
11+
<a data-remove-file>Remove</a>
1012
{{else}}
11-
{{#if currentUpload}}
12-
{{#with currentUpload}}
13-
Uploading <b>{{file.name}}</b>:
14-
<span class="progress">{{progress}}%</span>
15-
{{/with}}
13+
{{#with currentUpload}}
14+
Uploading <b>{{file.name}}</b>:
15+
<span class="progress">{{progress}}%</span>
1616
{{else}}
17-
<input class="form-control af-file-upload-capture" type="file"/>
18-
{{/if}}
19-
{{/if}}
17+
<input data-files-collection-upload class="form-control af-file-upload-capture" type="file" />
18+
{{#if error}}
19+
<span class="help-block">{{error}}</span>
20+
{{/if}}
21+
{{/with}}
22+
{{/with}}
2023
</template>

lib/client/fileUpload.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
Template.afFileUpload.onCreated(function () {
2+
this.collection = Meteor.connection._mongo_livedata_collections[this.data.atts.collection]
3+
if (!this.collection) {
4+
throw new Meteor.Error(404, '[meteor-autoform-files] No such collection "' + this.data.atts.collection + '"');
5+
}
26
this.collectionName = () => this.data.atts.collection
3-
this.fileId = new ReactiveVar(this.data.value);
4-
this.currentUpload = new ReactiveVar(false);
7+
this.currentUpload = new ReactiveVar(false);
8+
this.fileId = new ReactiveVar(this.data.value);
9+
this.error = new ReactiveVar(false);
510
return;
611
});
712

@@ -13,19 +18,28 @@ Template.afFileUpload.helpers({
1318
return Template.instance().fileId.get();
1419
},
1520
uploadedFile() {
16-
let fId = Template.instance().fileId.get();
17-
let file = global[Template.instance().collectionName()].find({_id: fId});
18-
return file.cursor;
21+
return global[Template.instance().collectionName()].findOne({
22+
_id: Template.instance().fileId.get()
23+
});
24+
},
25+
error() {
26+
return Template.instance().error.get();
1927
}
2028
});
2129

2230
Template.afFileUpload.events({
23-
'click [data-action="remove-file"]'(e, t) {
24-
t.fileId.set(false);
31+
'click [data-remove-file]'(e, template) {
32+
template.fileId.set(false);
33+
try {
34+
this.remove();
35+
} catch (e) {}
2536
return;
2637
},
27-
'change .af-file-upload-capture'(e, template) {
38+
'change [data-files-collection-upload]'(e, template) {
2839
if (e.currentTarget.files && e.currentTarget.files[0]) {
40+
const input = $(e.currentTarget);
41+
const parent = input.parent('.form-group');
42+
parent.removeClass('has-error');
2943
const upload = global[template.collectionName()].insert({
3044
file: e.currentTarget.files[0],
3145
streams: 'dynamic',
@@ -37,9 +51,20 @@ Template.afFileUpload.events({
3751
return;
3852
});
3953

54+
upload.on('error', function (error) {
55+
template.error.set(error.reason);
56+
input.val('');
57+
if (parent[0]) {
58+
parent.addClass('has-error');
59+
}
60+
return;
61+
});
62+
4063
upload.on('end', (error, fileObj) => {
4164
if (!error) {
42-
template.fileId.set(fileObj._id);
65+
if (template) {
66+
template.fileId.set(fileObj._id);
67+
}
4368
}
4469
template.currentUpload.set(false);
4570
return;

lib/client/uploadFileDemo.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template name="uploadFileDemo">
2+
<a href="{{link}}?download=true" target="_top">{{name}}</a>
3+
</template>

lib/client/uploadImageDemo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template name="uploadImageDemo">
2-
<img style="width:50%;" src="{{fileURL .}}"/>
2+
<img style="width:50%;max-width:250px;max-height:250px;" src="{{link}}"/>
33
</template>

package.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package.describe({
22
name: "ostrio:autoform-files",
33
summary: "File upload for AutoForm using ostrio:files",
44
description: "File upload for AutoForm using ostrio:files",
5-
version: "1.0.0",
5+
version: "1.0.1",
66
git: "https://github.com/VeliovGroup/meteor-autoform-file.git"
77
});
88

@@ -18,8 +18,11 @@ Package.onUse(function(api) {
1818
'ostrio:files@1.6.9'
1919
]);
2020

21-
api.addFiles('lib/client/autoform.js', 'client');
22-
api.addFiles('lib/client/fileUpload.html', 'client');
23-
api.addFiles('lib/client/fileUpload.js', 'client');
24-
api.addFiles('lib/client/uploadImageDemo.html', 'client');
21+
api.addFiles([
22+
'lib/client/autoform.js',
23+
'lib/client/fileUpload.html',
24+
'lib/client/fileUpload.js',
25+
'lib/client/uploadImageDemo.html',
26+
'lib/client/uploadFileDemo.html'
27+
], 'client');
2528
});

0 commit comments

Comments
 (0)