Skip to content

Instantly share code, notes, and snippets.

@darwin
Created July 28, 2009 13:32
Show Gist options
  • Select an option

  • Save darwin/157281 to your computer and use it in GitHub Desktop.

Select an option

Save darwin/157281 to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////////////////////////////
Mesan.Rules.SourcePanel = function(config){
config = config || {};
Ext.applyIf(config, {
id: config.prefix+'rule-source-panel',
title: _('Source Code'),
iconCls: 'rule-code',
tbar: this.createToolbar()
});
Mesan.Rules.SourcePanel.superclass.constructor.call(this, config);
var that = this;
this.on('render', function(){
this.createEditor(function(editor){
if (that.readOnly) that.body.mask();
});
}, this);
this.on('show', function(){
this.updateToolbar();
}, this);
};
Ext.extend(Mesan.Rules.SourcePanel, Ext.Panel, {
//----------------------------------------------------------------------------------------
createEditor: function(fn){
this.editor = new CodeMirror(this.body.dom, {
path: 'libjs/codemirror/js/',
parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
stylesheet: "libjs/codemirror/css/jscolors.css",
height: '100%',
width: '100%',
readOnly: this.readOnly,
initCallback: fn
});
},
//----------------------------------------------------------------------------------------
createToolbar: function(){
this.undoButton = new Ext.Toolbar.Button({
text: _('Undo'),
tooltip: {title:_('Undo'), text:_('Undoes last action')},
iconCls: 'rule-undo',
disabled: true,
scope: this,
handler: this.undoAction
}
);
this.redoButton = new Ext.Toolbar.Button({
text: _('Redo'),
tooltip: {title:_('Redo'), text:_('Redoes last action')},
iconCls: 'rule-redo',
disabled: true,
scope: this,
handler: this.redoAction
}
);
this.indentButton = new Ext.Toolbar.Button({
text: _('Indent'),
tooltip: {title:_('Indent document'), text:_('Applies indentation to the whole document')},
iconCls: 'rule-indent',
disabled: true,
scope: this,
handler: this.indentAction
}
);
var res = [this.undoButton, this.redoButton, this.indentButton];
if (Mesan.PRODUCTION_MODE) return res;
this.tokenizeButton = new Ext.Toolbar.Button({
text: 'Tokenize',
iconCls: 'rule-warning',
scope: this,
handler: this.tokenizeAction
}
);
res.push(this.tokenizeButton);
return res;
},
//----------------------------------------------------------------------------------------
updateToolbar: function(){
if (this.editor && !this.readOnly)
{
this.undoButton.enable();
this.redoButton.enable();
this.indentButton.enable();
}
else
{
this.undoButton.disable();
this.redoButton.disable();
this.indentButton.disable();
}
},
//----------------------------------------------------------------------------------------
setCode: function(code){
this.currentCode = code||'';
if (!this.editor) { return; }
if (!this.editor.editor) // velmi divne chovani na FF - vnitrni editor jeste nemusi byt pripraveny
{
this.setCode.defer(100, this, [code]);
return;
}
this.editor.setCode(this.currentCode);
this.updateToolbar();
},
//----------------------------------------------------------------------------------------
getCode: function(){
if (!this.editor) return this.currentCode||'';
this.currentCode = this.editor.getCode();
return this.currentCode;
},
//----------------------------------------------------------------------------------------
undoAction: function(){
if (this.editor) this.editor.editor.history.undo();
},
//----------------------------------------------------------------------------------------
redoAction: function(){
if (this.editor) this.editor.editor.history.redo();
},
//----------------------------------------------------------------------------------------
indentAction: function(){
if (this.editor) this.editor.reindent();
},
//----------------------------------------------------------------------------------------
tokenizeAction: function(){
var code = this.getCode();
this.form.editor.parseSource(code, true);
},
//----------------------------------------------------------------------------------------
loadItem: function(item){
this.setCode(item.code);
},
//----------------------------------------------------------------------------------------
saveItem: function(item){
item.code = this.getCode();
}
});
//////////////////////////////////////////////////////////////////////////////////////////////
Mesan.Rules.TabPanel = function(){
this.form = new Mesan.Rules.FormPanel({
prefix: 'edit-'
});
this.source = new Mesan.Rules.SourcePanel({
prefix: 'edit-',
form: this.form
});
this.form.editor.createWizardWindow();
Mesan.Rules.TabPanel.superclass.constructor.call(this, {
family: Mesan.rulesFamilyId,
id: 'rule-tab-panel',
iconCls: 'rule-tab-panel',
activeTab: 0,
items: [this.form, this.source]
});
this.on('tabchange', function(parent, newTab, oldTab){
if (newTab==this.source)
{
if (this.form.editor.isDirty)
{
var code = this.form.editor.generateSource();
this.source.setCode.defer(500, this.source, [code]);
this.form.editor.isDirty = false;
}
}
if (newTab==this.form)
{
var code = this.source.getCode();
this.form.editor.parseSource(code);
}
});
};
Ext.extend(Mesan.Rules.TabPanel, Ext.TabPanel, {
//----------------------------------------------------------------------------------------
loadItem: function(item) {
this.source.loadItem(item);
this.form.loadItem(item);
},
//----------------------------------------------------------------------------------------
saveItem: function(item) {
this.form.saveItem(item);
this.source.saveItem(item);
}
});
//////////////////////////////////////////////////////////////////////////////////////////////
Mesan.Rules.RuleWindow = function(config) {
config = config || {};
this.tabPanel = new Mesan.Rules.TabPanel();
Ext.applyIf(config, {
title: _('Create Rule'),
iconCls: 'new-rule',
id: 'rule-win',
help: 'rules',
resizable: true,
closable: true,
maximizable: true,
width: 1000,
height: 500,
minWidth: 700,
minHeight: 500,
border: false,
layout: 'fit',
items: [
this.tabPanel
],
defaultButton: 0,
buttons:[{
text: _('Create'),
tabIndex: 4,
handler: this.onCreate,
scope: this
},{
text: _('Cancel'),
tabIndex: 5,
handler: this.hide.createDelegate(this, [])
}]
});
Mesan.Rules.RuleWindow.superclass.constructor.call(this, config);
}
Ext.extend(Mesan.Rules.RuleWindow, Mesan.Window, {
//----------------------------------------------------------------------------------------
initEvents: function(){
Mesan.Rules.RuleWindow.superclass.initEvents.call(this);
},
//----------------------------------------------------------------------------------------
show: function(item){
var wasVisible = this.isVisible();
Mesan.Rules.RuleWindow.superclass.show.apply(this);
if (item==undefined) {
this.el.removeClass('edit');
this.el.addClass('new');
this.setTitle(_('Create Rule'));
this.setIconClass('new-rule');
this.buttons[0].setText(_('Create'));
this.tabPanel.form.editor.isDirty = true; // pri vytvareni generovat prazdny skript s return true;
}
else {
this.el.removeClass('new');
this.el.addClass('edit');
this.setTitle(_('Edit Rule'));
this.setIconClass('edit-rule');
this.buttons[0].setText(_('Save'));
}
this.item = item || {code:'', name:_('New Rule'), description:''};
this.tabPanel.loadItem(this.item);
this.tabPanel.form.ruleName.focus(true, 100);
if (!wasVisible) this.tabPanel.setActiveTab(0);
},
//----------------------------------------------------------------------------------------
onCreate: function() {
if (!this.tabPanel.form.form.form.isValid()) return;
var editor = this.tabPanel.form.editor;
var source = this.tabPanel.source;
if (editor.isDirty) var code = editor.generateSource(); else var code = source.getCode();
editor.parseSource(code);
var ds = editor.getStore();
var records = ds.getRange();
editor.detectProblems(records);
if (editor.problematicRows && editor.problematicRows.length){
if (this.tabPanel.getActiveTab()!=this.tabPanel.form) this.tabPanel.setActiveTab(this.tabPanel.form);
var mb = messageBox({
width: 500,
modal: true,
title: _('Possible problems detected'),
msg: _('Your rule contains possible runtime problems at {0} {1}.<br/>Do you really want to save this rule ?', _E('row', editor.problematicRows.length), editor.problematicRows.toSentence()),
buttons: Ext.MessageBox.YESNO,
fn: function(res){
if (res=='yes') {
this.finalCreate(code);
}
},
scope: this,
icon: Ext.MessageBox.WARNING
});
}
else this.finalCreate(code);
},
//----------------------------------------------------------------------------------------
finalCreate: function(code){
this.tabPanel.saveItem(this.item);
this.item.code = code; // takovy pekny hack #355
if (this.item.id===undefined)
{
// create new item
Mesan.send(Mesan.Items.ActInsert(Mesan.rulesFamilyId, [this.item]));
}
else
{
// edit item
Mesan.send(Mesan.Items.ActEdit([this.item], Mesan.rulesFamilyId));
}
this.hide();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment