This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
forked from davedf/mindmup
/
tabular-export.js
120 lines (120 loc) · 4.07 KB
/
tabular-export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*global MM, MAPJS, _, $, jQuery*/
MM.exportIdeas = function (contentAggregate, exporter) {
'use strict';
var traverse = function (iterator, idea, level) {
level = level || 0;
iterator(idea, level);
_.each(idea.sortedSubIdeas(), function (subIdea) {
traverse(iterator, subIdea, level + 1);
});
};
if (exporter.begin) {
exporter.begin();
}
traverse(exporter.each, contentAggregate);
if (exporter.end) {
exporter.end();
}
return exporter.contents();
};
MM.TabSeparatedTextExporter = function () {
'use strict';
var contents = [];
this.contents = function () {
return contents.join('\n');
};
this.each = function (idea, level) {
contents.push(
_.map(_.range(level), function () {
return '\t';
}).join('') + idea.title.replace(/\t|\n|\r/g, ' ')
);
};
};
MM.HtmlTableExporter = function () {
'use strict';
var result;
this.begin = function () {
result = $('<table>').wrap('<div></div>'); /*parent needed for html generation*/
};
this.contents = function () {
return '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head><body>' +
$(result).parent().html() +
'</body></html>';
};
this.each = function (idea, level) {
var row = $('<tr>').appendTo(result),
cell = $('<td>').appendTo(row).text(idea.title);
if (idea.attr && idea.attr.style && idea.attr.style.background) {
cell.css('background-color', idea.attr.style.background);
cell.css('color', MAPJS.contrastForeground(idea.attr.style.background));
}
if (level > 0) {
$('<td>').prependTo(row).html(' ').attr('colspan', level);
}
};
};
MM.exportToHtmlDocument = function (idea) {
'use strict';
var deferred = jQuery.Deferred(),
createContent = function () {
var result = $('<div>'), /*parent needed for html generation*/
appendLinkOrText = function (element, text) {
if (MAPJS.URLHelper.containsLink(text)) {
$('<a>').attr('href', MAPJS.URLHelper.getLink(text))
.text(MAPJS.URLHelper.stripLink(text) || text)
.appendTo(element);
} else {
element.text(text);
}
},
appendAttachment = function (element, anIdea) {
var attachment = anIdea && anIdea.attr && anIdea.attr.attachment;
if (attachment && attachment.contentType === 'text/html') {
$('<div>').addClass('attachment').appendTo(element).html(attachment.content);
}
},
toList = function (ideaList) {
var list = $('<ul>');
_.each(ideaList, function (subIdea) {
var element = $('<li>').appendTo(list);
appendLinkOrText(element, subIdea.title);
appendAttachment(element, subIdea);
if (subIdea.attr && subIdea.attr.style && subIdea.attr.style.background) {
element.css('background-color', subIdea.attr.style.background);
element.css('color', MAPJS.contrastForeground(subIdea.attr.style.background));
}
if (!_.isEmpty(subIdea.ideas)) {
toList(subIdea.sortedSubIdeas()).appendTo(element);
}
});
return list;
},
heading = $('<h1>').appendTo(result);
appendLinkOrText(heading, idea.title);
appendAttachment(result, idea);
if (!_.isEmpty(idea.ideas)) {
toList(idea.sortedSubIdeas()).appendTo(result);
}
deferred.resolve('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
'<style type="text/css">' +
'body{font-family:"HelveticaNeue",Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;color:#333333;margin-left:10%;margin-right:10%;}h1{display:block;font-size:38.5px;line-height:40px;font-family:inherit;}li{line-height:20px;padding-left:10px;}ul{list-style-type:none;}div.attachment{border:1px solid black;margin:5px;padding:5px;}img.mapimage{border:1px solid black;max-height:600px;max-width:600px;}</style>' +
'</head><body>' +
$(result).html() +
'</body></html>', 'text/html');
};
createContent();
return deferred.promise();
};
MM.exportTableToText = function (table) {
'use strict';
return _.map(table, function (row) {
return _.map(row, function (cell) {
if (!cell) {
return '';
}
return cell.toString().replace(/\t|\n|\r/g, ' ');
}).join('\t');
})
.join('\n');
};