beef-has_assets 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/app/controllers/admin/assets_controller.rb +159 -0
- data/app/helpers/admin/assets_helper.rb +34 -0
- data/app/models/asset.rb +72 -0
- data/app/models/asseting.rb +5 -0
- data/app/views/admin/assets/_browser.html.erb +25 -0
- data/app/views/admin/assets/_document.html.erb +12 -0
- data/app/views/admin/assets/_file_info.html.erb +51 -0
- data/app/views/admin/assets/_form.html.erb +26 -0
- data/app/views/admin/assets/_image.html.erb +19 -0
- data/app/views/admin/assets/_list.html.erb +12 -0
- data/app/views/admin/assets/_toggle.html.erb +4 -0
- data/app/views/admin/assets/index.html.erb +10 -0
- data/app/views/admin/assets/index.js.rjs +1 -0
- data/app/views/admin/assets/rename_category.js.rjs +1 -0
- data/app/views/admin/assets/show.html.erb +36 -0
- data/app/views/admin/assets/show.js.rjs +3 -0
- data/config/routes.rb +5 -0
- data/generators/asset_migration/asset_migration_generator.rb +11 -0
- data/generators/asset_migration/templates/migration.rb +30 -0
- data/generators/assets_admin_files/asset_admin_files_generator.rb +19 -0
- data/generators/assets_admin_files/templates/public/flash/swfupload.swf +0 -0
- data/generators/assets_admin_files/templates/public/javascripts/admin/assets.js +383 -0
- data/generators/assets_admin_files/templates/public/javascripts/swfupload.js +980 -0
- data/generators/assets_admin_files/templates/public/javascripts/upload_progress.js +179 -0
- data/has_assets.gemspec +76 -0
- data/lib/custom_redcloth_tags.rb +18 -0
- data/lib/flash_sesion_cookie_middleware.rb +16 -0
- data/lib/geometry_crop.rb +51 -0
- data/lib/has_assets.rb +28 -0
- data/lib/imagescience_crop.rb +40 -0
- data/lib/swfupload.rb +32 -0
- data/rails/init.rb +19 -0
- data/test/has_assets_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +94 -0
@@ -0,0 +1,383 @@
|
|
1
|
+
var AssetBrowser = Class.create({
|
2
|
+
initialize: function(grouping, folder) {
|
3
|
+
AssetBrowser.current_grouping = grouping;
|
4
|
+
AssetBrowser.dl = $('asset-browser');
|
5
|
+
AssetBrowser.setContentNodeForm('has-assets-form');
|
6
|
+
AssetBrowser.dts = AssetBrowser.dl.select('dt');
|
7
|
+
AssetBrowser.open_folder = folder;
|
8
|
+
|
9
|
+
AssetBrowser.dts.each(function(dt) {
|
10
|
+
dt.dd = dt.next('dd');
|
11
|
+
if (dt.dd) {
|
12
|
+
dt.dd.hide();
|
13
|
+
dt.onclick = function() {
|
14
|
+
AssetBrowser.closeInfo();
|
15
|
+
this.dd.toggle();
|
16
|
+
if (AssetBrowser.current_content != this.dd) {
|
17
|
+
if (AssetBrowser.current_content != null && AssetBrowser.current_content.visible()) {
|
18
|
+
AssetBrowser.current_content.toggle();
|
19
|
+
}
|
20
|
+
AssetBrowser.current_content = this.dd;
|
21
|
+
}
|
22
|
+
};
|
23
|
+
new Renameable(dt, 0.7);
|
24
|
+
}
|
25
|
+
});
|
26
|
+
AssetBrowser.openContent();
|
27
|
+
}
|
28
|
+
});
|
29
|
+
|
30
|
+
Object.extend(AssetBrowser, {
|
31
|
+
|
32
|
+
openContent: function() {
|
33
|
+
type = this.open_folder;
|
34
|
+
if (type) {
|
35
|
+
dt = this.dl.down('#'+type);
|
36
|
+
if (dt) { dt.onclick(); }
|
37
|
+
}
|
38
|
+
},
|
39
|
+
|
40
|
+
openInfo: function(id, anchor) {
|
41
|
+
|
42
|
+
this.closeInfo();
|
43
|
+
list = $(anchor).up('ul');
|
44
|
+
list.hide();
|
45
|
+
list.insert({after: '<div id="asset-info"></div>'});
|
46
|
+
this.current_list = list;
|
47
|
+
this.current_info = list.next();
|
48
|
+
|
49
|
+
new Ajax.Request('/admin/assets/' + id, { method: 'get',
|
50
|
+
asynchronous:true,
|
51
|
+
evalScripts:true } );
|
52
|
+
},
|
53
|
+
|
54
|
+
closeInfo: function() {
|
55
|
+
if (this.current_list) {
|
56
|
+
this.current_list.show();
|
57
|
+
this.current_info.remove();
|
58
|
+
}
|
59
|
+
this.current_list = null;
|
60
|
+
this.current_info = null;
|
61
|
+
},
|
62
|
+
|
63
|
+
closeThumbnail: function() {
|
64
|
+
key = Object.keys(SWFUpload.instances).last();
|
65
|
+
SWFUpload.instances[key].destroy();
|
66
|
+
this.closeInfo();
|
67
|
+
},
|
68
|
+
|
69
|
+
assetAction: function(asset) {
|
70
|
+
$('asset-upload-form').reset();
|
71
|
+
if (this.contentNodeForm) {
|
72
|
+
this.addAssetToContentNode(asset);
|
73
|
+
} else {
|
74
|
+
this.destroyAsset(asset);
|
75
|
+
}
|
76
|
+
},
|
77
|
+
|
78
|
+
destroyAsset: function(asset) {
|
79
|
+
if (confirm('Are you sure you wish to delete the asset \'' + asset.filename + ' \'?')) {
|
80
|
+
new Ajax.Request('/admin/assets/'+ asset.id, { method: 'delete',
|
81
|
+
asynchronous:true,
|
82
|
+
evalScripts:true,
|
83
|
+
parameters: { content_type: asset.content_type, authenticity_token: AJ.authenticity_token() } } );
|
84
|
+
|
85
|
+
asset_li = $('browser-asset-' + asset.id);
|
86
|
+
asset_list = asset_li.up('ul');
|
87
|
+
asset_li.remove();
|
88
|
+
if (asset_list.empty()) {
|
89
|
+
asset_list.up('dd').previous().remove();
|
90
|
+
asset_list.up('dd').remove();
|
91
|
+
}
|
92
|
+
if (this.contentNodeForm) {
|
93
|
+
this.removeAssetFromContentNode(asset.id);
|
94
|
+
}
|
95
|
+
|
96
|
+
}
|
97
|
+
},
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// Fire once a asset has been uploaded
|
102
|
+
uploadedAsset: function(asset) {
|
103
|
+
type = Object.keys(asset).first();
|
104
|
+
$('asset-upload-form').reset();
|
105
|
+
if (this.contentNodeForm) {
|
106
|
+
this.addAssetToContentNode(asset[type]);
|
107
|
+
}
|
108
|
+
if (asset[type]) {
|
109
|
+
switch (this.current_grouping){
|
110
|
+
case 'by_content_type':
|
111
|
+
this.open_folder = asset[type].content_type.replace(/\W+/,'-');
|
112
|
+
break;
|
113
|
+
case 'by_category':
|
114
|
+
this.open_folder = asset[type].category;
|
115
|
+
break;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
},
|
119
|
+
|
120
|
+
reload: function(grouping) {
|
121
|
+
if (grouping) {
|
122
|
+
this.current_grouping = grouping;
|
123
|
+
}
|
124
|
+
new Ajax.Request('/admin/assets/'+this.current_grouping, { method: 'get',
|
125
|
+
asynchronous:true,
|
126
|
+
evalScripts:true,
|
127
|
+
parameters: { for_content: this.contentNodeForm != null,
|
128
|
+
folder: this.open_folder },
|
129
|
+
onSuccess: init_asset_category_auto_complete } );
|
130
|
+
|
131
|
+
},
|
132
|
+
|
133
|
+
setContentNodeForm: function(id) {
|
134
|
+
this.contentNodeForm = $(id);
|
135
|
+
if (this.contentNodeForm) {
|
136
|
+
this.contentNodeForm.model_name = this.contentNodeForm.className.split('_').slice(1,this.contentNodeForm.className.split('_').length).join('_');
|
137
|
+
this.setUpAssetList();
|
138
|
+
this.contentNodeForm.onsubmit = function() {
|
139
|
+
assets = $$('#asset-list li');
|
140
|
+
if (assets.size() == 0) {
|
141
|
+
$(this).insert('<input type="hidden" name="' + this.model_name + '[asset_ids]" value="" />');
|
142
|
+
} else {
|
143
|
+
assets.each(function(li) {
|
144
|
+
asset_id = li.id.split('-').last();
|
145
|
+
this.insert('<input type="hidden" name="' + this.model_name + '[asset_ids][]" id="asset_id-' + asset_id + '" value="' + asset_id + '" />');
|
146
|
+
}, this);
|
147
|
+
}
|
148
|
+
return true;
|
149
|
+
};
|
150
|
+
}
|
151
|
+
},
|
152
|
+
|
153
|
+
setUpAssetList: function() {
|
154
|
+
this.assetList = $('asset-list');
|
155
|
+
if (this.assetList) {
|
156
|
+
Sortable.create(this.assetList);
|
157
|
+
}
|
158
|
+
this.contentNodeForm.assetList = this.assetList;
|
159
|
+
},
|
160
|
+
|
161
|
+
addAssetToContentNode: function(asset) {
|
162
|
+
if (!this.assetList) {
|
163
|
+
$('attach-asset-list').insert({top:'<h2 class="drop-down open">' + this.contentNodeForm.model_name + ' Files</h2><ul class="asset-list" id="asset-list"></ul>'});
|
164
|
+
this.setUpAssetList();
|
165
|
+
}
|
166
|
+
|
167
|
+
this.assetList.show();
|
168
|
+
if (!$('asset-' + asset.id)) {
|
169
|
+
|
170
|
+
if(/image/.test(asset.content_type)){
|
171
|
+
li = new Element('li', { 'class': asset.content_type.replace('/','-'), id: 'asset-' + asset.id }).update('<img alt=\"' + asset.filename + '\" src=\"' + asset.sizes.find(function(size){ return size[0] == 'square'; })[1] + '\" />');
|
172
|
+
} else {
|
173
|
+
li = new Element('li', { 'class': asset.content_type.replace('/','-'), id: 'asset-' + asset.id }).update(asset.filename + ' ' + asset.description);
|
174
|
+
}
|
175
|
+
|
176
|
+
filename = new Element('h4').update(asset.filename);
|
177
|
+
div = new Element('div');
|
178
|
+
h4 = new Element('h4').update("Insert Image");
|
179
|
+
view_info = new Element('a').update('View Info');
|
180
|
+
|
181
|
+
view_info.onclick = function() {
|
182
|
+
AssetBrowser.openInfo(asset.id, this);
|
183
|
+
return false;
|
184
|
+
};
|
185
|
+
|
186
|
+
insert_image_large = new Element('a').update('Large | ');
|
187
|
+
insert_image_medium = new Element('a').update('Medium | ');
|
188
|
+
insert_image_small = new Element('a').update('Small');
|
189
|
+
|
190
|
+
insert_image_large.onclick = function() {
|
191
|
+
addAsset(asset.id, 'large' );
|
192
|
+
return false;
|
193
|
+
};
|
194
|
+
insert_image_medium.onclick = function() {
|
195
|
+
addAsset(asset.id, 'medium' );
|
196
|
+
return false;
|
197
|
+
};
|
198
|
+
insert_image_small.onclick = function() {
|
199
|
+
addAsset(asset.id, 'thumb' );
|
200
|
+
return false;
|
201
|
+
};
|
202
|
+
insert_document = new Element('a').update(' | Insert');
|
203
|
+
|
204
|
+
var theForm = this.contentNodeForm;
|
205
|
+
|
206
|
+
deleter = new Element('a').update('Detach');
|
207
|
+
deleter.onclick = function() {
|
208
|
+
AssetBrowser.removeAssetFromContentNode(asset.id);
|
209
|
+
return false;
|
210
|
+
};
|
211
|
+
|
212
|
+
div.insert(filename);
|
213
|
+
div.insert(view_info);
|
214
|
+
div.insert('<br/>');
|
215
|
+
div.insert(deleter);
|
216
|
+
|
217
|
+
li.insert(div);
|
218
|
+
|
219
|
+
if (/image/.test(asset.content_type)) {
|
220
|
+
//setup for images
|
221
|
+
div.insert(h4);
|
222
|
+
div.insert(insert_image_large);
|
223
|
+
div.insert(insert_image_medium);
|
224
|
+
div.insert(insert_image_small);
|
225
|
+
|
226
|
+
} else {
|
227
|
+
//setup for documents
|
228
|
+
insert_document.onclick = function() {
|
229
|
+
addAsset(asset.id);
|
230
|
+
return false;
|
231
|
+
};
|
232
|
+
div.insert(insert_document);
|
233
|
+
}
|
234
|
+
|
235
|
+
this.assetList.insert(li);
|
236
|
+
this.setUpAssetList();
|
237
|
+
}
|
238
|
+
},
|
239
|
+
|
240
|
+
removeAssetFromContentNode: function(asset_id) {
|
241
|
+
$('asset-' + asset_id).remove();
|
242
|
+
},
|
243
|
+
|
244
|
+
rootUrl: function(url){
|
245
|
+
if(!url)
|
246
|
+
url = window.location.href;
|
247
|
+
|
248
|
+
var matches = url.match(/^(?:http:\/\/)?[^\/]+/);
|
249
|
+
if(matches.length <1){
|
250
|
+
return url;
|
251
|
+
}else{
|
252
|
+
return matches[0];
|
253
|
+
}
|
254
|
+
},
|
255
|
+
|
256
|
+
// For swfu only
|
257
|
+
uploadSuccess: function(fileObj, server_data) {
|
258
|
+
AssetBrowser.uploadedAsset(server_data.evalJSON());
|
259
|
+
},
|
260
|
+
|
261
|
+
uploadComplete: function(fileObj) {
|
262
|
+
/* I want the next upload to continue automatically so I'll call startUpload here */
|
263
|
+
if (this.getStats().files_queued > 0) {
|
264
|
+
this.startUpload();
|
265
|
+
} else {
|
266
|
+
UploadProgress.finish();
|
267
|
+
AssetBrowser.reload();
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
});
|
272
|
+
|
273
|
+
// Load SWFU
|
274
|
+
var swfu;
|
275
|
+
Event.observe(window, 'load', function() {
|
276
|
+
if ($('asset-browser')) {
|
277
|
+
form = $('asset-upload-form');
|
278
|
+
form.insert({after: '<div id="flash-button"></div>'});
|
279
|
+
js_path = form.action.split('?').join('.js?');
|
280
|
+
swfu = new SWFUpload({
|
281
|
+
// Create the custom swfupload_photos_path in the routes.rb file
|
282
|
+
// Session name must match with environment.rb
|
283
|
+
upload_url : js_path,
|
284
|
+
flash_url : '/flash/swfupload.swf',
|
285
|
+
file_post_name: 'asset[uploaded_data]',
|
286
|
+
|
287
|
+
file_size_limit : '5 MB',
|
288
|
+
file_upload_limit : 0,
|
289
|
+
|
290
|
+
file_queue_error_handler : UploadHandler.fileQueueError,
|
291
|
+
file_dialog_complete_handler : UploadHandler.fileDialogComplete,
|
292
|
+
upload_progress_handler : UploadHandler.uploadProgress,
|
293
|
+
upload_start_handler: UploadHandler.uploadStart,
|
294
|
+
upload_error_handler : UploadHandler.uploadError,
|
295
|
+
upload_success_handler : AssetBrowser.uploadSuccess,
|
296
|
+
upload_complete_handler : AssetBrowser.uploadComplete,
|
297
|
+
custom_settings : {
|
298
|
+
upload_form : 'asset-upload-form'
|
299
|
+
},
|
300
|
+
|
301
|
+
button_placeholder_id: 'flash-button',
|
302
|
+
button_height: 19,
|
303
|
+
button_width: 157,
|
304
|
+
button_image_url: '/images/admin/file-uploads.png',
|
305
|
+
button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
|
306
|
+
button_disable : false,
|
307
|
+
button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
|
308
|
+
|
309
|
+
debug: false
|
310
|
+
|
311
|
+
});
|
312
|
+
if (swfu) {
|
313
|
+
form.onsubmit = function(){return false;};
|
314
|
+
$('upload-elements').remove();
|
315
|
+
}
|
316
|
+
}
|
317
|
+
|
318
|
+
});
|
319
|
+
|
320
|
+
init_asset_category_auto_complete = function() {
|
321
|
+
asset_category_field = $('asset_category');
|
322
|
+
if (asset_category_field) {
|
323
|
+
new Ajax.Request('/admin/assets/categories', {
|
324
|
+
method: 'get',
|
325
|
+
onSuccess : function(response) {
|
326
|
+
new Autocompleter.Local('asset_category', 'asset_category_complete', response.responseJSON );
|
327
|
+
}
|
328
|
+
});
|
329
|
+
}
|
330
|
+
};
|
331
|
+
document.observe('dom:loaded', init_asset_category_auto_complete);
|
332
|
+
|
333
|
+
init_edit_asset_form = function() {
|
334
|
+
$$('form.edit_asset').first().onsubmit = function(e) {
|
335
|
+
this.request({
|
336
|
+
onComplete: function() {AssetBrowser.reload();}
|
337
|
+
});
|
338
|
+
|
339
|
+
return false;
|
340
|
+
};
|
341
|
+
};
|
342
|
+
|
343
|
+
var Holdable = Class.create({
|
344
|
+
initialize: function(elem, seconds, firer) {
|
345
|
+
this.elem = elem;
|
346
|
+
this.seconds = seconds;
|
347
|
+
if (firer) {
|
348
|
+
this.firer = firer;
|
349
|
+
}
|
350
|
+
|
351
|
+
this.elem.observe('mousedown', this.starter.bindAsEventListener(this));
|
352
|
+
this.elem.observe('mouseup', this.clearTimeout.bindAsEventListener(this));
|
353
|
+
},
|
354
|
+
|
355
|
+
starter: function() {
|
356
|
+
this.clear_id = this.firer.delay(this.seconds, this.elem);
|
357
|
+
},
|
358
|
+
|
359
|
+
firer: function(elem) {
|
360
|
+
alert('NO FIRER!');
|
361
|
+
},
|
362
|
+
|
363
|
+
clearTimeout: function() {
|
364
|
+
window.clearTimeout(this.clear_id);
|
365
|
+
}
|
366
|
+
});
|
367
|
+
|
368
|
+
var Renameable = Class.create(Holdable, {
|
369
|
+
firer: function(elem) {
|
370
|
+
this.text_input = new Element('input', { href: 'text', value: elem.innerHTML });
|
371
|
+
elem.update(this.text_input);
|
372
|
+
this.text_input.focus();
|
373
|
+
this.text_input.observe('blur', function() {
|
374
|
+
this.replace(this.value);
|
375
|
+
});
|
376
|
+
this.text_input.observe('change', function() {
|
377
|
+
asset_id = elem.id.split(':').last();
|
378
|
+
new Ajax.Request('/admin/assets/' + asset_id + '/rename_category' , {
|
379
|
+
parameters: { name: this.value }
|
380
|
+
});
|
381
|
+
});
|
382
|
+
}
|
383
|
+
});
|