提交 e4cf8981 编写于 作者: S Sercan

refactored

上级 4dacbaf6
......@@ -13,6 +13,64 @@ Template.clearSessions = function () {
Session.set(Template.strSessionSelectedCollection, undefined);
};
Template.setOptionsComboboxChangeEvent = function (cmb) {
cmb.on('change', function (evt, params) {
var array = Session.get(Template.strSessionSelectedOptions);
if (params.deselected) {
array.remove(params.deselected);
}
else {
array.push(params.selected);
}
Session.set(Template.strSessionSelectedOptions, array);
});
};
Template.getParentTemplateName = function (levels) {
var view = Blaze.currentView;
if (typeof levels === "undefined") {
levels = 1;
}
while (view) {
if (view.name.indexOf("Template.") != -1 && !(levels--)) {
return view.name.substring(view.name.indexOf('.') + 1);
}
view = view.parentView;
}
};
Template.initializeAceEditor = function (id, evt) {
AceEditor.instance(id, {
mode: "javascript",
theme: 'dawn'
}, function (editor) {
editor.$blockScrolling = Infinity;
editor.setOptions({
fontSize: "11pt",
showPrintMargin: false
});
// remove newlines in pasted text
editor.on("paste", function (e) {
e.text = e.text.replace(/[\r\n]+/g, " ");
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function (x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// disable Enter Shift-Enter keys
editor.commands.bindKey("Enter|Shift-Enter", evt);
});
};
Template.registerHelper('isSelected', function (option) {
return $.inArray(option, Session.get(Template.strSessionSelectedOptions)) != -1;
});
Template.registerHelper('getConnection', function () {
if (Session.get(Template.strSessionConnection)) {
return Connections.findOne({_id: Session.get(Template.strSessionConnection)});
......@@ -27,7 +85,6 @@ Template.registerHelper('getSelectedCollection', function () {
return Session.get(Template.strSessionSelectedCollection);
});
/**
* Adds remove by value functionality to arrays. e.x. myArray.remove('myValue');
* */
......@@ -40,23 +97,4 @@ Array.prototype.remove = function () {
}
}
return this;
};
/**
* Get the parent template instance
* @param {Number} [levels] How many levels to go up. Default is 1
* @returns {Blaze.TemplateInstance}
*/
Blaze.TemplateInstance.prototype.parentTemplate = function (levels) {
var view = Blaze.currentView;
if (typeof levels === "undefined") {
levels = 1;
}
while (view) {
if (view.name.indexOf("Template.") != -1 && !(levels--)) {
return view.templateInstance();
}
view = view.parentView;
}
};
\ No newline at end of file
......@@ -15,6 +15,11 @@ Template.browseCollection.onRendered(function () {
});
cmb.chosen();
cmb.on('change', function (evt, params) {
Session.set(Template.strSessionSelectedOptions, []);
});
Session.set(Template.strSessionSelectedOptions, []);
});
Template.browseCollection.events({
......@@ -41,7 +46,7 @@ Template.browseCollection.events({
var queryTemplate = Session.get(Template.strSessionSelectedQuery);
if (queryTemplate) {
Template[queryTemplate].executeQuery();
}else{
} else {
Template["find"].executeQuery();
}
}
......
......@@ -10,7 +10,7 @@
<div class="form-group">
<label class="col-lg-1 control-label">Options</label>
<div class="col-lg-11">
<select id="cmbCursorOptions"
<select id="cmbFindCursorOptions"
data-placeholder="Choose one or more options.."
multiple="true"
class="chosen-select form-control"
......
......@@ -2,42 +2,13 @@
* Created by sercan on 30.12.2015.
*/
Template.find.onRendered(function () {
// set ace editor
Template.find.initializeAceEditor();
Template.initializeAceEditor('preSelector', Template.find.executeQuery);
Template.find.initializeOptions();
Template.find.initializeSessionVariable();
});
Template.find.initializeAceEditor = function () {
AceEditor.instance("preSelector", {
mode: "javascript",
theme: 'dawn'
}, function (editor) {
editor.$blockScrolling = Infinity;
editor.setOptions({
fontSize: "11pt",
showPrintMargin: false
});
// remove newlines in pasted text
editor.on("paste", function (e) {
e.text = e.text.replace(/[\r\n]+/g, " ");
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function (x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// disable Enter Shift-Enter keys
editor.commands.bindKey("Enter|Shift-Enter", Template.find.executeQuery);
});
};
Template.find.initializeOptions = function () {
var cmb = $('#cmbCursorOptions');
var cmb = $('#cmbFindCursorOptions');
$.each(CURSOR_OPTIONS, function (key, value) {
cmb.append($("<option></option>")
.attr("value", key)
......@@ -45,27 +16,10 @@ Template.find.initializeOptions = function () {
});
cmb.chosen();
Template.find.setCursorOptionsChangeEvent(cmb);
};
Template.find.setCursorOptionsChangeEvent = function (cmb) {
cmb.on('change', function (evt, params) {
var array = Session.get(Template.strSessionSelectedOptions);
if (params.deselected) {
array.remove(params.deselected);
}
else {
array.push(params.selected);
}
Session.set(Template.strSessionSelectedOptions, array);
});
};
Template.find.initializeSessionVariable = function () {
Session.set(Template.strSessionSelectedOptions, []);
Template.setOptionsComboboxChangeEvent(cmb);
};
Template.find.executeQuery = function (methodName) {
Template.find.executeQuery = function () {
var laddaButton = Template.browseCollection.initExecuteQuery();
var connection = Connections.findOne({_id: Session.get(Template.strSessionConnection)});
var selectedCollection = Session.get(Template.strSessionSelectedCollection);
......@@ -92,7 +46,7 @@ Template.find.executeQuery = function (methodName) {
return;
}
Meteor.call((methodName ? methodName : "find"), connection, selectedCollection, selector, cursorOptions, function (err, result) {
Meteor.call("find", connection, selectedCollection, selector, cursorOptions, function (err, result) {
if (err || result.error) {
var errorMessage;
if (err) {
......
......@@ -10,7 +10,7 @@
<div class="form-group">
<label class="col-lg-1 control-label">Options</label>
<div class="col-lg-11">
<select id="cmbCursorOptions"
<select id="cmbFindOneCursorOptions"
data-placeholder="Choose one or more options.."
multiple="true"
class="chosen-select form-control"
......
......@@ -2,14 +2,12 @@
* Created by RSercan on 1.1.2016.
*/
Template.findOne.onRendered(function () {
// set ace editor
Template.find.initializeAceEditor();
Template.initializeAceEditor('preSelector', Template.findOne.executeQuery);
Template.findOne.initializeOptions();
Template.find.initializeSessionVariable();
});
Template.findOne.initializeOptions = function () {
var cmb = $('#cmbCursorOptions');
var cmb = $('#cmbFindOneCursorOptions');
$.each(CURSOR_OPTIONS, function (key, value) {
// dont add limit, it will be 1 already
if (value != CURSOR_OPTIONS.LIMIT) {
......@@ -20,9 +18,52 @@ Template.findOne.initializeOptions = function () {
});
cmb.chosen();
Template.find.setCursorOptionsChangeEvent(cmb);
Template.setOptionsComboboxChangeEvent(cmb);
};
Template.findOne.executeQuery = function () {
Template.find.executeQuery('findOne');
var laddaButton = Template.browseCollection.initExecuteQuery();
var connection = Connections.findOne({_id: Session.get(Template.strSessionConnection)});
var selectedCollection = Session.get(Template.strSessionSelectedCollection);
var cursorOptions = Template.find.getCursorOptions();
var selector = ace.edit("preSelector").getSession().getValue();
if (!selector) {
selector = {};
}
else {
try {
selector = JSON.parse(selector);
}
catch (err) {
toastr.error("Syntax error on selector: " + err.message);
laddaButton.ladda('stop');
return;
}
}
if (cursorOptions["ERROR"]) {
toastr.error(cursorOptions["ERROR"]);
laddaButton.ladda('stop');
return;
}
Meteor.call("findOne", connection, selectedCollection, selector, cursorOptions, function (err, result) {
if (err || result.error) {
var errorMessage;
if (err) {
errorMessage = err.message;
} else {
errorMessage = result.error.message;
}
toastr.error("Couldn't execute query: " + errorMessage);
// stop loading animation
laddaButton.ladda('stop');
return;
}
Template.browseCollection.setResult(result.result);
// stop loading animation
laddaButton.ladda('stop');
});
};
\ No newline at end of file
<template name="findOneModifyOptions">
<template name="findOneAndUpdate">
<div class="form-group">
<label class="col-lg-1 control-label">Selector</label>
<div class="col-lg-11">
......@@ -10,7 +10,7 @@
<div class="form-group">
<label class="col-lg-1 control-label">Options</label>
<div class="col-lg-11">
<select id="cmbFindOneOptions"
<select id="cmbFindOneModifyOptions"
data-placeholder="Choose one or more options.."
multiple="true"
class="chosen-select form-control"
......
/**
* Created by RSercan on 1.1.2016.
*/
Template.findOneAndUpdate.onRendered(function () {
// set ace editor
Template.initializeAceEditor('preSelector', Template.findOneAndUpdate.executeQuery);
Template.findOneAndUpdate.initializeOptions();
});
Template.findOneAndUpdate.initializeOptions = function () {
var cmb = $('#cmbFindOneModifyOptions');
$.each(CURSOR_OPTIONS, function (key, value) {
// dont add limit, it will be 1 already
if (value != CURSOR_OPTIONS.LIMIT) {
cmb.append($("<option></option>")
.attr("value", key)
.text(value));
}
});
cmb.chosen();
Template.setOptionsComboboxChangeEvent(cmb);
};
Template.findOneAndUpdate.executeQuery = function () {
};
\ No newline at end of file
<template name="project">
<div class="form-group">
<label class="col-lg-1 control-label">project</label>
<div class="col-lg-11">
<pre class="form-control" id='aceProject'></pre>
</div>
</div>
</template>
\ No newline at end of file
/**
* Created by RSercan on 1.1.2016.
*/
Template.project.onRendered(function () {
switch (Template.getParentTemplateName(2)) {
case QUERY_TYPES.FIND:
Template.initializeAceEditor('aceProject', Template.find.executeQuery);
break;
case QUERY_TYPES.FINDONE:
Template.initializeAceEditor('aceProject', Template.findOne.executeQuery);
break;
case QUERY_TYPES.FINDONE_AND_UPDATE:
Template.initializeAceEditor('aceProject', Template.findOneAndUpdate.executeQuery);
break;
}
});
\ No newline at end of file
<template name="sort">
<div class="form-group">
<label class="col-lg-1 control-label">sort</label>
<div class="col-lg-11">
<pre class="form-control" id='aceSort'></pre>
</div>
</div>
</template>
\ No newline at end of file
/**
* Created by RSercan on 1.1.2016.
*/
Template.sort.onRendered(function () {
switch (Template.getParentTemplateName(2)) {
case QUERY_TYPES.FIND:
Template.initializeAceEditor('aceSort', Template.find.executeQuery);
break;
case QUERY_TYPES.FINDONE:
Template.initializeAceEditor('aceSort', Template.findOne.executeQuery);
break;
case QUERY_TYPES.FINDONE_AND_UPDATE:
Template.initializeAceEditor('aceSort', Template.findOneAndUpdate.executeQuery);
break;
}
});
\ No newline at end of file
......@@ -19,15 +19,6 @@
{{/if}}
</template>
<template name="project">
<div class="form-group">
<label class="col-lg-1 control-label">project</label>
<div class="col-lg-11">
<pre class="form-control" id='aceProject'></pre>
</div>
</div>
</template>
<template name="limit">
<div class="form-group">
<label class="col-lg-1 control-label" labelfo>limit</label>
......@@ -46,15 +37,6 @@
</div>
</template>
<template name="sort">
<div class="form-group">
<label class="col-lg-1 control-label">sort</label>
<div class="col-lg-11">
<pre class="form-control" id='aceSort'></pre>
</div>
</div>
</template>
<template name="max">
<div class="form-group">
<label class="col-lg-1 control-label">max</label>
......
/**
* Created by sercan on 31.12.2015.
*/
Template.cursorOptions.helpers({
'isSelected': function (option) {
return $.inArray(option, Session.get(Template.strSessionSelectedOptions)) != -1;
}
});
Template.project.onRendered(function () {
Template.cursorOptions.initializeAceEditor('aceProject');
});
Template.sort.onRendered(function () {
Template.cursorOptions.initializeAceEditor('aceSort');
});
Template.max.onRendered(function () {
Template.cursorOptions.initializeAceEditor('aceMax');
});
......@@ -24,37 +10,14 @@ Template.min.onRendered(function () {
});
Template.cursorOptions.initializeAceEditor = function (id) {
AceEditor.instance(id, {
mode: "javascript",
theme: 'dawn'
}, function (editor) {
editor.$blockScrolling = Infinity;
editor.setOptions({
fontSize: "11pt",
showPrintMargin: false
});
// remove newlines in pasted text
editor.on("paste", function (e) {
e.text = e.text.replace(/[\r\n]+/g, " ");
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function (x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// find and findOne templates uses same cursor options so clarify ENTER key event on SELECTOR.
// disable Enter Shift-Enter keys and bind to executeQuery for corresponding template
var parentTemplateName = Template.getParentTemplateName(2);
if (parentTemplateName == QUERY_TYPES.FIND) {
Template.initializeAceEditor(id, Template.find.executeQuery);
}
else if (parentTemplateName == QUERY_TYPES.FINDONE) {
Template.initializeAceEditor(id, Template.findOne.executeQuery);
}
// disable Enter Shift-Enter keys and bind to executeQuery for corresponding template
var name = Template.instance().parentTemplate(2).view.name;
var cleanName = name.substring(name.indexOf('.') + 1);
if (cleanName == QUERY_TYPES.FIND) {
editor.commands.bindKey("Enter|Shift-Enter", Template.find.executeQuery);
}
else if (cleanName == QUERY_TYPES.FINDONE) {
editor.commands.bindKey("Enter|Shift-Enter", Template.findOne.executeQuery);
}
});
};
\ No newline at end of file
......@@ -13,24 +13,6 @@
{{/if}}
</template>
<template name="project">
<div class="form-group">
<label class="col-lg-1 control-label">project</label>
<div class="col-lg-11">
<pre class="form-control" id='aceProject'></pre>
</div>
</div>
</template>
<template name="sort">
<div class="form-group">
<label class="col-lg-1 control-label">sort</label>
<div class="col-lg-11">
<pre class="form-control" id='aceSort'></pre>
</div>
</div>
</template>
<template name="upsert">
<div class="form-group">
<label class="col-lg-1 control-label">upsert</label>
......
/**
* Created by RSercan on 1.1.2016.
*/
Template.findOneModifyOptions.helpers({
'isSelected': function (option) {
return $.inArray(option, Session.get(Template.strSessionSelectedOptions)) != -1;
}
});
Template.project.onRendered(function () {
Template.findOneModifyOptions.initializeAceEditor('aceProject');
});
Template.sort.onRendered(function () {
Template.findOneModifyOptions.initializeAceEditor('aceSort');
});
Template.findOneModifyOptions.initializeAceEditor = function (id) {
AceEditor.instance(id, {
mode: "javascript",
theme: 'dawn'
}, function (editor) {
editor.$blockScrolling = Infinity;
editor.setOptions({
fontSize: "11pt",
showPrintMargin: false
});
// remove newlines in pasted text
editor.on("paste", function (e) {
e.text = e.text.replace(/[\r\n]+/g, " ");
});
// make mouse position clipping nicer
editor.renderer.screenToTextCoordinates = function (x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// disable Enter Shift-Enter keys and bind to executeQuery for corresponding template
//var name = Template.instance().parentTemplate(2).view.name;
//var cleanName = name.substring(name.indexOf('.') + 1);
//if (cleanName == QUERY_TYPES.FIND) {
// editor.commands.bindKey("Enter|Shift-Enter", Template.find.executeQuery);
//}
//else if (cleanName == QUERY_TYPES.FINDONE) {
// editor.commands.bindKey("Enter|Shift-Enter", Template.findOne.executeQuery);
//}
});
};
\ No newline at end of file
*/
\ No newline at end of file
......@@ -18,6 +18,7 @@ Meteor.methods({
}
});
},
'removeConnection': function (connectionId) {
Connections.remove(connectionId);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册