This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
confirm.js
142 lines (120 loc) · 3.67 KB
/
confirm.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
define(function(require, exports, module) {
/*jshint laxbreak:true*/
'use strict';
/**
* Module Dependencies
*/
var prepareBlob = require('lib/prepare-preview-blob');
var debug = require('debug')('controller:confirm');
var resizeImageAndSave = require('lib/resize-image-and-save');
var ConfirmView = require('views/confirm');
var bindAll = require('lib/bind-all');
/**
* Exports
*/
module.exports = function(options) { return new ConfirmController(options); };
module.exports.ConfirmController = ConfirmController;
/**
* Initialize a new `ConfirmController`
*
* @param {Object} options
*/
function ConfirmController(app) {
this.app = app;
this.settings = app.settings;
this.activity = app.activity;
this.camera = app.camera;
this.container = app.el;
// Allow these dependencies
// to be injected if need be.
this.ConfirmView = app.ConfirmView || ConfirmView;
this.prepareBlob = app.prepareBlob || prepareBlob;
bindAll(this);
this.bindEvents();
debug('initialized');
}
ConfirmController.prototype.renderView = function() {
if (!this.activity.active) {
return;
}
if (this.confirmView) {
this.confirmView.show();
return;
}
// Check whether the MediaFrame should limit the pixel size.
var maxPreviewSize =
this.settings.previewGallery.get('limitMaxPreviewSize') ?
window.CONFIG_MAX_IMAGE_PIXEL_SIZE : 0;
this.confirmView = new this.ConfirmView();
this.confirmView.maxPreviewSize = maxPreviewSize;
this.confirmView.render().appendTo(this.container);
this.confirmView.on('click:select', this.onSelectMedia);
this.confirmView.on('click:retake', this.onRetakeMedia);
};
/**
* Bind callbacks to required events.
*
*/
ConfirmController.prototype.bindEvents = function() {
// Render/Show the view on the `newimage` and `newvideo` events
// since they are fired immediately when tapping 'Capture'/'Stop'.
// This prevents the 'Capture'/'Stop' button from being able to be
// triggered multiple times before the confirm view appears.
this.camera.on('newimage', this.renderView);
this.camera.on('newvideo', this.renderView);
// Update the MediaFrame contents with the image/video upon
// receiving the `newmedia` event. This event is slightly delayed
// since it waits for the storage callback to complete.
this.app.on('newmedia', this.onNewMedia);
};
/**
* When inside a 'pick' activity
* will present the user with a
* confirm overlay where they can
* decide to 'select' or 'retake'
* the photo or video
*
* @param {Object} data
*
*/
ConfirmController.prototype.onNewMedia = function(newMedia) {
if (!this.activity.active) { return; }
this.newMedia = newMedia;
if (newMedia.isVideo) { // Is video
this.confirmView.showVideo(newMedia);
} else { // Is Image
this.prepareBlob(this.newMedia.blob, this.confirmView.showImage);
}
};
ConfirmController.prototype.onSelectMedia = function() {
var activity = this.activity;
var needsResizing;
var media = {
blob: this.newMedia.blob
};
if (this.newMedia.isVideo) { // Is Video
media.type = 'video/3gpp';
media.poster = this.newMedia.poster.blob;
} else { // Is Image
media.type = 'image/jpeg';
needsResizing = activity.data.width || activity.data.height;
debug('needs resizing: %s', needsResizing);
if (needsResizing) {
resizeImageAndSave({
blob: media.blob,
width: activity.data.width,
height: activity.data.height
}, function(resizedBlob) {
media.blob = resizedBlob;
activity.postResult(media);
});
return;
}
}
activity.postResult(media);
};
ConfirmController.prototype.onRetakeMedia = function() {
this.confirmView.hide();
this.confirmView.clearMediaFrame();
};
});