omouse / sandals

An imitation for JavaScript of shoes, the tiny Ruby graphical toolkit

This URL has Read+Write access

sandals / sandals.js
100644 158 lines (139 sloc) 4.448 kb
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Sandals - a JavaScript/Rhino version of _why's Shoes library
* for Ruby that uses Java's Swing library
* Copyright (C) 2008, Rudolf Olah
*/
 
importPackage(java.awt);
importPackage(javax.swing);
importPackage(javax.swing.text);
importPackage(Packages.beachhouse);
importPackage(Packages.beachhouse.util);
 
/*** STYLE STUFF ***/
function inherit_style (parent, child) {
  var name = '';
  var merged = new Object();
  for (name in parent)
    merged[name] = parent[name];
  for (name in child)
    merged[name] = child[name];
  return merged;
}
 
function has_style (args, position) {
  var x = position >= 0 ? args[position] : args[args.length + position];
  if (x instanceof Object)
    return x;
  return false;
}
 
function extract_style (default_style, args, position) {
  var user_style = has_style(args, position);
  if (user_style)
    return inherit_style(default_style, user_style);
  return default_style;
}
 
function js_to_beach_style (js, bs) {
  if ("color" in js)
    bs.setColor(js.color);
  if ("background" in js)
    bs.setBackground(js.background);
  bs.setResizable("resizable" in js && js.resizable ? true : false);
  bs.setBold("bold" in js && js.bold ? true : false);
  bs.setItalic("italic" in js && js.italic ? true : false);
  bs.setUnderline("underline" in js && js.underline ? true : false);
  bs.setFontSize("size" in js ? js.size : 12);
}
 
/*** STYLED TEXT ***/
function StyledText (text) {
  if (arguments.length == 0 || arguments.length > 2)
    throw Error("Too many arguments.");
  this.style = (arguments.length == 2 ? arguments[1] : new Object() );
  if (text instanceof StyledText) {
    this.text = text.text;
    this.style = inherit_style(this.style, text.style);
  } else {
    this.text = text;
  }
}
 
/*** SANDAL STUFF ***/
var Sandals = new Object();
Sandals.RELEASE_NAME = "Rootbeer";
Sandals.RELEASE_ID = 1;
/*
Sandals.app = function () {
if (arguments.length == 0)
throw Error("Sandals requires style, or things to display");
this.style = extract_style({ width: 500, height: 600, resizable: true,
background: "grey", color: "black" },
arguments, 0);
 
this.window = new AppWindow();
js_to_beach_style(this.style, this.window.style);
var blah = function () {
var app = {
get name() { return this.window.getTitle(); },
set name(x) { this.window.setTitle(x); },
location: java.lang.System.getProperty("user.dir")
};
arguments[0]();
this.window.go();
print("Walking on to the beach.");
};
*/
try {
  if (app.started)
    print("Sandals application already running.");
} catch (ReferenceError) {
  /* There will only ever be one Sandals application running at a time. */
  var app = new Object;
}
 
Sandals.go = function (body) {
  this.app_window = new AppWindow();
  app.__defineGetter__("name", function () { return Sandals.app_window.name; });
  app.__defineSetter__("name", function (x) { Sandals.app_window.name = x; });
  app.location = this.app_window.getCurrentDirectory();
  print("Slipping feet into Sandals.");
  body();
  this.app_window.go();
  print("Walking on to the beach.");
}
 
/*** LAYOUT STUFF ***/
Sandals.stack = function () {
  var layout = new Stack();
  for (var i = 0; i < arguments.length; i++)
    layout.add(arguments[i]);
  return layout;
};
 
Sandals.flow = function () {
  var layout = new Flow();
  for (var i = 0; i < arguments.length; i++)
    layout.add(arguments[i]);
  return layout;
};
 
/*** TEXT BLOCKS STUFF ***/
Sandals.para = function () {
  print("Arguments to para: " + arguments[0]);
  var style = extract_style({ background: "white", color: "black" },
arguments, -1);
  var text = TextBlock.Para("");
  for each (var x in arguments) {
    text.appendText(x);
  }
  js_to_beach_style(style, text.style);
  this.app_window.add(text);
};
 
Sandals.title = function () {
  return TextBlock.Title(arguments[0]);
};
 
Sandals.caption = function () {
  var style = extract_style({ size: 14 }, arguments, -1);
  return this.para();
};
 
Sandals.strong = function (text) {
  return new StyledText(text, extract_style({ bold: true }, arguments, -1));
};
 
Sandals.em = function (text) {
  return new StyledText(text, extract_style({ italic: true }, arguments, -1));
};
 
Sandals.code = function (text) {
  return new StyledText(text, extract_style({ code: true }, arguments, -1));
};
 
Sandals.ins = function (text) {
  return new StyledText(text, extract_style({ underline: true }, arguments, -1));
};