7604 |
25 Feb 19 |
nicklas |
/* $Id $ |
7604 |
25 Feb 19 |
nicklas |
2 |
------------------------------------------------------------------ |
7604 |
25 Feb 19 |
nicklas |
Copyright (C) 2012 Nicklas Nordborg |
7604 |
25 Feb 19 |
nicklas |
4 |
|
7604 |
25 Feb 19 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
7604 |
25 Feb 19 |
nicklas |
Available at http://base.thep.lu.se/ |
7604 |
25 Feb 19 |
nicklas |
7 |
|
7604 |
25 Feb 19 |
nicklas |
BASE is free software; you can redistribute it and/or |
7604 |
25 Feb 19 |
nicklas |
modify it under the terms of the GNU General Public License |
7604 |
25 Feb 19 |
nicklas |
as published by the Free Software Foundation; either version 3 |
7604 |
25 Feb 19 |
nicklas |
of the License, or (at your option) any later version. |
7604 |
25 Feb 19 |
nicklas |
12 |
|
7604 |
25 Feb 19 |
nicklas |
BASE is distributed in the hope that it will be useful, |
7604 |
25 Feb 19 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
7604 |
25 Feb 19 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7604 |
25 Feb 19 |
nicklas |
GNU General Public License for more details. |
7604 |
25 Feb 19 |
nicklas |
17 |
|
7604 |
25 Feb 19 |
nicklas |
You should have received a copy of the GNU General Public License |
7604 |
25 Feb 19 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
7604 |
25 Feb 19 |
nicklas |
20 |
------------------------------------------------------------------ |
7604 |
25 Feb 19 |
nicklas |
21 |
|
7604 |
25 Feb 19 |
nicklas |
@author Nicklas |
7604 |
25 Feb 19 |
nicklas |
23 |
*/ |
7604 |
25 Feb 19 |
nicklas |
'use strict'; |
7604 |
25 Feb 19 |
nicklas |
25 |
|
7604 |
25 Feb 19 |
nicklas |
var ExpressionBuilder = function() |
7604 |
25 Feb 19 |
nicklas |
27 |
{ |
7604 |
25 Feb 19 |
nicklas |
28 |
|
7604 |
25 Feb 19 |
nicklas |
var textarea; |
7604 |
25 Feb 19 |
nicklas |
30 |
|
7604 |
25 Feb 19 |
nicklas |
var builder = {}; |
7604 |
25 Feb 19 |
nicklas |
32 |
|
7604 |
25 Feb 19 |
nicklas |
builder.initPage = function() |
7604 |
25 Feb 19 |
nicklas |
34 |
{ |
7604 |
25 Feb 19 |
nicklas |
// Get the expression from the parent window |
7604 |
25 Feb 19 |
nicklas |
textarea = window.opener.document.getElementById(Data.get('page-data', 'textarea')); |
7604 |
25 Feb 19 |
nicklas |
37 |
|
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['expression']; |
7604 |
25 Feb 19 |
nicklas |
frm.expression.value = textarea.value; |
7604 |
25 Feb 19 |
nicklas |
frm.expression.focus(); |
7604 |
25 Feb 19 |
nicklas |
41 |
|
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('spot', 'change', builder.insertSelected); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('rawdata', 'change', builder.insertSelected); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('reporter', 'change', builder.insertSelected); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('reporterList', 'change', builder.insertReporterListFunction); |
7604 |
25 Feb 19 |
nicklas |
46 |
|
7604 |
25 Feb 19 |
nicklas |
// Dialog buttons |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler('close', App.closeWindow); |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler('btnSave', builder.save); |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler('btnValidate', builder.validateExpression); |
7604 |
25 Feb 19 |
nicklas |
51 |
} |
7604 |
25 Feb 19 |
nicklas |
52 |
|
7604 |
25 Feb 19 |
nicklas |
builder.initializeElements = function(element, autoInit) |
7604 |
25 Feb 19 |
nicklas |
54 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (autoInit == 'enclose-selection') |
7604 |
25 Feb 19 |
nicklas |
56 |
{ |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler(element, builder.textButtonOnClick); |
7604 |
25 Feb 19 |
nicklas |
58 |
} |
7604 |
25 Feb 19 |
nicklas |
59 |
} |
7604 |
25 Feb 19 |
nicklas |
60 |
|
7604 |
25 Feb 19 |
nicklas |
builder.save = function() |
7604 |
25 Feb 19 |
nicklas |
62 |
{ |
7604 |
25 Feb 19 |
nicklas |
var value = document.forms['expression'].expression.value; |
7604 |
25 Feb 19 |
nicklas |
if (textarea.type == 'text') value = value.replace(/\n/g, ''); |
7604 |
25 Feb 19 |
nicklas |
textarea.value = value; |
7604 |
25 Feb 19 |
nicklas |
Events.sendChangeEvent(textarea); |
7604 |
25 Feb 19 |
nicklas |
window.close(); |
7604 |
25 Feb 19 |
nicklas |
68 |
} |
7604 |
25 Feb 19 |
nicklas |
69 |
|
7604 |
25 Feb 19 |
nicklas |
builder.textButtonOnClick = function(event) |
7604 |
25 Feb 19 |
nicklas |
71 |
{ |
7604 |
25 Feb 19 |
nicklas |
var prefix = Data.get(event.currentTarget, 'prefix') || ''; |
7604 |
25 Feb 19 |
nicklas |
var suffix = Data.get(event.currentTarget, 'suffix') || ''; |
7604 |
25 Feb 19 |
nicklas |
builder.encloseSelection(prefix, suffix); |
7604 |
25 Feb 19 |
nicklas |
75 |
} |
7604 |
25 Feb 19 |
nicklas |
76 |
|
7604 |
25 Feb 19 |
nicklas |
builder.insertSelected = function(event) |
7604 |
25 Feb 19 |
nicklas |
78 |
{ |
7604 |
25 Feb 19 |
nicklas |
var list = event.currentTarget; |
7604 |
25 Feb 19 |
nicklas |
var selected = list[list.selectedIndex]; |
7604 |
25 Feb 19 |
nicklas |
81 |
|
7604 |
25 Feb 19 |
nicklas |
var prefix = Data.get(selected, 'prefix') || ''; |
7604 |
25 Feb 19 |
nicklas |
var suffix = Data.get(selected, 'suffix') || ''; |
7604 |
25 Feb 19 |
nicklas |
84 |
|
7604 |
25 Feb 19 |
nicklas |
builder.encloseSelection(prefix, suffix); |
7604 |
25 Feb 19 |
nicklas |
list.selectedIndex = 0; |
7604 |
25 Feb 19 |
nicklas |
87 |
} |
7604 |
25 Feb 19 |
nicklas |
88 |
|
7604 |
25 Feb 19 |
nicklas |
builder.insertReporterListFunction = function() |
7604 |
25 Feb 19 |
nicklas |
90 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['expression']; |
7604 |
25 Feb 19 |
nicklas |
var listFunction = frm.listFunction.value; |
7604 |
25 Feb 19 |
nicklas |
var listId = frm.reporterList.value; |
7604 |
25 Feb 19 |
nicklas |
builder.encloseSelection(listFunction+'(' + listId + ')', ''); |
7604 |
25 Feb 19 |
nicklas |
frm.reporterList.selectedIndex = 0; |
7604 |
25 Feb 19 |
nicklas |
96 |
} |
7604 |
25 Feb 19 |
nicklas |
97 |
|
7604 |
25 Feb 19 |
nicklas |
builder.encloseSelection = function(prefix, suffix) |
7604 |
25 Feb 19 |
nicklas |
99 |
{ |
7604 |
25 Feb 19 |
nicklas |
100 |
|
7604 |
25 Feb 19 |
nicklas |
101 |
/* |
7604 |
25 Feb 19 |
nicklas |
Enclose the selected text with prefix and suffix. Code taken |
7604 |
25 Feb 19 |
nicklas |
from Trac: http://projects.edgewall.com/trac/ |
7604 |
25 Feb 19 |
nicklas |
104 |
*/ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['expression']; |
7604 |
25 Feb 19 |
nicklas |
var textarea = frm.expression; |
7604 |
25 Feb 19 |
nicklas |
textarea.focus(); |
7604 |
25 Feb 19 |
nicklas |
var start, end, sel, scrollPos, subst; |
7604 |
25 Feb 19 |
nicklas |
if (typeof(document["selection"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
110 |
{ |
7604 |
25 Feb 19 |
nicklas |
sel = document.selection.createRange().text; |
7604 |
25 Feb 19 |
nicklas |
112 |
} |
7604 |
25 Feb 19 |
nicklas |
else if (typeof(textarea["setSelectionRange"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
114 |
{ |
7604 |
25 Feb 19 |
nicklas |
start = textarea.selectionStart; |
7604 |
25 Feb 19 |
nicklas |
end = textarea.selectionEnd; |
7604 |
25 Feb 19 |
nicklas |
scrollPos = textarea.scrollTop; |
7604 |
25 Feb 19 |
nicklas |
sel = textarea.value.substring(start, end); |
7604 |
25 Feb 19 |
nicklas |
119 |
} |
7604 |
25 Feb 19 |
nicklas |
if (sel.match(/ $/)) |
7604 |
25 Feb 19 |
nicklas |
121 |
{ |
7604 |
25 Feb 19 |
nicklas |
// exclude ending space char, if any |
7604 |
25 Feb 19 |
nicklas |
sel = sel.substring(0, sel.length - 1); |
7604 |
25 Feb 19 |
nicklas |
suffix = suffix + " "; |
7604 |
25 Feb 19 |
nicklas |
125 |
} |
7604 |
25 Feb 19 |
nicklas |
subst = prefix + sel + suffix; |
7604 |
25 Feb 19 |
nicklas |
if (typeof(document["selection"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
128 |
{ |
7604 |
25 Feb 19 |
nicklas |
var range = document.selection.createRange().text = subst; |
7604 |
25 Feb 19 |
nicklas |
textarea.caretPos -= suffix.length; |
7604 |
25 Feb 19 |
nicklas |
131 |
} |
7604 |
25 Feb 19 |
nicklas |
else if (typeof(textarea["setSelectionRange"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
133 |
{ |
7604 |
25 Feb 19 |
nicklas |
textarea.value = textarea.value.substring(0, start) + subst + |
7604 |
25 Feb 19 |
nicklas |
textarea.value.substring(end); |
7604 |
25 Feb 19 |
nicklas |
if (sel) |
7604 |
25 Feb 19 |
nicklas |
137 |
{ |
7604 |
25 Feb 19 |
nicklas |
textarea.setSelectionRange(start + subst.length, start + subst.length); |
7604 |
25 Feb 19 |
nicklas |
139 |
} |
7604 |
25 Feb 19 |
nicklas |
else |
7604 |
25 Feb 19 |
nicklas |
141 |
{ |
7604 |
25 Feb 19 |
nicklas |
textarea.setSelectionRange(start + prefix.length, start + prefix.length); |
7604 |
25 Feb 19 |
nicklas |
143 |
} |
7604 |
25 Feb 19 |
nicklas |
textarea.scrollTop = scrollPos; |
7604 |
25 Feb 19 |
nicklas |
145 |
} |
7604 |
25 Feb 19 |
nicklas |
146 |
} |
7604 |
25 Feb 19 |
nicklas |
147 |
|
7604 |
25 Feb 19 |
nicklas |
builder.validateExpression = function() |
7604 |
25 Feb 19 |
nicklas |
149 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['expression']; |
7604 |
25 Feb 19 |
nicklas |
var expression = frm.expression.value; |
7604 |
25 Feb 19 |
nicklas |
152 |
|
7604 |
25 Feb 19 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7604 |
25 Feb 19 |
nicklas |
var url = 'expression_builder_ajax.jsp?ID='+App.getSessionId(); |
7604 |
25 Feb 19 |
nicklas |
url += '&cmd=ValidateExpression&expression=' + encodeURIComponent(expression); |
7604 |
25 Feb 19 |
nicklas |
url += '&formulatype='+Data.get('page-data', 'formula-type'); |
7604 |
25 Feb 19 |
nicklas |
url += '&rawdatatype='+Data.get('page-data', 'raw-data-type'); |
7604 |
25 Feb 19 |
nicklas |
url += '&channels='+Data.get('page-data', 'channels'); |
7604 |
25 Feb 19 |
nicklas |
159 |
|
7604 |
25 Feb 19 |
nicklas |
request.open("GET", url, false); |
7604 |
25 Feb 19 |
nicklas |
request.send(null); |
7604 |
25 Feb 19 |
nicklas |
162 |
|
7604 |
25 Feb 19 |
nicklas |
var response = JSON.parse(request.responseText); |
7604 |
25 Feb 19 |
nicklas |
var message; |
7604 |
25 Feb 19 |
nicklas |
if (response.status != 'ok') |
7604 |
25 Feb 19 |
nicklas |
166 |
{ |
7604 |
25 Feb 19 |
nicklas |
message = response.message; |
7604 |
25 Feb 19 |
nicklas |
message = message.replace('org.nfunk.jep.ParseException:', ''); |
7604 |
25 Feb 19 |
nicklas |
169 |
} |
7604 |
25 Feb 19 |
nicklas |
else |
7604 |
25 Feb 19 |
nicklas |
171 |
{ |
7604 |
25 Feb 19 |
nicklas |
message = 'The expression seems to be ok.'; |
7604 |
25 Feb 19 |
nicklas |
173 |
} |
7604 |
25 Feb 19 |
nicklas |
Forms.showNotification('btnValidate', message + '<br><span style="font-weight: normal;">Note! This feature is experimental and may not be 100% correct.</span>'); |
7604 |
25 Feb 19 |
nicklas |
175 |
|
7604 |
25 Feb 19 |
nicklas |
176 |
} |
7604 |
25 Feb 19 |
nicklas |
177 |
|
7604 |
25 Feb 19 |
nicklas |
return builder; |
7604 |
25 Feb 19 |
nicklas |
179 |
}(); |
7604 |
25 Feb 19 |
nicklas |
180 |
|
7604 |
25 Feb 19 |
nicklas |
Doc.onLoad(ExpressionBuilder.initPage); |
7604 |
25 Feb 19 |
nicklas |
Doc.addElementInitializer(ExpressionBuilder.initializeElements); |