darwin (owner)

Revisions

gist: 157281 Download_button fork
public
Public Clone URL: git://gist.github.com/157281.git
Embed All Files: show embed
JavaScript #
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
//////////////////////////////////////////////////////////////////////////////////////////////
 
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();
}
});
 
JavaScript #
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
//////////////////////////////////////////////////////////////////////////////////////////////
 
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);
}
});
 
JavaScript #
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
//////////////////////////////////////////////////////////////////////////////////////////////
 
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();
}
});