small refactor + fix brackets
[fnpeditor.git] / src / editor / views / attachments / attachments.js
1 define(function(require) {
2
3     'use strict';
4
5     var $ = require('libs/jquery'),
6         Dialog = require('views/dialog/dialog');
7
8
9
10 // Move it somewhere else.
11 var attachmentLibrary = function(callback, params) {
12     var dialog = Dialog.create({
13         title: gettext('Attachment library'),
14         executeButtonText: gettext('Select'),
15         cancelButtonText: gettext('Cancel'),
16     });
17
18     var output = "";
19
20     dialog.on('execute', function(event) {
21         callback($(".active", dialog.$el).attr('data-output'));
22         event.success();
23     });
24
25     dialog.show();
26     var body = $(".modal-body", dialog.$el);
27     $.ajax(config.documentGalleryUrl, {
28         dataType: 'json',
29         success: function(data, status, jqxhr) {
30             for (var i in data) {
31                 var img = $("<img style='margin-right: 1em'>");
32                 var input = $("<input type='radio' name='attachment'>");
33                 var label = $("<label/>");
34                 var div = $("<div style='border: 1px solid white'/>");
35                 label.append(input);
36                 label.append(img);
37                 label.append(data[i]['name']);
38                 img.attr("src", data[i]['thumbnail_url']);
39                 img.attr("title", data[i]['name']);
40                 input.attr('id', 'attachment' + i);
41                 label.attr('for', 'attachment' + i);
42                 div.append(label);
43                 div.attr("data-output", 'file://' + data[i]['name']);
44                 div.on('click', function() {
45                     $("div.active", body).removeClass('active');
46                     $(this).addClass('active');
47                 });
48                 body.append(div);
49             }
50             var editlink = $("<a target='_blank'>" + gettext("Manage attachments") + "</a>");
51             editlink.attr('href', config.documentGalleryUrl);
52             body.append(editlink);
53         },
54     });
55 };
56
57
58
59     return {
60         select: function(callback, params) {
61             return new attachmentLibrary(callback, params);
62         }
63     };
64
65 });