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