45366ffdd15f1b29e77e997acc4823d8d9053b58
[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 div = $("<div style='border: 1px solid white'/>");
33                 div.append(img);
34                 div.append(data[i]['name'])
35                 img.attr("src", data[i]['thumbnail_url']);
36                 img.attr("title", data[i]['name']);
37                 div.attr("data-output", 'file://' + data[i]['name']);
38                 div.on('click', function() {
39                     $("div", body).attr('style', 'border: 1px solid white');
40                     $("div", body).removeClass('active');
41                     $(this).attr('style', 'border: 1px solid black');
42                     $(this).addClass('active');
43                 });
44                 body.append(div);
45             }
46             var editlink = $("<a target='_blank'>" + gettext("Manage attachments") + "</a>");
47             editlink.attr('href', config.documentGalleryUrl);
48             body.append(editlink);
49         },
50     });
51 };
52
53
54
55     return {
56         select: function(callback, params) {
57             return new attachmentLibrary(callback, params);
58         }
59     };
60
61 });