1 | $(function () { |
||
2 | |||
3 | 'use strict'; |
||
4 | |||
5 | var console = window.console || { log: function () {} }, |
||
6 | $alert = $('.docs-alert'), |
||
7 | $message = $alert.find('.message'), |
||
8 | showMessage = function (message, type) { |
||
9 | $message.text(message); |
||
10 | |||
11 | if (type) { |
||
12 | $message.addClass(type); |
||
13 | } |
||
14 | |||
15 | $alert.fadeIn(); |
||
16 | |||
17 | setTimeout(function () { |
||
18 | $alert.fadeOut(); |
||
19 | }, 3000); |
||
20 | }; |
||
21 | |||
22 | // Demo |
||
23 | // ------------------------------------------------------------------------- |
||
24 | |||
25 | (function () { |
||
26 | var $image = $('.img-container > img'), |
||
27 | $dataX = $('#dataX'), |
||
28 | $dataY = $('#dataY'), |
||
29 | $dataHeight = $('#dataHeight'), |
||
30 | $dataWidth = $('#dataWidth'), |
||
31 | $dataRotate = $('#dataRotate'), |
||
32 | options = { |
||
33 | aspectRatio: 16 / 9, |
||
34 | preview: '.img-preview', |
||
35 | crop: function (data) { |
||
36 | $dataX.val(Math.round(data.x)); |
||
37 | $dataY.val(Math.round(data.y)); |
||
38 | $dataHeight.val(Math.round(data.height)); |
||
39 | $dataWidth.val(Math.round(data.width)); |
||
40 | $dataRotate.val(Math.round(data.rotate)); |
||
41 | } |
||
42 | }; |
||
43 | |||
44 | $image.on({ |
||
45 | 'build.cropper': function (e) { |
||
46 | console.log(e.type); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
47 | }, |
||
48 | 'built.cropper': function (e) { |
||
49 | console.log(e.type); |
||
0 ignored issues
–
show
|
|||
50 | } |
||
51 | }).cropper(options); |
||
52 | |||
53 | |||
54 | // Methods |
||
55 | $(document.body).on('click', '[data-method]', function () { |
||
56 | var data = $(this).data(), |
||
57 | $target, |
||
58 | result; |
||
59 | |||
60 | if (data.method) { |
||
61 | data = $.extend({}, data); // Clone a new one |
||
62 | |||
63 | if (typeof data.target !== 'undefined') { |
||
64 | $target = $(data.target); |
||
65 | |||
66 | if (typeof data.option === 'undefined') { |
||
67 | try { |
||
68 | data.option = JSON.parse($target.val()); |
||
69 | } catch (e) { |
||
70 | console.log(e.message); |
||
0 ignored issues
–
show
|
|||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | result = $image.cropper(data.method, data.option); |
||
76 | |||
77 | if (data.method === 'getDataURL') { |
||
78 | $('#getDataURLModal').modal().find('.modal-body').html('<img src="' + result + '">'); |
||
79 | } |
||
80 | |||
81 | if ($.isPlainObject(result) && $target) { |
||
82 | try { |
||
83 | $target.val(JSON.stringify(result)); |
||
84 | } catch (e) { |
||
85 | console.log(e.message); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | } |
||
90 | }).on('keydown', function (e) { |
||
91 | |||
92 | switch (e.which) { |
||
93 | case 37: |
||
94 | e.preventDefault(); |
||
95 | $image.cropper('move', -1, 0); |
||
96 | break; |
||
97 | |||
98 | case 38: |
||
99 | e.preventDefault(); |
||
100 | $image.cropper('move', 0, -1); |
||
101 | break; |
||
102 | |||
103 | case 39: |
||
104 | e.preventDefault(); |
||
105 | $image.cropper('move', 1, 0); |
||
106 | break; |
||
107 | |||
108 | case 40: |
||
109 | e.preventDefault(); |
||
110 | $image.cropper('move', 0, 1); |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | }); |
||
115 | |||
116 | |||
117 | // Import image |
||
118 | var $inputImage = $('#inputImage'), |
||
119 | URL = window.URL || window.webkitURL, |
||
120 | blobURL; |
||
121 | |||
122 | if (URL) { |
||
123 | $inputImage.change(function () { |
||
124 | var files = this.files, |
||
125 | file; |
||
126 | |||
127 | if (files && files.length) { |
||
128 | file = files[0]; |
||
129 | |||
130 | if (/^image\/\w+$/.test(file.type)) { |
||
131 | blobURL = URL.createObjectURL(file); |
||
132 | $image.one('built.cropper', function () { |
||
133 | URL.revokeObjectURL(blobURL); // Revoke when load complete |
||
134 | }).cropper('reset', true).cropper('replace', blobURL); |
||
135 | $inputImage.val(''); |
||
136 | } else { |
||
137 | showMessage('Please choose an image file.'); |
||
138 | } |
||
139 | } |
||
140 | }); |
||
141 | } else { |
||
142 | $inputImage.parent().remove(); |
||
143 | } |
||
144 | |||
145 | |||
146 | // Options |
||
147 | $('.docs-options :checkbox').on('change', function () { |
||
148 | var $this = $(this); |
||
149 | |||
150 | options[$this.val()] = $this.prop('checked'); |
||
151 | $image.cropper('destroy').cropper(options); |
||
152 | }); |
||
153 | |||
154 | |||
155 | // Tooltips |
||
156 | $('[data-toggle="tooltip"]').tooltip(); |
||
157 | |||
158 | }()); |
||
159 | |||
160 | }); |
||
161 |