on ea4fe54 help sign in
elk/core/Native/Input.js
84 lines 2.6 kB history raw
2Elm.Input = function() {
3 var JS = Elm.JavaScript;
4 var toElmString = Elm.JavaScript.castJSStringToString;
5 var newTextInput = function(elem, ghostText) {
6 elem.placeholder = JS.castStringToJSString(ghostText);
7 var str = Elm.Signal.constant(["Nil"]);
8 Value.addListener(elem, 'keyup', function(e) {
9 Dispatcher.notify(str.id, toElmString(elem.value));
10 elem.focus();
11 });
12 elem.style.padding = "1px";
13 return Value.Tuple(Value.wrap(elem), str);
14 };
15 var newElement = function(name) {
16 var e = document.createElement(name);
17 e.style.padding = "0";
18 e.style.margin = "0";
19 return e;
20 };
21 var textArea = function(cols) { return function(rows) {
22 var textarea = newElement('textarea');
23 textarea.rows = rows;
24 textarea.cols = cols;
25 return newTextInput(textarea, "");
26 };
27 };
28 var textField = function(ghostText) {
29 var field = newElement('input');
30 field.type = 'text';
31 return newTextInput(field, ghostText);
32 };
33 var password = function(ghostText) {
34 var field = newElement('input');
35 field.type = 'password';
36 return newTextInput(field, ghostText);
37 };
38 var checkbox = function(checked) {
39 var box = newElement('input');
40 box.type = 'checkbox';
41 box.checked = checked;
42 var status = Elm.Signal.constant(checked);
43 Value.addListener(box, 'change', function(e) {
44 Dispatcher.notify(status.id, box.checked);
45 });
46 return Value.Tuple(Value.wrap(box), status);
47 };
48 var dropDown = function(options) {
49 var slct = newElement('select');
50 var opts = [];
51 while (options[0] === "Cons") {
52 var opt = newElement('option');
53 var str = Value.toText(options[1][1]);
54 opt.value = str;
55 opt.innerHTML = str;
56 slct.appendChild(opt);
57 opts.push(options[1][2]);
58 options = options[2];
59 }
60 var status = Elm.Signal.constant(opts[0]);
61 Value.addListener(slct, 'change', function(e) {
62 Dispatcher.notify(status.id, opts[slct.selectedIndex]);
63 });
64 return Value.Tuple(Value.wrap(slct), status);
65 };
66 var stringDropDown = function(opts) {
67 return dropDown(Elm.List.map (function(x) {return Value.Tuple(x,x);}) (opts));
68 };
69 var button = function(name) {
70 var b = newElement('input');
71 b.type = "button";
72 b.value = JS.castStringToJSString(name);
73 var press = Elm.Signal.constant(false);
74 Value.addListener(b, 'click', function(e) {
75 Dispatcher.notify(press.id, true);
76 Dispatcher.notify(press.id, false);
77 });
78 return Value.Tuple(Value.wrap(b),press);
79 };
80 return {textArea:textArea, textField:textField,
81 password:password, checkbox:checkbox,
82 dropDown:dropDown, stringDropDown:stringDropDown,
83 button:button};
84}();