on 9df2eac help sign in
167 lines 5.1 kB history raw
1Elm.Native.Text = {};
2Elm.Native.Text.make = function(elm) {
3 elm.Native = elm.Native || {};
4 elm.Native.Text = elm.Native.Text || {};
5 if (elm.Native.Text.values) return elm.Native.Text.values;
7 var Color = Elm.Native.Color.make(elm);
8 var Element = Elm.Graphics.Element.make(elm);
9 var List = Elm.Native.List.make(elm);
10 var Utils = Elm.Native.Utils.make(elm);
11 var show = Elm.Native.Show.make(elm).show;
13 function makeSpaces(s) {
14 if (s.length == 0) { return s; }
15 var arr = s.split('');
16 if (arr[0] == ' ') { arr[0] = " " }
17 for (var i = arr.length; --i; ) {
18 if (arr[i][0] == ' ' && arr[i-1] == ' ') {
19 arr[i-1] = arr[i-1] + arr[i];
20 arr[i] = '';
21 }
22 }
23 for (var i = arr.length; i--; ) {
24 if (arr[i].length > 1 && arr[i][0] == ' ') {
25 var spaces = arr[i].split('');
26 for (var j = spaces.length - 2; j >= 0; j -= 2) {
27 spaces[j] = ' ';
28 }
29 arr[i] = spaces.join('');
30 }
31 }
32 arr = arr.join('');
33 if (arr[arr.length-1] === " ") {
34 return arr.slice(0,-1) + ' ';
35 }
36 return arr;
37 }
39 function properEscape(str) {
40 if (str.length == 0) return str;
41 str = str //.replace(/&/g, "&")
42 .replace(/"/g, '"')
43 .replace(/'/g, "'")
44 .replace(/</g, "&#60;")
45 .replace(/>/g, "&#62;")
46 .replace(/\n/g, "<br/>");
47 var arr = str.split('<br/>');
48 for (var i = arr.length; i--; ) {
49 arr[i] = makeSpaces(arr[i]);
50 }
51 return arr.join('<br/>');
52 }
54 function toText(str) { return Utils.txt(properEscape(str)); }
56 // conversions from Elm values to CSS
57 function toTypefaces(list) {
58 var typefaces = List.toArray(list);
59 for (var i = typefaces.length; i--; ) {
60 var typeface = typefaces[i];
61 if (typeface.contains(' ')) {
62 typefaces[i] = "'" + typeface + "'";
63 }
64 }
65 return typefaces.join(',');
66 }
67 function toLine(line) {
68 var ctor = line.ctor;
69 return ctor === 'Under' ? 'underline' :
70 ctor === 'Over' ? 'overline' : 'line-through';
71 }
73 // setting styles of Text
74 function style(style, text) {
75 var newText = '<span style="color:' + toCss(style.color) + ';'
76 if (style.typeface.ctor !== '[]') {
77 newText += 'font-family:' + toTypefaces(style.typeface) + ';'
78 }
79 if (style.height.ctor !== "Nothing") {
80 newText += 'font-size:' + style.height._0 + 'px;';
81 }
82 if (style.bold) {
83 newText += 'font-weight:bold;';
84 }
85 if (style.italic) {
86 newText += 'font-style:italic;';
87 }
88 if (style.line.ctor !== 'Nothing') {
89 newText += 'text-decoration:' + toLine(style.line._0) + ';';
90 }
91 newText += '">' + Utils.makeText(text) + '</span>'
92 return Utils.txt(newText);
93 }
94 function height(px, text) {
95 return { style: 'font-size:' + px + 'px;', text:text }
96 }
97 function typeface(names, text) {
98 return { style: 'font-family:' + toTypefaces(names) + ';', text:text }
99 }
100 function monospace(text) {
101 return { style: 'font-family:monospace;', text:text }
102 }
103 function italic(text) {
104 return { style: 'font-style:italic;', text:text }
105 }
106 function bold(text) {
107 return { style: 'font-weight:bold;', text:text }
108 }
109 function link(href, text) {
110 return { href: toText(href), text:text };
111 }
112 function line(line, text) {
113 return { style: 'text-decoration:' + toLine(line) + ';', text:text };
114 }
116 function color(color, text) {
117 return { style: 'color:' + toCss(color) + ';', text:text };
118 }
120 function block(align) {
121 return function(text) {
122 var raw = {
123 ctor :'RawHtml',
124 html : Utils.makeText(text),
125 align: align,
126 guid : null,
127 args : [],
128 };
129 var pos = A2(Utils.htmlHeight, 0, raw);
130 return A3(Element.newElement, pos._0, pos._1, raw);
131 }
132 }
134 function markdown(text, guid) {
135 var raw = {
136 ctor:'RawHtml',
137 html: text,
138 align: null,
139 guid: guid,
140 args: [],
141 };
142 var pos = A2(Utils.htmlHeight, 0, raw);
143 return A3(Element.newElement, pos._0, pos._1, raw);
144 }
146 return elm.Native.Text.values = {
147 toText: toText,
149 height : F2(height),
150 italic : italic,
151 bold : bold,
152 line : F2(line),
153 monospace : monospace,
154 typeface : F2(typeface),
155 color : F2(color),
156 link : F2(link),
158 leftAligned : block('left'),
159 rightAligned : block('right'),
160 centered : block('center'),
161 justified : block('justify'),
162 markdown : markdown,
164 toTypefaces:toTypefaces,
165 toLine:toLine,
166 };
167};