7604 |
25 Feb 19 |
nicklas |
/* $Id $ |
7604 |
25 Feb 19 |
nicklas |
2 |
------------------------------------------------------------------ |
7604 |
25 Feb 19 |
nicklas |
Copyright (C) 2013 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 EditFile = function() |
7604 |
25 Feb 19 |
nicklas |
27 |
{ |
7604 |
25 Feb 19 |
nicklas |
var editFile = {}; |
7604 |
25 Feb 19 |
nicklas |
var originalText = ''; |
7626 |
07 Mar 19 |
nicklas |
var originalCharset = ''; |
7604 |
25 Feb 19 |
nicklas |
var isModified = false; |
7604 |
25 Feb 19 |
nicklas |
32 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.initPage = function() |
7604 |
25 Feb 19 |
nicklas |
34 |
{ |
7604 |
25 Feb 19 |
nicklas |
editFile.loadFile(); |
7604 |
25 Feb 19 |
nicklas |
36 |
|
7604 |
25 Feb 19 |
nicklas |
// Save + Close buttons |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler('btnSave', editFile.save); |
7604 |
25 Feb 19 |
nicklas |
Buttons.addClickHandler('close', editFile.close); |
7604 |
25 Feb 19 |
nicklas |
40 |
|
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('filedata', 'scroll', editFile.updateLineNumbers); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('filedata', 'keydown', editFile.detectTab); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler('filedata', 'keyup', editFile.detectChanges); |
7626 |
07 Mar 19 |
nicklas |
Events.addEventHandler('filedata', 'blur', editFile.detectChangesFull); |
7626 |
07 Mar 19 |
nicklas |
Events.addEventHandler('charset', 'change', editFile.detectChangesFull); |
7604 |
25 Feb 19 |
nicklas |
46 |
} |
7604 |
25 Feb 19 |
nicklas |
47 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.loadFile = function() |
7604 |
25 Feb 19 |
nicklas |
49 |
{ |
7604 |
25 Feb 19 |
nicklas |
var path = Data.get('page-data', 'path'); |
7604 |
25 Feb 19 |
nicklas |
var url = 'download/-'+App.getSessionId()+'-'+path+'?'+(new Date().getTime()); |
7604 |
25 Feb 19 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7604 |
25 Feb 19 |
nicklas |
request.open("GET", url, true); |
7604 |
25 Feb 19 |
nicklas |
Ajax.setReadyStateHandler(request, editFile.onFileLoaded); |
7604 |
25 Feb 19 |
nicklas |
request.send(null); |
7604 |
25 Feb 19 |
nicklas |
56 |
} |
7604 |
25 Feb 19 |
nicklas |
57 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.onFileLoaded = function(request) |
7604 |
25 Feb 19 |
nicklas |
59 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['file']; |
7604 |
25 Feb 19 |
nicklas |
frm.filedata.value = request.responseText; |
7604 |
25 Feb 19 |
nicklas |
originalText = frm.filedata.value; |
7626 |
07 Mar 19 |
nicklas |
originalCharset = frm.charset.value; |
7604 |
25 Feb 19 |
nicklas |
editFile.setModified(false); |
7604 |
25 Feb 19 |
nicklas |
editFile.updateLineNumbers(); |
7604 |
25 Feb 19 |
nicklas |
66 |
} |
7604 |
25 Feb 19 |
nicklas |
67 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.setModified = function(modified) |
7604 |
25 Feb 19 |
nicklas |
69 |
{ |
7604 |
25 Feb 19 |
nicklas |
isModified = modified; |
7604 |
25 Feb 19 |
nicklas |
Doc.element('modified').innerHTML = isModified ? '*' : ''; |
7604 |
25 Feb 19 |
nicklas |
72 |
} |
7604 |
25 Feb 19 |
nicklas |
73 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.updateLineNumbers = function() |
7604 |
25 Feb 19 |
nicklas |
75 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['file']; |
7604 |
25 Feb 19 |
nicklas |
var totalScroll = frm.filedata.scrollHeight; |
7604 |
25 Feb 19 |
nicklas |
var topScroll = frm.filedata.scrollTop; |
7604 |
25 Feb 19 |
nicklas |
var textHeight = frm.filedata.offsetHeight; |
7604 |
25 Feb 19 |
nicklas |
var topLine = 1; |
7604 |
25 Feb 19 |
nicklas |
var scrollRemain = 0; |
7604 |
25 Feb 19 |
nicklas |
if (totalScroll > textHeight || topScroll > 0) |
7604 |
25 Feb 19 |
nicklas |
83 |
{ |
7604 |
25 Feb 19 |
nicklas |
var numLines = frm.filedata.value.split('\n').length; |
7604 |
25 Feb 19 |
nicklas |
var scrollPerLine = totalScroll / numLines; |
7604 |
25 Feb 19 |
nicklas |
topLine = 1 + Math.floor(topScroll / scrollPerLine); |
7604 |
25 Feb 19 |
nicklas |
scrollRemain = topScroll - scrollPerLine * (topLine - 1); |
7604 |
25 Feb 19 |
nicklas |
88 |
} |
7604 |
25 Feb 19 |
nicklas |
var lineNumberHTML = ''; |
7604 |
25 Feb 19 |
nicklas |
for (var i = topLine; i < topLine + 100; i++) |
7604 |
25 Feb 19 |
nicklas |
91 |
{ |
7604 |
25 Feb 19 |
nicklas |
lineNumberHTML += i + '<br>'; |
7604 |
25 Feb 19 |
nicklas |
93 |
} |
7604 |
25 Feb 19 |
nicklas |
var lineNumberElem = Doc.element('linenumbers'); |
7604 |
25 Feb 19 |
nicklas |
lineNumberElem.innerHTML = lineNumberHTML; |
7604 |
25 Feb 19 |
nicklas |
lineNumberElem.scrollTop = scrollRemain; |
7604 |
25 Feb 19 |
nicklas |
97 |
} |
7604 |
25 Feb 19 |
nicklas |
98 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.save = function() |
7604 |
25 Feb 19 |
nicklas |
100 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (isModified) |
7604 |
25 Feb 19 |
nicklas |
102 |
{ |
7604 |
25 Feb 19 |
nicklas |
editFile.saveFile(); |
7604 |
25 Feb 19 |
nicklas |
104 |
} |
7604 |
25 Feb 19 |
nicklas |
else |
7604 |
25 Feb 19 |
nicklas |
106 |
{ |
7604 |
25 Feb 19 |
nicklas |
editFile.closeWindow(false); |
7604 |
25 Feb 19 |
nicklas |
108 |
} |
7604 |
25 Feb 19 |
nicklas |
109 |
} |
7604 |
25 Feb 19 |
nicklas |
110 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.close = function() |
7604 |
25 Feb 19 |
nicklas |
112 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (isModified) |
7604 |
25 Feb 19 |
nicklas |
114 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (confirm('The file has been modified. Save changes?')) |
7604 |
25 Feb 19 |
nicklas |
116 |
{ |
7604 |
25 Feb 19 |
nicklas |
editFile.saveFile(); |
7604 |
25 Feb 19 |
nicklas |
118 |
} |
7604 |
25 Feb 19 |
nicklas |
119 |
} |
7604 |
25 Feb 19 |
nicklas |
editFile.closeWindow(false); |
7604 |
25 Feb 19 |
nicklas |
121 |
} |
7604 |
25 Feb 19 |
nicklas |
122 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.saveFile = function() |
7604 |
25 Feb 19 |
nicklas |
124 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['file']; |
7604 |
25 Feb 19 |
nicklas |
var url = 'ajax.jsp?ID='+App.getSessionId(); |
7604 |
25 Feb 19 |
nicklas |
url += '&cmd=StoreFile&item_id='+Data.get('page-data', 'file-id'); |
7604 |
25 Feb 19 |
nicklas |
url += '&size='+frm.filedata.value.length; |
7626 |
07 Mar 19 |
nicklas |
url += '&charset='+frm.charset.value; |
7604 |
25 Feb 19 |
nicklas |
var request = Ajax.getXmlHttpRequest(); |
7604 |
25 Feb 19 |
nicklas |
request.open("POST", url, true); |
7604 |
25 Feb 19 |
nicklas |
Ajax.setReadyStateHandler(request, editFile.onFileStored); |
7604 |
25 Feb 19 |
nicklas |
request.send(frm.filedata.value); |
7604 |
25 Feb 19 |
nicklas |
134 |
} |
7604 |
25 Feb 19 |
nicklas |
135 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.onFileStored = function(request) |
7604 |
25 Feb 19 |
nicklas |
137 |
{ |
7604 |
25 Feb 19 |
nicklas |
var response = JSON.parse(request.responseText); |
7604 |
25 Feb 19 |
nicklas |
if (response.status != 'ok') |
7604 |
25 Feb 19 |
nicklas |
140 |
{ |
7626 |
07 Mar 19 |
nicklas |
Forms.showNotification('btnSave', response.message, 'bigger-notify'); |
7604 |
25 Feb 19 |
nicklas |
return; |
7604 |
25 Feb 19 |
nicklas |
143 |
} |
7604 |
25 Feb 19 |
nicklas |
editFile.setModified(false); |
7604 |
25 Feb 19 |
nicklas |
editFile.closeWindow(true, response.message); |
7604 |
25 Feb 19 |
nicklas |
146 |
} |
7604 |
25 Feb 19 |
nicklas |
147 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.closeWindow = function(refreshParent, message) |
7604 |
25 Feb 19 |
nicklas |
149 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (refreshParent) |
7604 |
25 Feb 19 |
nicklas |
151 |
{ |
7604 |
25 Feb 19 |
nicklas |
var url = App.getRoot() + 'common/close_popup.jsp?refresh_opener=1'; |
7604 |
25 Feb 19 |
nicklas |
if (message) url += '&message='+encodeURIComponent(message); |
7604 |
25 Feb 19 |
nicklas |
location.href = url; |
7604 |
25 Feb 19 |
nicklas |
155 |
} |
7604 |
25 Feb 19 |
nicklas |
else |
7604 |
25 Feb 19 |
nicklas |
157 |
{ |
7604 |
25 Feb 19 |
nicklas |
window.close(); |
7604 |
25 Feb 19 |
nicklas |
159 |
} |
7604 |
25 Feb 19 |
nicklas |
160 |
} |
7604 |
25 Feb 19 |
nicklas |
161 |
|
7626 |
07 Mar 19 |
nicklas |
editFile.detectChangesFull = function() |
7626 |
07 Mar 19 |
nicklas |
163 |
{ |
7626 |
07 Mar 19 |
nicklas |
var frm = document.forms['file']; |
7626 |
07 Mar 19 |
nicklas |
var modified = frm.filedata.value != originalText || frm.charset.value != originalCharset; |
7626 |
07 Mar 19 |
nicklas |
editFile.setModified(modified); |
7626 |
07 Mar 19 |
nicklas |
167 |
|
7626 |
07 Mar 19 |
nicklas |
Doc.addOrRemoveClass('charsetSection','utf8-warning', frm.charset.value != 'UTF-8' && frm.charset.value != originalCharset); |
7626 |
07 Mar 19 |
nicklas |
169 |
} |
7626 |
07 Mar 19 |
nicklas |
170 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.detectChanges = function() |
7604 |
25 Feb 19 |
nicklas |
172 |
{ |
7626 |
07 Mar 19 |
nicklas |
if (!isModified) editFile.detectChangesFull(); |
7604 |
25 Feb 19 |
nicklas |
174 |
} |
7604 |
25 Feb 19 |
nicklas |
175 |
|
7604 |
25 Feb 19 |
nicklas |
editFile.detectTab = function(event) |
7604 |
25 Feb 19 |
nicklas |
177 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (event.keyCode == 9) |
7604 |
25 Feb 19 |
nicklas |
179 |
{ |
7604 |
25 Feb 19 |
nicklas |
var frm = document.forms['file']; |
7604 |
25 Feb 19 |
nicklas |
editFile.replaceSelection(frm.filedata, '\t'); |
7604 |
25 Feb 19 |
nicklas |
event.preventDefault(); |
7604 |
25 Feb 19 |
nicklas |
return false; |
7604 |
25 Feb 19 |
nicklas |
184 |
} |
7604 |
25 Feb 19 |
nicklas |
return true; |
7604 |
25 Feb 19 |
nicklas |
186 |
} |
7604 |
25 Feb 19 |
nicklas |
187 |
|
7604 |
25 Feb 19 |
nicklas |
188 |
/* |
7604 |
25 Feb 19 |
nicklas |
Replace the selected text with the given text. Code based on |
7604 |
25 Feb 19 |
nicklas |
Trac: http://projects.edgewall.com/trac/ |
7604 |
25 Feb 19 |
nicklas |
191 |
*/ |
7604 |
25 Feb 19 |
nicklas |
editFile.replaceSelection = function(field, text) |
7604 |
25 Feb 19 |
nicklas |
193 |
{ |
7604 |
25 Feb 19 |
nicklas |
field.focus(); |
7604 |
25 Feb 19 |
nicklas |
var start, end, sel, scrollPos; |
7604 |
25 Feb 19 |
nicklas |
if (typeof(document["selection"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
197 |
{ |
7604 |
25 Feb 19 |
nicklas |
sel = document.selection.createRange().text; |
7604 |
25 Feb 19 |
nicklas |
199 |
} |
7604 |
25 Feb 19 |
nicklas |
else if (typeof(field["setSelectionRange"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
201 |
{ |
7604 |
25 Feb 19 |
nicklas |
start = field.selectionStart; |
7604 |
25 Feb 19 |
nicklas |
end = field.selectionEnd; |
7604 |
25 Feb 19 |
nicklas |
scrollPos = field.scrollTop; |
7604 |
25 Feb 19 |
nicklas |
sel = field.value.substring(start, end); |
7604 |
25 Feb 19 |
nicklas |
206 |
} |
7604 |
25 Feb 19 |
nicklas |
if (sel.match(/ $/)) |
7604 |
25 Feb 19 |
nicklas |
208 |
{ |
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 |
211 |
} |
7604 |
25 Feb 19 |
nicklas |
if (typeof(document["selection"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
213 |
{ |
7604 |
25 Feb 19 |
nicklas |
var range = document.selection.createRange().text = text; |
7604 |
25 Feb 19 |
nicklas |
215 |
} |
7604 |
25 Feb 19 |
nicklas |
else if (typeof(field["setSelectionRange"]) != "undefined") |
7604 |
25 Feb 19 |
nicklas |
217 |
{ |
7604 |
25 Feb 19 |
nicklas |
field.value = field.value.substring(0, start) + text + |
7604 |
25 Feb 19 |
nicklas |
field.value.substring(end); |
7604 |
25 Feb 19 |
nicklas |
field.setSelectionRange(start + text.length, start + text.length); |
7604 |
25 Feb 19 |
nicklas |
field.scrollTop = scrollPos; |
7604 |
25 Feb 19 |
nicklas |
222 |
} |
7604 |
25 Feb 19 |
nicklas |
223 |
} |
7604 |
25 Feb 19 |
nicklas |
224 |
|
7604 |
25 Feb 19 |
nicklas |
225 |
|
7604 |
25 Feb 19 |
nicklas |
226 |
|
7604 |
25 Feb 19 |
nicklas |
return editFile; |
7604 |
25 Feb 19 |
nicklas |
228 |
}(); |
7604 |
25 Feb 19 |
nicklas |
229 |
|
7604 |
25 Feb 19 |
nicklas |
Doc.onLoad(EditFile.initPage); |