oxen_media 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.bowerrc +3 -0
  3. data/.envrc +1 -0
  4. data/.gitignore +11 -0
  5. data/.rspec +3 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +16 -0
  8. data/Gemfile.lock +331 -0
  9. data/Guardfile +135 -0
  10. data/MIT-LICENSE +20 -0
  11. data/README.rdoc +31 -0
  12. data/Rakefile +15 -0
  13. data/app/assets/images/oxen_media/.keep +0 -0
  14. data/app/assets/javascripts/jsrender.min.js +4 -0
  15. data/app/assets/javascripts/jsrender.min.js.map +463 -0
  16. data/app/assets/javascripts/oxen_media.js +3 -0
  17. data/app/assets/javascripts/oxen_media/.keep +0 -0
  18. data/app/assets/javascripts/oxen_media/carrier_wave_cropper.js.coffee +22 -0
  19. data/app/assets/javascripts/oxen_media/media.js.coffee +469 -0
  20. data/app/assets/javascripts/oxen_media/medium_pane.js.coffee +107 -0
  21. data/app/assets/javascripts/templates/selected_files.html +35 -0
  22. data/app/assets/stylesheets/media.css +10 -0
  23. data/app/assets/stylesheets/oxen_media/.keep +0 -0
  24. data/app/assets/stylesheets/scaffold.css +56 -0
  25. data/app/controllers/.keep +0 -0
  26. data/app/controllers/media_controller.rb +106 -0
  27. data/app/helpers/.keep +0 -0
  28. data/app/helpers/media_helper.rb +2 -0
  29. data/app/mailers/.keep +0 -0
  30. data/app/models/.keep +0 -0
  31. data/app/models/concerns/roleable.rb +61 -0
  32. data/app/models/medium.rb +25 -0
  33. data/app/policies/oxen_medium_policy.rb +12 -0
  34. data/app/uploaders/medium_uploader.rb +106 -0
  35. data/app/views/.keep +0 -0
  36. data/app/views/media/_fields.html.haml +42 -0
  37. data/app/views/media/_form.html.haml +48 -0
  38. data/app/views/media/_media.html.haml +18 -0
  39. data/app/views/media/_medium.html.haml +25 -0
  40. data/app/views/media/_medium_fields.html.haml +4 -0
  41. data/app/views/media/create.js.haml +2 -0
  42. data/app/views/media/crop.html.haml +3 -0
  43. data/app/views/media/edit.html.haml +2 -0
  44. data/app/views/media/index.html.haml +29 -0
  45. data/app/views/media/index.json.jbuilder +4 -0
  46. data/app/views/media/new.html.erb +5 -0
  47. data/app/views/media/show.html.haml +21 -0
  48. data/app/views/media/show.json.jbuilder +1 -0
  49. data/app/views/media/update_crop.js.haml +1 -0
  50. data/bin/rails +12 -0
  51. data/config/initializers/backtrace_silencers.rb +9 -0
  52. data/config/initializers/carrier_wave.rb +11 -0
  53. data/config/initializers/simple_form.rb +14 -0
  54. data/config/locales/media.en.yml +20 -0
  55. data/config/routes.rb +6 -0
  56. data/db/migrate/20150629071720_create_media.rb +13 -0
  57. data/lib/generators/media/USAGE +8 -0
  58. data/lib/generators/media/media_generator.rb +48 -0
  59. data/lib/generators/media/templates/medium.rb +13 -0
  60. data/lib/oxen_media.rb +7 -0
  61. data/lib/oxen_media/engine.rb +19 -0
  62. data/lib/oxen_media/version.rb +3 -0
  63. data/lib/tasks/oxen_media_tasks.rake +4 -0
  64. data/oxen_media.gemspec +59 -0
  65. data/test/controllers/media_controller_test.rb +49 -0
  66. data/test/fixtures/media.yml +17 -0
  67. data/test/integration/oxen_media_test.rb +7 -0
  68. data/test/lib/generators/media_generator_test.rb +14 -0
  69. data/test/models/medium_test.rb +7 -0
  70. data/test/test_helper.rb +13 -0
  71. metadata +387 -0
