Open Office Editor W.I.P:

- Implement create new document
- Add select file dialog for saving a newly created doc
This commit is contained in:
Hadi Nategh 2016-07-20 15:35:25 +02:00 committed by Ralf Becker
parent 6e1bee6987
commit f9e7a06bca
3 changed files with 76 additions and 38 deletions

BIN
api/js/webodf/template.odt Normal file

Binary file not shown.

View File

@ -36,6 +36,7 @@ app.classes.filemanager = AppJS.extend(
*/ */
editor: {}, editor: {},
editor_mime: RegExp(/application\/vnd\.oasis\.opendocument\.text/),
/** /**
* Constructor * Constructor
* *
@ -1032,8 +1033,10 @@ app.classes.filemanager = AppJS.extend(
*/ */
_init_odf_editor: function () _init_odf_editor: function ()
{ {
var file_path = this.et2.getArrayMgr('content').getEntry('file_path'); var widgetFilePath = this.et2.getWidgetById('file_path'),
var self = this; file_path = widgetFilePath.value,
isNew = file_path == '/api/js/webodf/template.odt'? true: false,
self = this;
var onEditorCreated = function (err ,editor) var onEditorCreated = function (err ,editor)
{ {
@ -1044,10 +1047,17 @@ app.classes.filemanager = AppJS.extend(
} }
self.editor = editor; self.editor = editor;
self.editor.openDocumentFromUrl(egw.webserverUrl+file_path); self.editor.openDocumentFromUrl(egw.webserverUrl+file_path);
if (isNew) {
widgetFilePath.set_value('');
}
}; };
var editorOptions = { var editorOptions = {
allFeaturesEnabled: true allFeaturesEnabled: true,
userData: {
fullName: egw.user('account_fullName'),
color: 'blue'
}
}; };
var editor = this.et2.getWidgetById('odfEditor'); var editor = this.et2.getWidgetById('odfEditor');
@ -1062,9 +1072,11 @@ app.classes.filemanager = AppJS.extend(
* *
* @param {object} _egwAction egw action object * @param {object} _egwAction egw action object
*/ */
editor_close: function (_egwAction) { editor_close: function (_egwAction, _callback) {
var self = this; var self = this,
var action = _egwAction.id; action = _egwAction.id,
callback = _callback;
if (this.editor) if (this.editor)
{ {
var closeFn = function () var closeFn = function ()
@ -1075,6 +1087,7 @@ app.classes.filemanager = AppJS.extend(
self.editor.destroy(function(){}); self.editor.destroy(function(){});
window.close(); window.close();
} }
callback.call(this);
} }
// warn user about unsaved changes // warn user about unsaved changes
@ -1109,34 +1122,33 @@ app.classes.filemanager = AppJS.extend(
* @todo: creating new empty odt file * @todo: creating new empty odt file
*/ */
editor_new: function (_egwAction) { editor_new: function (_egwAction) {
return egw(window).message('Sorry creating new odt document is not fully implemented yet. Please try later.'); var self = this,
var mimeType = 'application/vnd.oasis.opendocument.text'; template_url = '/api/js/webodf/template.odt';
var bytes = new Uint8Array('');
var blob = new Blob([bytes.buffer], {type:mimeType}); if (Object.keys(this.editor).length > 0)
var egwAction = _egwAction; {
var self = this; this.editor_close(_egwAction, function(){
this.editor_file_operation({ self.editor.openDocumentFromUrl(egw.webserverUrl+template_url);
url: egw.webserverUrl+'/webdav.php?/home/'+egw.user('account_lid')+'/'+this.et2._inst.etemplate_exec_id+'.odt', self.et2.getWidgetById('file_path').set_value('');
method: 'PUT', });
success: function(_data) { }
egw(window).message(''); else
self.editor_close(egwAction); {
}, egw.open_link(egw.link('/index.php', {
error: function (_err) { menuaction: 'filemanager.filemanager_ui.editor',
egw(window).message('Create new document faild because of %1', _err); path: template_url,
}, isNew: true,
data: blob, }), '', '800x600');
processData: false, }
mimeType: mimeType
});
}, },
/** /**
* Method call for saving edited document * Method call for saving edited document
*/ */
editor_save: function () { editor_save: function () {
var self = this; var self = this,
var file_path = this.et2.getArrayMgr('content').getEntry('file_path'); widgetFilePath = this.et2.getWidgetById('file_path'),
file_path = widgetFilePath.value;
if (this.editor) if (this.editor)
{ {
@ -1146,9 +1158,8 @@ app.classes.filemanager = AppJS.extend(
return; return;
} }
var mimetype = "application/vnd.oasis.opendocument.text", var filename = file_path.split('/webdav.php'),
filename = file_path.split('/webdav.php'), blob = new Blob([data.buffer], {type: self.editor_mime});
blob = new Blob([data.buffer], {type: mimetype});
self.editor_file_operation({ self.editor_file_operation({
url: egw.webserverUrl+file_path, url: egw.webserverUrl+file_path,
@ -1157,14 +1168,40 @@ app.classes.filemanager = AppJS.extend(
success: function(data) { success: function(data) {
egw(window).message(egw.lang('Document %1 successfully has been saved.', filename[1])); egw(window).message(egw.lang('Document %1 successfully has been saved.', filename[1]));
self.editor.setDocumentModified(false); self.editor.setDocumentModified(false);
}, },
error: function () {}, error: function () {},
data: blob, data: blob,
mimeType: mimetype mimeType: self.editor_mime
}); });
} }
this.editor.getDocumentAsByteArray(saveByteArrayLocally);
//existed file
if (file_path != '') {
this.editor.getDocumentAsByteArray(saveByteArrayLocally);
}
// new file
else
{
// create file selector
var vfs_select = et2_createWidget('vfs-select', {
id:'savefile',
mode: 'saveas',
button_caption:"",
button_label:"save",
value: "doc.odt"
}, this.et2);
// bind change handler for setting the selected path and calling save
jQuery(vfs_select.getDOMNode()).on('change', function (){
file_path = '/webdav.php'+vfs_select.get_value();
// Add odt extension if not exist
if (!file_path.match(/\.odt$/,'ig')) file_path += '.odt';
widgetFilePath.set_value(file_path);
self.editor.getDocumentAsByteArray(saveByteArrayLocally);
});
// start the file selector dialog
jQuery(vfs_select.getDOMNode()).click();
}
} }
}, },
@ -1173,7 +1210,7 @@ app.classes.filemanager = AppJS.extend(
* @param {type} _egwAction * @param {type} _egwAction
*/ */
editor_delete: function (_egwAction) { editor_delete: function (_egwAction) {
var fullpath = this.et2.getArrayMgr('content').getEntry('file_path'); var fullpath = this.et2.getWidgetById('file_path').value;
fullpath = fullpath.split('/webdav.php')[1]; fullpath = fullpath.split('/webdav.php')[1];
var selected = fullpath.split('/'); var selected = fullpath.split('/');
selected.pop(); selected.pop();
@ -1219,8 +1256,9 @@ app.classes.filemanager = AppJS.extend(
* @returns {boolean} returns true if is editable otherwise false * @returns {boolean} returns true if is editable otherwise false
*/ */
isEditable: function (_egwAction, _senders) { isEditable: function (_egwAction, _senders) {
var data = egw.dataGetUIDdata(_senders[0].id); var data = egw.dataGetUIDdata(_senders[0].id),
var mime = this.et2._inst.widgetContainer.getWidgetById('$row'); mime = this.et2._inst.widgetContainer.getWidgetById('$row');
return data.data.mime.match(mime.mime_odf_regex)?true:false; return data.data.mime.match(mime.mime_odf_regex)?true:false;
} }

View File

@ -11,7 +11,7 @@
<row height="45" class="dialogHeader"> <row height="45" class="dialogHeader">
<box height="30"> <box height="30">
<toolbar id="tools"/> <toolbar id="tools"/>
<description id="file_path"/> <textbox id="file_path" readonly="true"/>
</box> </box>
</row> </row>
<row> <row>