Widget.subclass('SimpleHTMLBrowser', {
constructor: function SimpleHTMLBrowser(){ Class.initializer.apply(this, arguments) },
open: function ($super) {
var result = $super();
this.label = result.titleBar;
this.init()
return result;
},
buildView: function (extent) {
var margin = 0;
var bounds = extent.extentAsRectangle();
// var labelExtent = bounds.scaleByRect(new Rectangle(0, 0, 1, 0.05));
var pane1Extent = bounds.scaleByRect(new Rectangle(0, 0.0, 0.5, 0.5));
var pane2Extent = bounds.scaleByRect(new Rectangle(0.5, 0.0, 0.5, 0.5));
var pane3Extent = bounds.scaleByRect(new Rectangle(0, 0.5, 1, 0.5));
var mainMorph = new ContainerMorph(bounds.expandBy(margin));
mainMorph.padding = new Rectangle(margin, margin, margin, margin);
mainMorph.applyStyle({fill: Color.gray, borderRadius: 4})
// var label = new SimpleTextMorph(labelExtent);
// label.applyStyle({suppressGrabbing: true, suppressHandles: true, fill: null});
// label.ignoreEvents();
// label.padding = new Rectangle(0,0,0,0)
// // FIXME
// label.textNode.style['text-align'] = 'center';
// label.textNode.style['font-size'] = 'small';
// label.moveBy(pt(0, -5))
// this.label = label;
// mainMorph.addMorph(label);
var list1 = new SimpleListMorph(pane1Extent);
list1.applyStyle({suppressGrabbing: true, suppressHandles: true});
this.classList = list1;
connect(list1, 'selection', this, 'classSelected');
mainMorph.addMorph(list1)
var list2 = new SimpleListMorph(pane2Extent);
list2.applyStyle({suppressGrabbing: true, suppressHandles: true});
this.methodList = list2;
connect(list2, 'selection', this, 'methodSelected');
mainMorph.addMorph(list2)
var sourcePane = new SimpleTextMorph(pane3Extent);
sourcePane.applyStyle({suppressGrabbing: true, suppressHandles: true});
this.sourcePane = sourcePane;
connect(sourcePane, 'savedString', this, 'newSource');
mainMorph.addMorph(sourcePane);
return mainMorph;
},
init: function () {
this.classList.setItemList(this.classNames())
},
classes: function () {
return Global.classes()
.reject(function(ea) { return !ea.name || ea.name.startsWith('anonym') })
.sortBy(function(ea) { return ea.name });
},
classNames: function () {
return this.classes().collect(function(ea) { return ea.name });
},
getSelectedClass: function () {
return this.classes().detect(function(ea) { return ea.name == this.selectedClassName }, this);
},
getSelectedMethod: function () {
return this.selectedClassName && this.selectedMethodName && this.getSelectedClass().prototype[this.selectedMethodName];
},
classSelected: function (className) {
this.selectedMethodName = null
this.selectedClassName = className;
methodNames = this.getSelectedClass().localFunctionNames();
methodNames = methodNames.without('constructor');
this.methodList.setItemList(methodNames);
this.label.setTitle(className);
this.showClassSource()
},
methodSelected: function (methodName) {
this.selectedMethodName = methodName
this.sourcePane.setTextString(this.getSelectedMethod().toString());
this.label.setTitle(this.selectedClassName +'>>' + methodName);
},
newSource: function (src) {
if (this.getSelectedMethod()) // otherwise class code
src = this.selectedClass.name + '.prototype.' + this.selectedMethodName + ' = ' + src
// console.log(src)
try {
eval(src)
}
catch(e) {
this.sourcePane.world().alert('Error evaluating '
+ (this.getSelectedMethod() ? (this.selectedClassName + '>>' + this.selectedMethodName) : this.selectedClassName)
+ ': ' + e);
}
this.sourcePane.world().setStatusMessage('Successfully evaluated ' + this.selectedClassName + '>>' + this.selectedMethodName);
},
showClassSource: function () {
var superName = (this.getSelectedClass().superclass && this.getSelectedClass().superclass.name) || 'Object';
var src = superName + '.subclass(\'' + this.selectedClassName + '\', {\n';
this.getSelectedClass().localFunctionNames().forEach(function(name) {
var methodSrc = this.getSelectedClass().prototype[name].toString();
src += '\n';
// FIX indentation of first line by looking at the second
var lines = methodSrc.split('\n');
if (!lines[1] || lines[1].startsWith('\t\t')) src += '\t';
src += name + ': ' + methodSrc + ',\n\n'
}, this);
src += '});'
this.sourcePane.setTextString(src);
},
});
truetruetruefalse