@@ -0,0 +1,3 @@
1
+ //= require_tree ./oxen_media
2
+ //= require jsrender.min
3
+ //= require jquery.jcrop
File without changes
@@ -0,0 +1,22 @@
1
+ class CarrierWaveCropperClass
2
+ constructor: ->
3
+ $('#medium_medium_cropbox').Jcrop
4
+ setSelect: [0, 0, 200, 200]
5
+ onSelect: @update
6
+ onChange: @update
7
+
8
+ update: (coords) =>
9
+ $('#medium_medium_crop_x').val(coords.x)
10
+ $('#medium_medium_crop_y').val(coords.y)
11
+ $('#medium_medium_crop_w').val(coords.w)
12
+ $('#medium_medium_crop_h').val(coords.h)
13
+ @updatePreview(coords)
14
+
15
+ updatePreview: (coords) =>
16
+ $('#medium_medium_previewbox').css
17
+ width: Math.round(100/coords.w * $('#medium_medium_cropbox').width()) + 'px'
18
+ height: Math.round(100/coords.h * $('#medium_medium_cropbox').height()) + 'px'
19
+ marginLeft: '-' + Math.round(100/coords.w * coords.x) + 'px'
20
+ marginTop: '-' + Math.round(100/coords.h * coords.y) + 'px'
21
+
22
+ @CarrierWaveCropper = CarrierWaveCropperClass
@@ -0,0 +1,469 @@
1
+ # Place all the behaviors and hooks related to the matching controller here.
2
+ # All this logic will automatically be available in application.js.
3
+ # You can use CoffeeScript in this file: http://coffeescript.org/
4
+ class MediumClass
5
+
6
+ constructor: (@dropbox_element='.dropbox') ->
7
+ @filesForUpload = new Array
8
+ @all_files = 0
9
+ @uploaded_files = 0
10
+ @dropbox = $(@dropbox_element)
11
+
12
+ initializeBindings: (dragdrop=document) ->
13
+ @unBinds()
14
+ @doBinds()
15
+
16
+ #
17
+ # first unbind clicks
18
+ unBinds: () ->
19
+ $(document.body).unbind('click.media_start')
20
+ $(document.body).unbind('click.media_delete')
21
+ $(document.body).unbind('drop.media')
22
+ $(document.body).unbind('click.media_pick')
23
+
24
+ # draggable
25
+ $(document.body).unbind("dragleave.media")
26
+ $(document.body).unbind("dragover.media")
27
+ $(document.body).unbind("drop.media")
28
+ $(document.body).unbind("drag.media")
29
+ $(document.body).unbind("dragenter.media")
30
+ $(document.body).unbind('change.media')
31
+
32
+ $(document.body).unbind('click.preventDefaultClick')
33
+ $(document.body).unbind('click.cropFile')
34
+ $(document.body).unbind('click.uploadFile')
35
+ $(document.body).unbind('click.deleteFile')
36
+ $(document.body).unbind('click.toggleCardReveal')
37
+ $(document.body).unbind('click.clickMediumInput')
38
+
39
+ # @dropbox.unbind('click.media_cancel')
40
+ # $(document.body).unbind('click.cancelFile')
41
+
42
+
43
+ #
44
+ # bind clicks
45
+ doBinds: () ->
46
+ $(document.body).on 'click.media_show', '.edit_link', @show_media
47
+ $(document.body).on 'click.media_start', 'span.btn.hover_green.start', @uploadFile
48
+ $(document.body).on 'click.media_delete', 'span.btn.delete', @deleteFile
49
+ $(document.body).on 'click.clickMediumInput', '.fileinput-button', @clickMediumInput
50
+ $(document.body).on 'click.preventDefaultClick', '.fileinput-button, a.file-upload', @preventDefaultClick
51
+ $(document.body).on 'click.cropFile', 'a.edit_link.file-upload', @cropFile
52
+ $(document.body).on 'click.uploadFile', 'a.start.file-upload', @uploadFile
53
+ $(document.body).on 'click.deleteFile', 'a.delete.file-upload', @deleteFile
54
+ $(document.body).on 'click.toggleCardReveal', '.card_properties', @toggleCardReveal
55
+
56
+ $(document.body).on "drag.media", @dropbox_element, @dragMedia
57
+ $(document.body).on "dragenter.media", @dropbox_element, @dragEnterMedia
58
+ $(document.body).on "dragleave.media", @dropbox_element, @dragLeaveMedia
59
+ $(document.body).on "dragover.media", @dropbox_element, @dragOverMedia
60
+ $(document.body).on "drop.media", @dropbox_element, @dropMedia
61
+ $(document.body).on 'change.media', '#medium_medium', @updateSelectedFiles
62
+
63
+ # $(document.body).on 'click.cancelFile', 'a.cancel.file-upload', @cancelFile
64
+ # $(document.body).on 'click.media_cancel', 'span.btn.hover_yellow.cancel', @cancelUploading
65
+
66
+ preventDefaultClick: (e) ->
67
+ e.preventDefault()
68
+ e.stopPropagation()
69
+
70
+ toggleCardReveal: (e) =>
71
+ @preventDefaultClick(e)
72
+ $(e.currentTarget).closest('.card').find('.card-reveal').toggle()
73
+
74
+ clickMediumInput: (e) =>
75
+ @preventDefaultClick()
76
+ $('#medium_medium').click()
77
+
78
+ dragEnterMedia: (e) =>
79
+ @preventDefaultClick(e)
80
+
81
+ dragMedia: (e) =>
82
+ @preventDefaultClick(e)
83
+ # ev.dataTransfer.setData("text", ev.target.id);
84
+
85
+ dragOverMedia: (e) =>
86
+ @preventDefaultClick(e)
87
+ $(@dropbox_element).addClass('drop-ready')
88
+
89
+ dragLeaveMedia: (e) =>
90
+ @preventDefaultClick(e)
91
+ $(@dropbox_element).removeClass('drop-ready')
92
+
93
+ #
94
+ # dropMedia handles the 'drop' event
95
+ #
96
+ dropMedia: (e) =>
97
+ @preventDefaultClick(e)
98
+ # ev.preventDefault();
99
+ # var data = ev.dataTransfer.getData("text");
100
+ # ev.target.appendChild(document.getElementById(data));
101
+
102
+ $(e.currentTarget).removeClass('drop-ready')
103
+ dt = e.originalEvent.dataTransfer
104
+ files = dt.files
105
+ @handleFiles(files)
106
+
107
+ #
108
+ # show_media opens a modal window and presents the images
109
+ #
110
+ show_media: (e) =>
111
+ @preventDefaultClick(e)
112
+ $elem = $(e.currentTarget).closest('a')
113
+ _id = $elem.data('id')
114
+ url = $elem.attr('href') + '/crop.js'
115
+ $.ajax
116
+ url: url,
117
+ type: "get",
118
+ dataType: 'html'
119
+ .done (r) ->
120
+ $modal = $('#modal1')
121
+ $modal.find('.modal-content').html(r)
122
+ $modal.openModal
123
+ ready: () ->
124
+ $('a.close-modal').on 'click', (e) ->
125
+ e.preventDefault()
126
+ e.stopPropagation()
127
+ jqxhr = $.ajax
128
+ url: '/media/' + _id + '/update_crop.js'
129
+ method: 'POST'
130
+ dataType: 'html'
131
+ data:
132
+ id: _id,
133
+ medium:
134
+ medium_crop_x: $('#medium_medium_crop_x').val(),
135
+ medium_crop_y: $('#medium_medium_crop_y').val(),
136
+ medium_crop_w: $('#medium_medium_crop_w').val(),
137
+ medium_crop_h: $('#medium_medium_crop_h').val()
138
+ .fail (r) ->
139
+ console.log r
140
+ .done (r) ->
141
+ $elem.closest('.card').find('.card-image').html(r)
142
+ $modal.closeModal()
143
+
144
+ new CarrierWaveCropper()
145
+
146
+ # statusCode:
147
+ # 200: (response,textStatus,jqXHR) ->
148
+ # $modal = $('#modal1')
149
+ # $modal.find('.modal-content').html(response.responseText)
150
+ # $modal.attr('task_id', _id)
151
+ # $modal.openModal()
152
+ # set_close_event(elem)
153
+ # 401: () ->
154
+ # alert 'Du har ikke lov at se denne opgave!?!'
155
+
156
+
157
+
158
+ # delete all files ready for upload - or if non exists - delete all
159
+ # media attached to this object
160
+ #
161
+ deleteFile: (elem='all') =>
162
+ deleteAllFiles = elem == 'all'
163
+ if deleteAllFiles
164
+ elem = $($(@dropbox_element).find('.card.new_upload'))
165
+ if elem.length==0
166
+ $('.sweet-overlay').show()
167
+ elem = swal
168
+ title: "Ingen filer der skal overføres?",
169
+ text: "Vil du istedet slette alle tilknyttede medier, vist herunder permanent! Du vil ikke kunne hente dem frem igen!",
170
+ type: "warning",
171
+ html: true,
172
+ showCancelButton: true,
173
+ confirmButtonColor: "#DD6B55",
174
+ confirmButtonText: "Ja, slet dem!",
175
+ cancelButtonText: "Nej - jeg har fortrudt!"
176
+ closeOnConfirm: false,
177
+ closeOnCancel: false,
178
+ (confirmed) ->
179
+ if confirmed
180
+ elem= $('#media').find('.delete_link:visible')
181
+ elem.push(e) for e in $('.files').find('.delete_link:visible')
182
+ for f in elem
183
+ deletePost $(f)
184
+ f.remove()
185
+ else
186
+ elem = []
187
+ closeSweetAlert()
188
+ elem
189
+ else
190
+ for f in elem
191
+ f.remove()
192
+ @filesForUpload = new Array
193
+ else
194
+ elem = elem.currentTarget
195
+ elem = $(elem).closest('.card')[0]
196
+ img = $(elem).find('img')[0]
197
+ if img
198
+ for card in $('.files .new_upload') #$($(@dropbox_element).find('.card.new_upload'))
199
+ if card==elem
200
+ tmp = @filesForUpload.slice()
201
+ for f in tmp
202
+ if ''+f.id == ''+img.id
203
+ @filesForUpload.splice( @filesForUpload.indexOf(f),1)
204
+ elem.remove()
205
+
206
+ showProgress: (card,percent,direction) ->
207
+ card.find(".progress.fileupload-progress").show()
208
+ card.find(".progress.fileupload-progress .determinate").css('width',percent)
209
+
210
+ # prepare upload for one file
211
+ # setup eventListeners
212
+ # before, during and after upload has completed
213
+ #
214
+ ajx: (_card, formData) =>
215
+ deferred = $.Deferred()
216
+ jqxhr = $.ajax
217
+ url : '/media.js'
218
+ type : 'POST'
219
+ data : formData
220
+ processData: false
221
+ contentType: false
222
+ dataType: 'html'
223
+ beforeSend: (req) =>
224
+ _card.find('.progress').show()
225
+ xhr: () =>
226
+ xhr = new window.XMLHttpRequest()
227
+ # Upload progress
228
+ xhr.upload.addEventListener "progress", (e) =>
229
+ # console.log 'progressing with upload'
230
+ if e.lengthComputable
231
+ percent = Math.round( e.loaded * 100 / e.total) + '%'
232
+ @showProgress _card,percent,'uploaded'
233
+ # console.log( percent + ' uploaded')
234
+
235
+ # Download progress
236
+ xhr.addEventListener "progress", (e) =>
237
+ # console.log 'progressing with download'
238
+ if (e.lengthComputable)
239
+ percent = (e.loaded * 100 / e.total) + '%'
240
+ @showProgress _card,percent,'downloaded'
241
+ # console.log( percent + ' downloaded')
242
+ xhr
243
+
244
+ .done (r) =>
245
+ $.when _card.remove()
246
+ .done () =>
247
+ @uploaded_files += 1
248
+ percent = Math.round( @uploaded_files * 100 / @all_files ) + '%'
249
+ @showProgress $('.fileupload-buttonbar'), percent, 'uploaded'
250
+ $('#media').prepend r
251
+ .then () =>
252
+ console.log 'ajx resolved ---'
253
+ deferred.resolve()
254
+
255
+ .fail (e,msg) =>
256
+ swal "Fejl!", "Der opstod desværre en fejl - beskrivelsen er:\n" + msg, "error"
257
+ deferred.resolve()
258
+
259
+ deferred.promise()
260
+
261
+ #
262
+ # collect and prepare all files ready for upload
263
+ # defer xfer files
264
+ #
265
+ xfer: (formData,img,uploadAllFiles) =>
266
+ deferred = $.Deferred()
267
+ _processes = []
268
+ if uploadAllFiles
269
+ @uploaded_files = 0
270
+ @all_files = $('.files .new_upload').length
271
+ while @filesForUpload.length>0
272
+ try
273
+ f = @filesForUpload.pop()
274
+ _card = $($('#'+ f.id).closest('.card:visible'))
275
+ if _card
276
+ formData.append 'medium[medium]', f
277
+ _processes.push( @ajx(_card,formData))
278
+ catch err
279
+ console.log err
280
+
281
+ $.when.apply $, _processes
282
+ .done () ->
283
+ console.log 'xfer resolved ---'
284
+ deferred.resolve()
285
+
286
+ else
287
+ @all_files = 1
288
+ console.log 'filesForUpload: ' + @filesForUpload
289
+ tmp = @filesForUpload.slice()
290
+ console.log '# of files: ' + tmp.length
291
+ console.log 'tmp: ' + tmp
292
+ while tmp.length > 0
293
+ f = tmp.pop()
294
+ console.log 'f: ' + f
295
+ try
296
+ if (''+f.id)==(''+img.id)
297
+ console.log 'hit: ' + f
298
+ @filesForUpload.splice( @filesForUpload.indexOf(f),1)
299
+ console.log '# of filesForUpload: ' + @filesForUpload.length
300
+ _card = $($('#'+ f.id).closest('.card'))
301
+ formData.append 'medium[medium]', f
302
+ _processes.push( @ajx(_card,formData))
303
+ catch err
304
+ console.log f
305
+ console.log img
306
+
307
+ $.when.apply $, _processes
308
+ .done () ->
309
+ deferred.resolve()
310
+
311
+ deferred.promise()
312
+
313
+
314
+ uploadFile: (elem='all') =>
315
+ formData = new FormData()
316
+ uploadAllFiles = elem == 'all'
317
+ if uploadAllFiles
318
+ elem = $('.files .new_upload')
319
+ return if $('.files .new_upload').length < 1
320
+ else
321
+ $elem = $(elem.currentTarget)
322
+ img = $elem.closest('.card').find('img')[0]
323
+ _id = $elem.closest(@dropbox_element).find('#medium_imageable_id').val()
324
+ _type = $elem.closest(@dropbox_element).find('#medium_imageable_type').val()
325
+ if img
326
+ formData.append 'medium[imageable_id]', _id
327
+ formData.append 'medium[imageable_type]', _type
328
+ $.when @xfer(formData,img,uploadAllFiles)
329
+ .done () =>
330
+ # console.log 'uploadFile will move on --'
331
+ $('.fileupload-buttonbar .fileupload-progress').hide()
332
+
333
+
334
+ updateSelectedFiles: (elem) =>
335
+ # nBytes = 0
336
+ elem = e.currentTarget
337
+ oFiles = $(elem)[0].files
338
+ console.log oFiles
339
+ # nFiles = oFiles.length
340
+ # nBytes += v.size for v in oFiles
341
+ #
342
+ # sOutput = nBytes + " bytes"
343
+ # nApprox = nBytes / 1024
344
+ # aMultiples = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
345
+ # nMultiple = 0
346
+ # nApprox /= 1024
347
+ # nMultiple = while nApprox > 1.0
348
+ # console.log nApprox + ' ' + nMultiple
349
+ # nApprox /= 1024
350
+ # nMultiple += 1
351
+ #
352
+ # sOutput = nApprox.toFixed(3) + " " + aMultiples[nMultiple] + " (" + nBytes + " bytes)"
353
+ #
354
+ # $(".fileNum").html nFiles + ' filer'
355
+ # $(".fileSize").html sOutput
356
+ # $('.files').html tmpl('uploads',data)
357
+ @handleFiles(oFiles)
358
+
359
+ lazyGetTemplate: (name) =>
360
+ deferred = $.Deferred()
361
+
362
+ if $.templates[name]
363
+ deferred.resolve()
364
+ else
365
+ $.get "/assets/templates/" + name + ".html"
366
+ .fail (r,msg) ->
367
+ if r.status==200
368
+ $.templates(name,r.responseText)
369
+ deferred.resolve()
370
+ else
371
+ swal 'Fejl', 'Der opstod en fejl! Ordlyden er:\n ' + msg + '\n' + r.status + ': ' + r.statusText + '\n kontakt ALCO på tlf: 9791 1470!', "error"
372
+ .done (r) ->
373
+ $.templates(name,r)
374
+ if $.templates[name]
375
+ deferred.resolve()
376
+ else
377
+ swal "Fejl", "Der opstod en fejl under hentningen af en skabelon til billed visning! Kontakt ALCO på tlf: 9791 1470", "error"
378
+ deferred.reject()
379
+
380
+ return deferred.promise()
381
+
382
+
383
+ handleFiles: (f) =>
384
+ $.when( @lazyGetTemplate('selected_files'))
385
+ .done () =>
386
+ for file in f
387
+ imageType = /^image\//
388
+
389
+ # if (!imageType.test(file.type))
390
+ # continue
391
+
392
+ file.id = Date.now()
393
+ $.when $('.files').prepend $.templates.selected_files.render(file)
394
+ .done () ->
395
+ img = $('#'+file.id)
396
+ img.file = file
397
+ if (!imageType.test(file.type))
398
+ src = switch
399
+ when /application.*pdf/.exec file.type then '/assets/2.jpg'
400
+ when /mp4/.exec file.type then '/assets/2.jpg'
401
+ when /applica.*sql/.exec file.type then '/assets/2.jpg'
402
+ when /applica.*zip/.exec file.type then '/assets/2.jpg'
403
+ when /applica.*csv/.exec file.type then '/assets/2.jpg'
404
+ when /applica.*sheet/.exec file.type then '/assets/2.jpg'
405
+ when /applica.*document/.exec file.type then '/assets/2.jpg'
406
+ else "#{ image_url('logo.png') }"
407
+ console.log 'src: ' + src
408
+ $(img).attr('src', src )
409
+ else
410
+ reader = new FileReader()
411
+ reader.onload = (e) ->
412
+ $(img).attr('src', e.target.result)
413
+ reader.readAsDataURL(file)
414
+
415
+ @filesForUpload.push(file)
416
+
417
+ freshImageSrcPattern = /// ^
418
+ data
419
+ .* ///i
420
+
421
+ cropFile: (e) =>
422
+ @preventDefaultClick(e)
423
+ $elem = $(e.currentTarget)
424
+ img = $elem.closest('.card').find('img')[0]
425
+ if $(img).attr('src').match freshImageSrcPattern
426
+ $('#medium_medium_cropbox').attr('src',$(img).attr('src'))
427
+ $modal = $('#modal1')
428
+ $modal.openModal
429
+ new CarrierWaveCropper()
430
+ else
431
+ # _edit = "/crop"
432
+ # _id = "0"
433
+ # url = '/media/' + _id + _edit + '.js'
434
+ jqxhr = $.ajax
435
+ url: $(elem).attr('href') + '/edit.js',
436
+ type: "get",
437
+ data:
438
+ id: _id,
439
+ dataType: 'html'
440
+ .fail (r) ->
441
+ console.log r
442
+ .done (r) ->
443
+ $modal = $('#modal1')
444
+ $('.modal-content').html(r)
445
+ $modal.openModal
446
+ ready: () ->
447
+ $('.close-modal').on 'click', (e) ->
448
+ e.preventDefault()
449
+ $.ajax
450
+ url: '/media/' + _id + '/update_crop.js'
451
+ method: 'POST'
452
+ data:
453
+ id: _id,
454
+ medium:
455
+ medium_crop_x: $('#medium_medium_crop_x').val(),
456
+ medium_crop_y: $('#medium_medium_crop_y').val(),
457
+ medium_crop_w: $('#medium_medium_crop_w').val(),
458
+ medium_crop_h: $('#medium_medium_crop_h').val()
459
+ .fail (r) ->
460
+ console.log r
461
+ .done (r) ->
462
+ $modal.closeModal()
463
+
464
+ new CarrierWaveCropper()
465
+
466
+
467
+
468
+
469
+ @Medium = MediumClass