Browse Source

sql.editor可以插入字段参数

master
windy 7 years ago
parent
commit
6210ceddfc
  1. 2
      demo/js/base/editor/demo.sql_editor.js
  2. 10
      dist/base.css
  3. 67
      dist/base.js
  4. 10
      dist/bundle.css
  5. 67
      dist/bundle.js
  6. 2
      dist/bundle.min.css
  7. 79
      dist/bundle.min.js
  8. 2
      dist/demo.js
  9. 2
      src/base/formula/config.js
  10. 4
      src/base/sql/codemirrior/sql-hint.js
  11. 61
      src/base/sql/editor.sql.js
  12. 10
      src/css/base/sql/sql.css
  13. 8
      src/less/base/sql/sql.less

2
demo/js/base/editor/demo.sql_editor.js

@ -11,7 +11,7 @@ Demo.SQLEditor = BI.inherit(BI.Widget, {
type : 'bi.sql_editor',
width : 300,
height : 200,
value : "select * from DEMO_CONTRACT where 合同类型 = '长期协议' and 购买数量 = sum([1,2,3,4])"
value : "select * from DEMO_CONTRACT where 合同类型 = ${长期协议} and 购买数量 = sum([1,2,3,4])"
});
BI.createWidget({
type: "bi.left",

10
dist/base.css vendored

@ -1043,6 +1043,16 @@ body .bi-button.button-ignore.disabled.clear:active,
.bi-sql-editor .cm-s-default .cm-function {
color: #3685f2;
}
.bi-sql-editor .param {
color: #ffffff;
padding: 0 5px;
margin: 1px 1px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
background: #3f8ce8;
display: inline-block;
}
.CodeMirror-hints .sql-fr-function {
color: #3685f2;
}

67
dist/base.js vendored

File diff suppressed because one or more lines are too long

10
dist/bundle.css vendored

@ -2656,6 +2656,16 @@ body .bi-button.button-ignore.disabled.clear:active,
.bi-sql-editor .cm-s-default .cm-function {
color: #3685f2;
}
.bi-sql-editor .param {
color: #ffffff;
padding: 0 5px;
margin: 1px 1px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
background: #3f8ce8;
display: inline-block;
}
.CodeMirror-hints .sql-fr-function {
color: #3685f2;
}

67
dist/bundle.js vendored

File diff suppressed because one or more lines are too long

2
dist/bundle.min.css vendored

File diff suppressed because one or more lines are too long

79
dist/bundle.min.js vendored

File diff suppressed because one or more lines are too long

2
dist/demo.js vendored

@ -844,7 +844,7 @@ Demo.SQLEditor = BI.inherit(BI.Widget, {
type : 'bi.sql_editor',
width : 300,
height : 200,
value : "select * from DEMO_CONTRACT where 合同类型 = '长期协议' and 购买数量 = sum([1,2,3,4])"
value : "select * from DEMO_CONTRACT where 合同类型 = ${长期协议} and 购买数量 = sum([1,2,3,4])"
});
BI.createWidget({
type: "bi.left",

2
src/base/formula/config.js

File diff suppressed because one or more lines are too long

4
src/base/sql/codemirrior/sql-hint.js

@ -239,14 +239,12 @@
var desc = {};
var cur = editor.getCursor();
var token = editor.getTokenAt(cur);
var collection =
BI.each(BI.FormulaCollections, function (idx, formula) {
if(formula.lastIndexOf(token.string, 0) == 0 && !BI.contains(functions, formula)) {
functions.push(formula);
}
});
BI.each(BI.FormulaJson, function (idx, formula) {
desc[formula.name.toLowerCase()] = formula.def;
});
keywords = BI.concat(BI.keys(keywords), functions);
identifierQuote = getIdentifierQuote(editor);

61
src/base/sql/editor.sql.js

@ -7,7 +7,8 @@ BI.SQLEditor = BI.inherit(BI.Widget, {
baseCls: "bi-sql-editor",
value: "",
lineHeight: 2,
showHint: true
showHint: true,
supportFunction: true
});
},
_init: function () {
@ -82,6 +83,18 @@ BI.SQLEditor = BI.inherit(BI.Widget, {
this.editor.focus();
},
insertParam: function (param) {
var value = param;
var from = this.editor.getCursor();
this.editor.replaceSelection(param);
var to = this.editor.getCursor();
var options = {className: "param", atomic: true};
options.value = value;
this.editor.markText(from, to, options);
this.editor.replaceSelection(" ");
this.editor.focus();
},
_checkWaterMark: function () {
var o = this.options;
if (!this.disabledWaterMark && BI.isEmptyString(this.editor.getValue()) && BI.isKey(o.watermark)) {
@ -91,12 +104,54 @@ BI.SQLEditor = BI.inherit(BI.Widget, {
}
},
_analyzeContent: function (v) {
var regx = /\$[\{][^\}]*[\}]|[^\$\{]*[^\$\{]/g;
return v.match(regx);
},
getValue: function () {
return this.editor.getValue();
return this.editor.getValue("\n", function (line) {
var rawText = line.text, value = line.text, num = 0;
value.text = rawText;
_.forEach(_.sortBy(line.markedSpans, "from"), function (i, ms) {
switch (i.marker.className) {
case "param":
case "error-param":
var fieldNameLength = i.to - i.from;
value = value.substr(0, i.from + num) + "$\{" + i.marker.value + "\}" + value.substr(i.to + num, value.length);
// 加上${}的偏移
num += 3;
// 加上实际值和显示值的长度差的偏移
num += (i.marker.value.length - fieldNameLength);
break;
}
});
return value;
});
},
setValue: function (v) {
this.editor.setValue(v);
var self = this, result;
this.refresh();
self.editor.setValue("");
result = this._analyzeContent(v || "");
BI.each(result, function (i, item) {
var fieldRegx = /\$[\{][^\}]*[\}]/;
var str = item.match(fieldRegx);
if (BI.isNotEmptyArray(str)) {
self.insertParam(str[0].substring(2, item.length - 1));
} else {
self.insertString(item);
}
});
this._checkWaterMark();
},
refresh: function () {
var self = this;
BI.nextTick(function () {
self.editor.refresh();
});
}
});
BI.shortcut("bi.sql_editor", BI.SQLEditor);

10
src/css/base/sql/sql.css

@ -7,6 +7,16 @@
.bi-sql-editor .cm-s-default .cm-function {
color: #3685f2;
}
.bi-sql-editor .param {
color: #ffffff;
padding: 0 5px;
margin: 1px 1px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
background: #3f8ce8;
display: inline-block;
}
.CodeMirror-hints .sql-fr-function {
color: #3685f2;
}

8
src/less/base/sql/sql.less

@ -14,6 +14,14 @@
color: @color-bi-text-highlight;
}
& .param {
color: @color-bi-text;
padding: 0 5px;
margin: 1px 1px;
.border-radius(2px);
background: @color-bi-background-highlight;
display: inline-block;
}
}
.CodeMirror-hints {

Loading…
Cancel
Save