|
1 | 1 | $(function() {
|
| 2 | + |
| 3 | + var capabilities = false; |
| 4 | + |
2 | 5 | if ($('form.print_box').length > 0) {
|
3 | 6 | var server = $('form.print_box').attr('action').replace(/\/?$/, '/');
|
4 | 7 |
|
5 | 8 | $.getJSON(server + "print/apps.json", function (data) {
|
6 | 9 | $.each(data, function(key, value) {
|
7 | 10 | // "default" profile does not really work for some reason
|
8 |
| - if (value != "default") { |
9 |
| - var option = $("<option></option>").attr("value",value).text(value); |
10 |
| - $('form.print_box select').append(option); |
| 11 | + if (value != "default" && value.split("_")[0] != "DEMO") { |
| 12 | + var option = $("<option></option>").attr("value",value).text(value.split("_")[1]); |
| 13 | + $('form.print_box select[name=template]').append(option); |
11 | 14 | }
|
12 | 15 | });
|
| 16 | + $('form.print_box select[name=template]').change(); |
13 | 17 | });
|
14 | 18 | }
|
15 | 19 |
|
16 |
| - $(document).on('click', '.print_box input[type=button]', function (e) { |
| 20 | + var apikey = ""; |
| 21 | + $.get('/my/api_key').done(function(data){ |
| 22 | + apikey = $('#content > div.box > pre', $(data)).first().text(); |
| 23 | + }); |
17 | 24 |
|
18 |
| - var appId = $('form.print_box select').val(); |
| 25 | + $('form.print_box select').on('change', function (e) { |
| 26 | + $.ajax({ |
| 27 | + type: 'GET', |
| 28 | + url: server + "print/" + $('form.print_box select').val() + "/capabilities.json", |
| 29 | + dataType: 'json', |
| 30 | + success: function (data) { |
| 31 | + |
| 32 | + $.each(data.formats, function(key, value) { |
| 33 | + var option = $("<option></option>").attr("value",value).text(value); |
| 34 | + $('form.print_box select[name=format]').append(option); |
| 35 | + $('form.print_box select[name=format]').val("pdf"); |
| 36 | + }); |
| 37 | + |
| 38 | + $.each(data.layouts, function(key, value) { |
| 39 | + var option = $("<option></option>").attr("value",key).text(value.name); |
| 40 | + $('form.print_box select[name=layout]').append(option); |
| 41 | + }); |
19 | 42 |
|
| 43 | + capabilities = data; |
| 44 | + } |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + $('form.print_box input[type=button]').on('click', function (e) { |
| 49 | + var appId = $('form.print_box select').val(); |
20 | 50 | $.ajax({
|
21 | 51 | type: 'GET',
|
22 | 52 | url: server + "print/" + appId + "/exampleRequest.json",
|
23 | 53 | dataType: 'json',
|
24 | 54 | success: function (data) {
|
25 |
| - requestPrint(appId, JSON.parse(data.requestData)); |
| 55 | + requestPrint(appId, capabilities, JSON.parse(data.requestData)); |
26 | 56 | }
|
27 | 57 | });
|
28 |
| - |
29 |
| - return false; |
30 | 58 | });
|
31 | 59 |
|
32 |
| - var requestPrint = function (id, requestData) { |
| 60 | + var requestPrint = function (id, config, requestData) { |
33 | 61 |
|
34 | 62 | // Defaults
|
35 |
| - var format = "pdf"; |
36 |
| - var layout = 0; |
| 63 | + var format = $('form.print_box select[name=format]').val(); |
| 64 | + var layout = $('form.print_box select[name=layout]').val(); |
37 | 65 |
|
38 |
| - var paramObj = {}; |
39 |
| - $.each($('#issue-form').serializeArray(), function(_, kv) { |
40 |
| - paramObj[kv.name] = kv.value; |
41 |
| - }); |
| 66 | + // var paramObj = {}; |
| 67 | + // $.each($('#issue-form').serializeArray(), function(_, kv) { |
| 68 | + // paramObj[kv.name] = kv.value; |
| 69 | + // }); |
| 70 | + |
| 71 | + $.ajax({ |
| 72 | + type: 'GET', |
| 73 | + url: $('#issue-form').attr("action") + ".json", |
| 74 | + headers: {'X-Redmine-API-Key': apikey}, |
| 75 | + data: { |
| 76 | + include: "attachments,journals" |
| 77 | + }, |
| 78 | + dataType: 'json', |
| 79 | + success: function (data) { |
42 | 80 |
|
43 |
| - // Apply geo data if exists |
44 |
| - if (requestData.attributes.map.layers[0].geoJson && paramObj["issue[geojson]"]) { |
45 |
| - var feature = (new ol.format.GeoJSON()).readFeature( |
46 |
| - JSON.parse(paramObj["issue[geojson]"]) |
47 |
| - ); |
48 |
| - feature.set('subject', paramObj["issue[subject]"]); |
49 |
| - feature.getGeometry().transform('EPSG:4326','EPSG:3857'); |
| 81 | + // Handle each available attribute |
| 82 | + $.each(config.layouts[layout].attributes, function(key, obj) { |
50 | 83 |
|
51 |
| - requestData.attributes.map.layers[0].geoJson = JSON.parse((new ol.format.GeoJSON()).writeFeatures([feature])); |
52 |
| - requestData.attributes.map.center = ol.extent.getCenter(feature.getGeometry().getExtent()); |
53 |
| - } |
| 84 | + // Apply geo data if exists |
| 85 | + if (obj.name === "map" && data.issue.geojson) { |
| 86 | + if(requestData.attributes.map.layers[0].geoJson) { |
| 87 | + var feature = (new ol.format.GeoJSON()).readFeature(data.issue.geojson); |
| 88 | + feature.set('foo','bar'); |
| 89 | + feature.getGeometry().transform('EPSG:4326','EPSG:3857'); |
54 | 90 |
|
55 |
| - // Apply template variables |
56 |
| - requestData.attributes.title = paramObj["issue[subject]"]; |
| 91 | + requestData.attributes.map.layers[0].geoJson = JSON.parse((new ol.format.GeoJSON()).writeFeatures([feature])); |
| 92 | + requestData.attributes.map.center = ol.extent.getCenter(feature.getGeometry().getExtent()); |
| 93 | + } |
| 94 | + return true; |
| 95 | + } |
57 | 96 |
|
58 |
| - console.log(requestData); |
59 |
| - var startTime = new Date().getTime(); |
| 97 | + switch (obj.name) { |
| 98 | + case 'scalebar': |
| 99 | + // currently nothing to do |
| 100 | + break; |
60 | 101 |
|
61 |
| - $.ajax({ |
62 |
| - type: 'POST', |
63 |
| - url: server + "print/" + id + "/report." + format, |
64 |
| - data: { |
65 |
| - spec: JSON.stringify(requestData) |
66 |
| - }, |
67 |
| - success: function (response) { |
68 |
| - downloadWhenReady(startTime, response.ref); |
69 |
| - }, |
70 |
| - error: function (data) { |
71 |
| - console.log(data); |
| 102 | + case 'freetext': |
| 103 | + requestData.attributes[obj.name] = $('form.print_box textarea').val(); |
| 104 | + break; |
| 105 | + |
| 106 | + case 'assigned_to': |
| 107 | + case 'author': |
| 108 | + case 'priority': |
| 109 | + case 'project': |
| 110 | + case 'status': |
| 111 | + case 'tracker': |
| 112 | + requestData.attributes[obj.name] = data.issue[obj.name].name; |
| 113 | + break; |
| 114 | + |
| 115 | + case 'created_on': |
| 116 | + case 'updated_on': |
| 117 | + case 'due_date': |
| 118 | + case 'start_date': |
| 119 | + requestData.attributes[obj.name] = moment(data.issue[obj.name]).format('YYYY-MM-DD'); |
| 120 | + break; |
| 121 | + |
| 122 | + case 'description': |
| 123 | + case 'done_ratio': |
| 124 | + case 'estimated_hours': |
| 125 | + case 'id': |
| 126 | + case 'subject': |
| 127 | + case 'total_estimated_hours': |
| 128 | + requestData.attributes[obj.name] = data.issue[obj.name]; |
| 129 | + break; |
| 130 | + |
| 131 | + default: |
| 132 | + requestData.attributes[obj.name] = obj.default; |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + if (data.issue.custom_fields) { |
| 137 | + $.each(data.issue.custom_fields, function (key,field) { |
| 138 | + if (obj.name === field.name) { |
| 139 | + requestData.attributes[obj.name] = field.value; |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + console.log(requestData); |
| 146 | + var startTime = new Date().getTime(); |
| 147 | + |
| 148 | + $.ajax({ |
| 149 | + type: 'POST', |
| 150 | + url: server + "print/" + id + "/report." + format, |
| 151 | + data: { |
| 152 | + spec: JSON.stringify(requestData) |
| 153 | + }, |
| 154 | + success: function (response) { |
| 155 | + downloadWhenReady(startTime, response.ref); |
| 156 | + }, |
| 157 | + error: function (data) { |
| 158 | + console.log(data); |
| 159 | + } |
| 160 | + }); |
72 | 161 | }
|
73 | 162 | });
|
74 | 163 | };
|
|
0 commit comments