the_crop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,205 @@
1
+ @TheCrop = do ->
2
+ # HELPERS
3
+ dec: (val) -> parseInt val, 10
4
+ json_to_str: (json = {}) -> JSON.stringify(json)
5
+ by_id: (id) -> document.getElementById(id)
6
+
7
+ # INIT
8
+ init: ->
9
+ @params = {}
10
+ do @init_open_btn
11
+ do @init_close_btn
12
+ do @init_submit
13
+ do @init_ajaxian_callback
14
+
15
+ init_open_btn: ->
16
+ $('.js_the_crop').on 'click', (e) =>
17
+ link = $ e.target
18
+ params = link.data()
19
+ @params = params if params
20
+
21
+ @show_canvas()
22
+ @create(params)
23
+ false
24
+
25
+ init_close_btn: ->
26
+ $('.js_the_crop_close').on 'click', =>
27
+ do @destroy
28
+ do @hide_canvas
29
+ false
30
+
31
+ init_submit: ->
32
+ $('.js_the_crop_submit').on 'click', =>
33
+ form = $('.js_crop_form')
34
+
35
+ x = $('#crop_x', form).val()
36
+ y = $('#crop_y', form).val()
37
+ w = $('#crop_w', form).val()
38
+ h = $('#crop_h', form).val()
39
+
40
+ if x is '0' && y is '0' && w is '0' && h is '0'
41
+ alert 'Please, select crop area'
42
+ else
43
+ $('.js_crop_form').submit()
44
+
45
+ false
46
+
47
+ init_ajaxian_callback: ->
48
+ $('.js_crop_form').on 'ajax:success', (e, data, status, xhr) =>
49
+ callback = null
50
+ fn_chain = @params.callbackHandler.split '.'
51
+
52
+ for fn in fn_chain
53
+ callback = if callback then callback[fn] else window[fn]
54
+
55
+ callback(data, @params) if callback
56
+
57
+ init_crop_form: ->
58
+ $('.js_crop_form').attr('action', @params.url)
59
+
60
+ init_jcrop: (context) =>
61
+ $('#js_jcrop_target').Jcrop
62
+ onChange: context.buildPreview
63
+ onSelect: context.buildPreview
64
+ setSelect: [0,0,100,100]
65
+ aspectRatio: context.get_aspect_ration()
66
+ , ->
67
+ context.api = @
68
+
69
+ # GETTERS
70
+ get_aspect_ration: ->
71
+ prev = $('.js_preview_image')
72
+ @dec(prev.css('width')) / @dec(prev.css('height'))
73
+
74
+ # SETTERS
75
+ set_crop_form_params: (c) ->
76
+ form = $('.js_crop_form')
77
+ orig_img = $('#js_jcrop_target')
78
+
79
+ # Set img size for calc scale value
80
+ img_w = $('#crop_img_w', form)
81
+ img_w.val TheCrop.dec orig_img.css('width')
82
+
83
+ # Set crop params
84
+ x = $('#crop_x', form)
85
+ y = $('#crop_y', form)
86
+ w = $('#crop_w', form)
87
+ h = $('#crop_h', form)
88
+
89
+ x.val(c.x); y.val(c.y)
90
+ w.val(c.w); h.val(c.h)
91
+
92
+ set_preview_defaults: ->
93
+ $('.js_preview_image').css
94
+ width: 300
95
+ height: 300
96
+
97
+ set_preview_dimensions: ->
98
+ if prev_opt = @params?.preview
99
+ if prev_opt?.width && prev_opt?.height
100
+ $('.js_preview_image').css
101
+ width: prev_opt.width
102
+ height: prev_opt.height
103
+
104
+ set_holder_defaults: ->
105
+ if holder_opt = @params?.holder
106
+ if holder_opt?.width
107
+ $('.js_source_image').css
108
+ width: holder_opt.width
109
+
110
+ set_holder_image_same_dimentions: ->
111
+ holder = $('.js_source_image')
112
+ src_img = $('#js_jcrop_target')
113
+
114
+ width = @dec holder.css 'width'
115
+ src_img.css { width: width }
116
+
117
+ src_img_height = @dec src_img.css 'height'
118
+ holder.css { height: src_img_height }
119
+
120
+ set_original_image_size_info: ->
121
+ w = @by_id('js_jcrop_target').width
122
+ h = @by_id('js_jcrop_target').height
123
+
124
+ $('.js_the_crop_src_size').html """
125
+ #{ w }x#{ h } (px)
126
+ """
127
+
128
+ set_croped_image_size_info: (w, h) ->
129
+ $('.js_the_crop_cropped_size').html """
130
+ #{ w }x#{ h } (px)
131
+ """
132
+
133
+ set_final_size_info: ->
134
+ if @params.finalSize
135
+ item = $('.js_the_crop_final_size')
136
+ item.html "#{ @params.finalSize } (px)"
137
+ item.parent().show()
138
+
139
+ set_canvas_dimensions: ->
140
+ do @set_preview_defaults
141
+ do @set_preview_dimensions
142
+
143
+ do @set_holder_defaults
144
+ do @set_holder_image_same_dimentions
145
+
146
+ # FUNCTIONS
147
+ create: ->
148
+ do @set_original_image_size_info
149
+ do @set_canvas_dimensions
150
+ do @set_final_size_info
151
+ do @init_crop_form
152
+ @init_jcrop @
153
+
154
+ destroy: ->
155
+ @api.destroy()
156
+
157
+ buildPreview: (coords) ->
158
+ preview_holder = $('.js_preview_image')
159
+ original_img = $('#js_jcrop_target')
160
+
161
+ preview_view_w = TheCrop.dec preview_holder.css('width')
162
+ preview_view_h = TheCrop.dec preview_holder.css('height')
163
+
164
+ original_view_w = TheCrop.dec original_img.css('width')
165
+ original_view_h = TheCrop.dec original_img.css('height')
166
+
167
+ orig_image_w = TheCrop.by_id('js_jcrop_target').width
168
+
169
+ # Calculate scale
170
+ scale = original_view_w / orig_image_w
171
+ sw = TheCrop.dec coords.w / scale
172
+ sh = TheCrop.dec coords.h / scale
173
+
174
+ # Set scaled sizes
175
+ TheCrop.set_croped_image_size_info(sw, sh)
176
+
177
+ # When crop-area not selected
178
+ if sw is 0 && sh is 0
179
+ TheCrop.set_crop_form_params({ x: 0, y: 0, w: 0, h: 0 })
180
+ else
181
+ TheCrop.set_crop_form_params(coords)
182
+
183
+ # Calculate values for preview
184
+ rx = preview_view_w / coords.w
185
+ ry = preview_view_h / coords.h
186
+
187
+ $('#js_preview').css
188
+ width: "#{ Math.round(rx * original_view_w) }px"
189
+ height: "#{ Math.round(ry * original_view_h) }px"
190
+
191
+ marginLeft: "-#{ Math.round(rx * coords.x) }px"
192
+ marginTop: "-#{ Math.round(ry * coords.y) }px"
193
+
194
+ # OTHERS
195
+ show_canvas: ->
196
+ canvas = $('.js_the_crop_canvas')
197
+
198
+ canvas.css
199
+ width: $(document).width()
200
+ height: $(document).height()
201
+
202
+ canvas.fadeIn()
203
+
204
+ hide_canvas: ->
205
+ $('.js_the_crop_canvas').fadeOut()
@@ -0,0 +1,167 @@
1
+ /* jquery.Jcrop.css v0.9.12 - MIT License */
2
+ /*
3
+ The outer-most container in a typical Jcrop instance
4
+ If you are having difficulty with formatting related to styles
5
+ on a parent element, place any fixes here or in a like selector
6
+
7
+ You can also style this element if you want to add a border, etc
8
+ A better method for styling can be seen below with .jcrop-light
9
+ (Add a class to the holder and style elements for that extended class)
10
+ */
11
+ .jcrop-holder {
12
+ direction: ltr;
13
+ text-align: left;
14
+ /* IE10 touch compatibility */
15
+ -ms-touch-action: none;
16
+ }
17
+ /* Selection Border */
18
+ .jcrop-vline,
19
+ .jcrop-hline {
20
+ background: #ffffff url("Jcrop.gif");
21
+ font-size: 0;
22
+ position: absolute;
23
+ }
24
+ .jcrop-vline {
25
+ height: 100%;
26
+ width: 1px !important;
27
+ }
28
+ .jcrop-vline.right {
29
+ right: 0;
30
+ }
31
+ .jcrop-hline {
32
+ height: 1px !important;
33
+ width: 100%;
34
+ }
35
+ .jcrop-hline.bottom {
36
+ bottom: 0;
37
+ }
38
+ /* Invisible click targets */
39
+ .jcrop-tracker {
40
+ height: 100%;
41
+ width: 100%;
42
+ /* "turn off" link highlight */
43
+ -webkit-tap-highlight-color: transparent;
44
+ /* disable callout, image save panel */
45
+ -webkit-touch-callout: none;
46
+ /* disable cut copy paste */
47
+ -webkit-user-select: none;
48
+ }
49
+ /* Selection Handles */
50
+ .jcrop-handle {
51
+ background-color: #333333;
52
+ border: 1px #eeeeee solid;
53
+ width: 7px;
54
+ height: 7px;
55
+ font-size: 1px;
56
+ }
57
+ .jcrop-handle.ord-n {
58
+ left: 50%;
59
+ margin-left: -4px;
60
+ margin-top: -4px;
61
+ top: 0;
62
+ }
63
+ .jcrop-handle.ord-s {
64
+ bottom: 0;
65
+ left: 50%;
66
+ margin-bottom: -4px;
67
+ margin-left: -4px;
68
+ }
69
+ .jcrop-handle.ord-e {
70
+ margin-right: -4px;
71
+ margin-top: -4px;
72
+ right: 0;
73
+ top: 50%;
74
+ }
75
+ .jcrop-handle.ord-w {
76
+ left: 0;
77
+ margin-left: -4px;
78
+ margin-top: -4px;
79
+ top: 50%;
80
+ }
81
+ .jcrop-handle.ord-nw {
82
+ left: 0;
83
+ margin-left: -4px;
84
+ margin-top: -4px;
85
+ top: 0;
86
+ }
87
+ .jcrop-handle.ord-ne {
88
+ margin-right: -4px;
89
+ margin-top: -4px;
90
+ right: 0;
91
+ top: 0;
92
+ }
93
+ .jcrop-handle.ord-se {
94
+ bottom: 0;
95
+ margin-bottom: -4px;
96
+ margin-right: -4px;
97
+ right: 0;
98
+ }
99
+ .jcrop-handle.ord-sw {
100
+ bottom: 0;
101
+ left: 0;
102
+ margin-bottom: -4px;
103
+ margin-left: -4px;
104
+ }
105
+ /* Dragbars */
106
+ .jcrop-dragbar.ord-n,
107
+ .jcrop-dragbar.ord-s {
108
+ height: 7px;
109
+ width: 100%;
110
+ }
111
+ .jcrop-dragbar.ord-e,
112
+ .jcrop-dragbar.ord-w {
113
+ height: 100%;
114
+ width: 7px;
115
+ }
116
+ .jcrop-dragbar.ord-n {
117
+ margin-top: -4px;
118
+ }
119
+ .jcrop-dragbar.ord-s {
120
+ bottom: 0;
121
+ margin-bottom: -4px;
122
+ }
123
+ .jcrop-dragbar.ord-e {
124
+ margin-right: -4px;
125
+ right: 0;
126
+ }
127
+ .jcrop-dragbar.ord-w {
128
+ margin-left: -4px;
129
+ }
130
+ /* The "jcrop-light" class/extension */
131
+ .jcrop-light .jcrop-vline,
132
+ .jcrop-light .jcrop-hline {
133
+ background: #ffffff;
134
+ filter: alpha(opacity=70) !important;
135
+ opacity: .70!important;
136
+ }
137
+ .jcrop-light .jcrop-handle {
138
+ -moz-border-radius: 3px;
139
+ -webkit-border-radius: 3px;
140
+ background-color: #000000;
141
+ border-color: #ffffff;
142
+ border-radius: 3px;
143
+ }
144
+ /* The "jcrop-dark" class/extension */
145
+ .jcrop-dark .jcrop-vline,
146
+ .jcrop-dark .jcrop-hline {
147
+ background: #000000;
148
+ filter: alpha(opacity=70) !important;
149
+ opacity: 0.7 !important;
150
+ }
151
+ .jcrop-dark .jcrop-handle {
152
+ -moz-border-radius: 3px;
153
+ -webkit-border-radius: 3px;
154
+ background-color: #ffffff;
155
+ border-color: #000000;
156
+ border-radius: 3px;
157
+ }
158
+ /* Simple macro to turn off the antlines */
159
+ .solid-line .jcrop-vline,
160
+ .solid-line .jcrop-hline {
161
+ background: #ffffff;
162
+ }
163
+ /* Fix for twitter bootstrap et al. */
164
+ .jcrop-holder img,
165
+ img.jcrop-preview {
166
+ max-width: none;
167
+ }
@@ -0,0 +1,70 @@
1
+ .the_crop_canvas{
2
+ display: none;
3
+ position: absolute;
4
+ top: 0; left: 0;
5
+ width: 100%; height: 100%;
6
+ z-index: 100;
7
+
8
+ &:before{
9
+ content:' ';
10
+ display: block;
11
+ position: absolute;
12
+ top: 0; left: 0;
13
+ width: 100%; height: 100%;
14
+ background: black;
15
+ opacity: 0.92;
16
+ z-index: -1;
17
+ }
18
+
19
+ .header_block{
20
+ p{
21
+ color: gray;
22
+ font-size: 12px;
23
+ margin-bottom: 3px;
24
+ line-height: 120%;
25
+ }
26
+ span{ color: white; }
27
+ margin-bottom: 7px;
28
+ }
29
+
30
+ .the_crop_body{
31
+ width: 90%;
32
+ margin: auto;
33
+ margin-top: 5%;
34
+ }
35
+
36
+ .the_crop_close{
37
+ float: right;
38
+ color: white;
39
+ }
40
+
41
+ .images_block{
42
+ overflow: hidden; zoom: 1;
43
+ margin-bottom: 30px;
44
+ }
45
+
46
+ .source_image{
47
+ float: left;
48
+ margin-right: 30px;
49
+
50
+ border: 1px solid white;
51
+ background: gray;
52
+
53
+ width: 400px;
54
+ height: 300px;
55
+ overflow: hidden;
56
+ zoom:1;
57
+ }
58
+
59
+ .preview_image{
60
+ float: left;
61
+
62
+ border: 1px solid white;
63
+ background: gray;
64
+
65
+ width: 100px;
66
+ height: 100px;
67
+ overflow: hidden;
68
+ zoom:1;
69
+ }
70
+ }
@@ -0,0 +1,34 @@
1
+ - content_for :the_crop do
2
+ .the_crop_canvas.js_the_crop_canvas
3
+ .the_crop_body
4
+ .header_block
5
+ = link_to "Close", '#', class: 'btn btn-info the_crop_close js_the_crop_close'
6
+
7
+ %p
8
+ Original image size:
9
+ %span.js_the_crop_src_size ?
10
+
11
+ %p
12
+ Cropped image size:
13
+ %span.js_the_crop_cropped_size ?
14
+
15
+ %p{ style: 'display:none' }
16
+ Image will be resized to:
17
+ %span.js_the_crop_final_size ?
18
+
19
+ .images_block
20
+ .source_image.js_source_image
21
+ %img#js_jcrop_target{ src: @post.main_image.url(:original) }
22
+
23
+ .preview_image.js_preview_image
24
+ %img#js_preview{ src: @post.main_image.url(:original) }
25
+
26
+ .actions
27
+ = link_to "Crop!", '#', class: 'btn btn-info js_the_crop_submit'
28
+
29
+ = form_tag '#', class: :js_crop_form, method: :patch, remote: true, style: 'display:none' do
30
+ = text_field_tag 'crop[img_w]'
31
+ = text_field_tag 'crop[x]'
32
+ = text_field_tag 'crop[y]'
33
+ = text_field_tag 'crop[w]'
34
+ = text_field_tag 'crop[h]'