Project

General

Profile

1
$(function() {
2
    // image_cropper
3
    altair_image_cropper.init();
4
});
5

    
6

    
7
altair_image_cropper  = {
8
    init: function() {
9

    
10
        'use strict';
11

    
12
        var console = window.console || { log: function () {} };
13
        var $image = $('#image_cropper');
14
        var $download = $('#image_download');
15
        var options = {
16
            aspectRatio: 16 / 9,
17
            preview: '.cr_preview'
18
        };
19

    
20
        // Cropper
21
        $image.on({
22
            'build.cropper': function (e) {
23
                console.log(e.type);
24
            },
25
            'built.cropper': function (e) {
26
                console.log(e.type);
27
            },
28
            'cropstart.cropper': function (e) {
29
                console.log(e.type, e.action);
30
            },
31
            'cropmove.cropper': function (e) {
32
                console.log(e.type, e.action);
33
            },
34
            'cropend.cropper': function (e) {
35
                console.log(e.type, e.action);
36
            },
37
            'crop.cropper': function (e) {
38
                console.log(e.type, e.x, e.y, e.width, e.height, e.rotate, e.scaleX, e.scaleY);
39
            },
40
            'zoom.cropper': function (e) {
41
                console.log(e.type, e.ratio);
42
            }
43
        }).cropper(options);
44

    
45
        // Buttons
46
        if (!$.isFunction(document.createElement('canvas').getContext)) {
47
            $('button[data-method="getCroppedCanvas"]').prop('disabled', true);
48
        }
49

    
50
        if (typeof document.createElement('cropper').style.transition === 'undefined') {
51
            $('button[data-method="rotate"]').prop('disabled', true);
52
            $('button[data-method="scale"]').prop('disabled', true);
53
        }
54

    
55
        // Download
56
        console.log(typeof $download[0].download === 'undefined');
57
        if (typeof $download[0].download === 'undefined') {
58
            $download.addClass('disabled');
59
        }
60

    
61
        // Options
62
        $('.croper-toggles').on('click', 'button', function () {
63
            if($(this).hasClass('uk-active')) {
64
                var $this = $(this),
65
                    value = $this.attr('data-value');
66

    
67
                if (!$image.data('cropper')) {
68
                    return;
69
                }
70

    
71
                $image.cropper('setAspectRatio',value);
72
            }
73
        });
74

    
75
        // Methods
76
        $('.cropper-buttons').on('click', '[data-method]', function () {
77
            var $this = $(this);
78
            var data = $this.data();
79
            var $target;
80
            var result;
81

    
82
            if ($this.prop('disabled') || $this.hasClass('disabled')) {
83
                return;
84
            }
85

    
86
            if ($image.data('cropper') && data.method) {
87
                data = $.extend({}, data); // Clone a new one
88

    
89
                if (typeof data.target !== 'undefined') {
90
                    $target = $(data.target);
91

    
92
                    if (typeof data.option === 'undefined') {
93
                        try {
94
                            data.option = JSON.parse($target.val());
95
                        } catch (e) {
96
                            console.log(e.message);
97
                        }
98
                    }
99
                }
100

    
101
                result = $image.cropper(data.method, data.option, data.secondOption);
102

    
103
                switch (data.method) {
104
                    case 'scaleX':
105
                    case 'scaleY':
106
                        $(this).data('option', -data.option);
107
                        break;
108

    
109
                    case 'getCroppedCanvas':
110
                        if (result) {
111

    
112
                            var modalId = '#getCroppedCanvasModal',
113
                                modal = UIkit.modal(modalId);
114

    
115
                            $(modalId).find('.canvasModalImage').html(result);
116
                            modal.show();
117

    
118
                            if (!$download.hasClass('disabled')) {
119
                                $download.attr('href', result.toDataURL('image/jpeg'));
120
                            }
121
                        }
122

    
123
                        break;
124
                }
125

    
126
                if ($.isPlainObject(result) && $target) {
127
                    try {
128
                        $target.val(JSON.stringify(result));
129
                    } catch (e) {
130
                        console.log(e.message);
131
                    }
132
                }
133

    
134
            }
135
        });
136

    
137
        // Keyboard
138
        $(document.body).on('keydown', function (e) {
139

    
140
            if (!$image.data('cropper') || this.scrollTop > 300) {
141
                return;
142
            }
143

    
144
            switch (e.which) {
145
                case 37:
146
                    e.preventDefault();
147
                    $image.cropper('move', -1, 0);
148
                    break;
149

    
150
                case 38:
151
                    e.preventDefault();
152
                    $image.cropper('move', 0, -1);
153
                    break;
154

    
155
                case 39:
156
                    e.preventDefault();
157
                    $image.cropper('move', 1, 0);
158
                    break;
159

    
160
                case 40:
161
                    e.preventDefault();
162
                    $image.cropper('move', 0, 1);
163
                    break;
164
            }
165

    
166
        });
167

    
168
        // Import image
169
        var $inputImage = $('#inputImage'),
170
            $imageUrl;
171
        var URL = window.URL || window.webkitURL;
172
        var blobURL;
173

    
174
        if (URL) {
175
            $inputImage.change(function () {
176
                var files = this.files;
177
                var file;
178

    
179
                if (!$image.data('cropper')) {
180
                    return;
181
                }
182

    
183
                if (files && files.length) {
184
                    file = files[0];
185

    
186
                    if (/^image\/\w+$/.test(file.type)) {
187
                        blobURL = URL.createObjectURL(file);
188
                        $image.one('built.cropper', function () {
189

    
190
                            // Revoke when load complete
191
                            URL.revokeObjectURL(blobURL);
192
                        }).cropper('reset').cropper('replace', blobURL);
193
                        $inputImage.val('');
194
                        $imageUrl = blobURL;
195
                    } else {
196
                        window.alert('Please choose an image file.');
197
                    }
198
                }
199
            });
200
        } else {
201
            $inputImage.prop('disabled', true).parent().addClass('disabled');
202
        }
203

    
204
    }
205
};
(101-101/114)