2013-03-05 18:06:52 +00:00
|
|
|
|
2013-04-28 12:21:46 +00:00
|
|
|
Elm.Native.Text = function(elm) {
|
2013-03-09 08:22:42 +00:00
|
|
|
'use strict';
|
2013-03-05 18:06:52 +00:00
|
|
|
|
2013-03-09 08:22:42 +00:00
|
|
|
elm.Native = elm.Native || {};
|
2013-04-28 12:21:46 +00:00
|
|
|
if (elm.Native.Text) return elm.Native.Text;
|
2013-03-05 18:06:52 +00:00
|
|
|
|
2013-03-09 08:22:42 +00:00
|
|
|
var JS = Elm.JavaScript(elm);
|
2013-03-13 05:59:15 +00:00
|
|
|
var htmlHeight = Elm.Native.Utils(elm).htmlHeight;
|
2013-04-28 12:21:46 +00:00
|
|
|
var Color = Elm.Native.Color(elm);
|
2013-03-09 08:22:42 +00:00
|
|
|
var Element = Elm.Graphics.Element(elm);
|
2013-03-13 07:00:02 +00:00
|
|
|
var show = Elm.Native.Show(elm).show;
|
2013-03-05 18:06:52 +00:00
|
|
|
|
|
|
|
function makeSpaces(s) {
|
|
|
|
if (s.length == 0) { return s; }
|
|
|
|
var arr = s.split('');
|
|
|
|
if (arr[0] == ' ') { arr[0] = " " }
|
|
|
|
for (var i = arr.length; --i; ) {
|
|
|
|
if (arr[i][0] == ' ' && arr[i-1] == ' ') {
|
|
|
|
arr[i-1] = arr[i-1] + arr[i];
|
|
|
|
arr[i] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = arr.length; i--; ) {
|
|
|
|
if (arr[i].length > 1 && arr[i][0] == ' ') {
|
|
|
|
var spaces = arr[i].split('');
|
|
|
|
for (var j = spaces.length - 2; j >= 0; j -= 2) {
|
|
|
|
spaces[j] = ' ';
|
|
|
|
}
|
|
|
|
arr[i] = spaces.join('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arr = arr.join('');
|
|
|
|
if (arr[arr.length-1] === " ") {
|
|
|
|
return arr.slice(0,-1) + ' ';
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function properEscape(str) {
|
|
|
|
if (str.length == 0) return str;
|
|
|
|
str = str //.replace(/&/g, "&")
|
|
|
|
.replace(/"/g, /*"*/ '"')
|
|
|
|
.replace(/'/g, /*'*/ "'")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/\n/g, "<br/>");
|
|
|
|
var arr = str.split('<br/>');
|
|
|
|
for (var i = arr.length; i--; ) {
|
|
|
|
arr[i] = makeSpaces(arr[i]);
|
|
|
|
}
|
|
|
|
return arr.join('<br/>');
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:22:42 +00:00
|
|
|
function toText(str) { return properEscape(JS.fromString(str)); }
|
2013-03-05 18:06:52 +00:00
|
|
|
|
|
|
|
function addTag(tag) { return function(text) {
|
|
|
|
return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addStyle(style, value, text) {
|
|
|
|
return "<span style='" + style + ":" + value + "'>" + text + "</span>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function typeface(name, text) {
|
2013-03-09 08:22:42 +00:00
|
|
|
return addStyle('font-family', JS.fromString(name), text);
|
2013-03-05 18:06:52 +00:00
|
|
|
}
|
2013-03-13 07:00:02 +00:00
|
|
|
function monospace(text) {
|
|
|
|
return addStyle('font-family', 'monospace', text);
|
|
|
|
}
|
2013-03-05 18:06:52 +00:00
|
|
|
function size(px, text) { return addStyle('font-size', px + 'px', text) }
|
|
|
|
var header = addTag('h1');
|
2013-04-05 16:52:18 +00:00
|
|
|
function height(h, text) { return addStyle('font-size', h+'em', text) }
|
2013-03-05 18:06:52 +00:00
|
|
|
function italic(text) { return addStyle('font-style', 'italic', text) }
|
|
|
|
var bold = addTag('b');
|
2013-04-05 16:52:18 +00:00
|
|
|
|
|
|
|
function extract(c) {
|
|
|
|
if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
|
|
|
|
return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
|
|
|
|
}
|
2013-03-05 18:06:52 +00:00
|
|
|
function color(c, text) {
|
2013-04-05 16:52:18 +00:00
|
|
|
return addStyle('color', extract(c), text);
|
2013-03-05 18:06:52 +00:00
|
|
|
}
|
|
|
|
function underline(text) { return addStyle('text-decoration', 'underline', text) }
|
|
|
|
function overline(text) { return addStyle('text-decoration', 'overline', text) }
|
|
|
|
function strikeThrough(text) {
|
|
|
|
return addStyle('text-decoration', 'line-through', text);
|
|
|
|
}
|
|
|
|
function link(href, text) {
|
|
|
|
return "<a href='" + toText(href) + "'>" + text + "</a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function position(pos) { return function(text) {
|
|
|
|
var e = {ctor:'RawHtml',
|
2013-03-13 05:59:15 +00:00
|
|
|
_0: '<div style="padding:0;margin:0;text-align:' +
|
|
|
|
pos + '">' + text + '</div>'
|
|
|
|
};
|
2013-04-15 01:07:26 +00:00
|
|
|
var p = A2(htmlHeight, 0, text);
|
|
|
|
return A3(Element.newElement, p._0, p._1, e);
|
2013-03-05 18:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function asText(v) {
|
2013-03-13 07:00:02 +00:00
|
|
|
return position('left')(monospace(toText(show(v))));
|
2013-03-05 18:06:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 07:52:53 +00:00
|
|
|
function plainText(v) {
|
|
|
|
return position('left')(toText(v));
|
|
|
|
}
|
|
|
|
|
2013-04-28 12:21:46 +00:00
|
|
|
return elm.Native.Text = {
|
2013-03-05 18:06:52 +00:00
|
|
|
toText: toText,
|
|
|
|
|
|
|
|
header : header,
|
|
|
|
height : F2(height),
|
|
|
|
italic : italic,
|
|
|
|
bold : bold,
|
|
|
|
underline : underline,
|
|
|
|
overline : overline,
|
|
|
|
strikeThrough : strikeThrough,
|
2013-03-13 07:00:02 +00:00
|
|
|
monospace : monospace,
|
2013-03-05 18:06:52 +00:00
|
|
|
typeface : F2(typeface),
|
|
|
|
color : F2(color),
|
|
|
|
link : F2(link),
|
|
|
|
|
|
|
|
justified : position('justify'),
|
|
|
|
centered : position('center'),
|
|
|
|
righted : position('right'),
|
|
|
|
text : position('left'),
|
2013-04-10 07:52:53 +00:00
|
|
|
plainText : plainText,
|
2013-03-05 18:06:52 +00:00
|
|
|
|
|
|
|
asText : asText
|
|
|
|
};
|
|
|
|
|
2013-03-09 08:22:42 +00:00
|
|
|
};
|