2013-08-09 21:54:33 +02:00
/*globals $,OC,fileDownloadPath,t,document,odf,webodfEditor,alert,require,dojo,runtime */
2014-04-26 02:05:11 +03:00
$ . widget ( 'oc.documentGrid' , {
options : {
context : '.documentslist' ,
documents : { } ,
sessions : { } ,
members : { }
} ,
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
_create : function ( ) {
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
render : function ( ) {
var that = this ;
jQuery . when ( this . _load ( ) )
. then ( function ( ) {
that . _render ( ) ;
} ) ;
} ,
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
add : function ( document ) {
var docElem = $ ( this . options . context + ' .template' ) . clone ( ) ,
a = docElem . find ( 'a' )
;
//Fill an element
docElem . removeClass ( 'template' ) . attr ( 'data-id' , document . fileid ) ;
a . css ( 'background-image' , 'url("' + document . icon + '")' )
. attr ( 'href' , OC . generateUrl ( 'apps/files/download{file}' , { file : document . path } ) )
2014-09-24 22:29:38 +03:00
. attr ( 'original-title' , document . path )
2014-04-26 02:05:11 +03:00
. find ( 'label' ) . text ( document . name )
;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
docElem . appendTo ( this . options . context ) . show ( ) ;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
//Preview
var previewURL ,
urlSpec = {
file : document . path . replace ( /^\/\// , '/' ) ,
x : 200 ,
y : 200 ,
c : document . etag ,
forceIcon : 0
} ;
if ( $ ( '#isPublic' ) . length ) {
urlSpec . t = $ ( '#dirToken' ) . val ( ) ;
}
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
if ( ! urlSpec . x ) {
urlSpec . x = $ ( '#filestable' ) . data ( 'preview-x' ) ;
}
if ( ! urlSpec . y ) {
urlSpec . y = $ ( '#filestable' ) . data ( 'preview-y' ) ;
}
urlSpec . y *= window . devicePixelRatio ;
urlSpec . x *= window . devicePixelRatio ;
previewURL = OC . generateUrl ( '/core/preview.png?' ) + $ . param ( urlSpec ) ;
previewURL = previewURL . replace ( '(' , '%28' ) . replace ( ')' , '%29' ) ;
2015-10-29 19:05:36 +02:00
2015-08-28 20:49:50 +03:00
if ( $ ( '#previews_enabled' ) . length && document . hasPreview ) {
2015-03-26 01:03:08 +03:00
var img = new Image ( ) ;
img . onload = function ( ) {
var ready = function ( node ) {
return function ( path ) {
node . css ( 'background-image' , 'url("' + path + '")' ) ;
2015-10-29 19:05:36 +02:00
} ;
2015-03-26 01:03:08 +03:00
} ( a ) ;
ready ( previewURL ) ;
} ;
2015-01-15 00:43:39 +03:00
img . src = previewURL ;
}
2014-04-26 02:05:11 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
_load : function ( ) {
var that = this ;
var def = new $ . Deferred ( ) ;
$ . getJSON ( OC . generateUrl ( 'apps/documents/ajax/documents/list' ) )
. done ( function ( data ) {
that . options . documents = data . documents ;
that . options . sessions = data . sessions ;
that . options . members = data . members ;
def . resolve ( ) ;
} )
. fail ( function ( data ) {
console . log ( t ( 'documents' , 'Failed to load documents.' ) ) ;
} ) ;
return def ;
} ,
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
_render : function ( data ) {
var that = this ,
documents = data && data . documents || this . options . documents ,
sessions = data && data . sessions || this . options . sessions ,
members = data && data . members || this . options . members ,
hasDocuments = false
;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
$ ( this . options . context + ' .document:not(.template,.progress)' ) . remove ( ) ;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
$ . each ( documents , function ( i , document ) {
hasDocuments = true ;
that . add ( document ) ;
} ) ;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
$ . each ( sessions , function ( i , session ) {
if ( members [ session . es _id ] . length > 0 ) {
var docElem = $ ( that . options . context + ' .document[data-id="' + session . file _id + '"]' ) ;
if ( docElem . length > 0 ) {
docElem . attr ( 'data-esid' , session . es _id ) ;
docElem . find ( 'label' ) . after ( '<img class="svg session-active" src="' + OC . imagePath ( 'core' , 'places/contacts-dark' ) + '">' ) ;
docElem . addClass ( 'session' ) ;
} else {
console . log ( 'Could not find file ' + session . file _id + ' for session ' + session . es _id ) ;
}
}
} ) ;
2015-10-29 19:05:36 +02:00
2014-04-26 02:05:11 +03:00
if ( ! hasDocuments ) {
$ ( this . options . context ) . before ( '<div id="emptycontent">'
2014-07-03 12:29:50 +02:00
+ t ( 'documents' , 'No documents were found. Upload or create a document to get started!' )
2014-04-26 02:05:11 +03:00
+ '</div>'
) ;
} else {
$ ( '#emptycontent' ) . remove ( ) ;
}
}
} ) ;
2014-04-30 13:20:36 +03:00
$ . widget ( 'oc.documentOverlay' , {
options : {
parent : 'document.body'
} ,
_create : function ( ) {
$ ( this . element ) . hide ( ) . appendTo ( document . body ) ;
} ,
show : function ( ) {
$ ( this . element ) . fadeIn ( 'fast' ) ;
} ,
hide : function ( ) {
$ ( this . element ) . fadeOut ( 'fast' ) ;
}
} ) ;
2013-08-28 12:02:27 +02:00
var documentsMain = {
2013-09-04 20:58:31 +03:00
isEditormode : false ,
2013-08-18 12:54:57 +03:00
useUnstable : false ,
2013-09-25 16:34:35 +03:00
isGuest : false ,
2013-10-28 20:01:30 +03:00
memberId : false ,
esId : false ,
2013-10-22 16:18:03 +03:00
ready : false ,
2013-12-05 14:04:36 +01:00
fileName : null ,
2015-10-28 10:46:36 +01:00
baseName : null ,
url : null ,
2014-12-02 03:34:05 +03:00
canShare : false ,
toolbar : '<div id="ocToolbar"><div id="ocToolbarInside"></div><span id="toolbar" class="claro"></span></div>' ,
2015-10-29 19:05:36 +02:00
UI : {
2013-09-18 18:48:52 +03:00
/* Editor wrapper HTML */
2015-10-28 19:45:12 +02:00
container : '<div id="mainContainer" class="claro">' +
2015-10-23 17:17:35 +02:00
'</div>' ,
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
/* Previous window title */
mainTitle : '' ,
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
init : function ( ) {
documentsMain . UI . mainTitle = $ ( 'title' ) . text ( ) ;
} ,
2015-10-29 19:05:36 +02:00
2014-12-02 03:34:05 +03:00
showEditor : function ( title ) {
2014-04-25 20:21:42 +03:00
if ( documentsMain . isGuest ) {
// !Login page mess wih WebODF toolbars
2014-04-30 13:20:36 +03:00
$ ( document . body ) . attr ( 'id' , 'body-user' ) ;
2014-04-25 20:21:42 +03:00
}
2013-09-18 18:48:52 +03:00
$ ( document . body ) . addClass ( "claro" ) ;
$ ( document . body ) . prepend ( documentsMain . UI . container ) ;
2015-10-27 23:20:02 -04:00
2014-02-12 16:54:44 +03:00
$ ( 'title' ) . text ( title + ' - ' + documentsMain . UI . mainTitle ) ;
2015-10-28 15:01:48 +01:00
var viewer = window . location . protocol + '//' + window . location . host + '/cloudsuite/cloudsuite.html?' +
2015-10-28 10:46:36 +01:00
'file_path=' + documentsMain . url +
'&host=' + 'ws://' + window . location . hostname + ':9980' +
2015-10-28 15:01:48 +01:00
'&edit=' + 'false' +
'×tamp=' + '' ;
2015-10-27 23:20:02 -04:00
2015-10-30 14:36:28 +02:00
var frame = '<iframe id="loleafletframe" allowfullscreen style="width:100%;height:100%;position:absolute;" src="' + viewer + '" sandbox="allow-scripts allow-same-origin allow-popups allow-modals"/>' ;
2015-10-27 23:20:02 -04:00
2015-10-28 15:01:48 +01:00
$ ( '#mainContainer' ) . append ( frame ) ;
2015-10-27 23:20:02 -04:00
documentsMain . overlay . documentOverlay ( 'hide' ) ;
$ ( '#loleafletframe' ) . load ( function ( ) {
2015-10-28 15:01:48 +01:00
var iframe = $ ( '#loleafletframe' ) . contents ( ) ;
iframe . find ( '#tb_toolbar-up_item_close' ) . click ( function ( ) {
2015-10-29 20:36:31 -04:00
documentsMain . onClose ( ) ;
2015-10-28 15:01:48 +01:00
} ) ;
2015-10-27 23:20:02 -04:00
} ) ;
2013-09-18 18:48:52 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
hideEditor : function ( ) {
2014-04-30 13:20:36 +03:00
if ( documentsMain . isGuest ) {
// !Login page mess wih WebODF toolbars
$ ( document . body ) . attr ( 'id' , 'body-login' ) ;
2014-12-02 03:34:05 +03:00
$ ( 'footer,nav' ) . show ( ) ;
2014-04-30 13:20:36 +03:00
}
2015-10-29 19:05:36 +02:00
2014-04-30 13:20:36 +03:00
// Fade out editor
$ ( '#mainContainer' ) . fadeOut ( 'fast' , function ( ) {
$ ( '#mainContainer' ) . remove ( ) ;
2015-06-11 01:31:01 +03:00
$ ( '#content-wrapper' ) . fadeIn ( 'fast' ) ;
2014-04-30 13:20:36 +03:00
$ ( document . body ) . removeClass ( 'claro' ) ;
$ ( 'title' ) . text ( documentsMain . UI . mainTitle ) ;
} ) ;
2013-11-12 19:16:05 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-01-21 21:42:52 +03:00
showSave : function ( ) {
$ ( '#odf-close' ) . hide ( ) ;
$ ( '#saving-document' ) . show ( ) ;
} ,
2015-10-29 19:05:36 +02:00
2014-01-21 21:42:52 +03:00
hideSave : function ( ) {
$ ( '#saving-document' ) . hide ( ) ;
$ ( '#odf-close' ) . show ( ) ;
} ,
2015-10-29 19:05:36 +02:00
2013-12-06 03:12:58 +03:00
showProgress : function ( message ) {
if ( ! message ) {
message = ' ' ;
}
$ ( '.documentslist .progress div' ) . text ( message ) ;
2013-11-12 19:16:05 +03:00
$ ( '.documentslist .progress' ) . show ( ) ;
} ,
2015-10-29 19:05:36 +02:00
2013-11-12 19:16:05 +03:00
hideProgress : function ( ) {
$ ( '.documentslist .progress' ) . hide ( ) ;
2013-11-14 19:02:46 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-11-14 19:02:46 +03:00
showLostConnection : function ( ) {
$ ( '#memberList .memberListButton' ) . css ( { opacity : 0.3 } ) ;
2014-12-02 03:34:05 +03:00
$ ( '#ocToolbar' ) . children ( ':not(#document-title)' ) . hide ( ) ;
2013-11-14 19:02:46 +03:00
$ ( '<div id="connection-lost"></div>' ) . prependTo ( '#memberList' ) ;
2014-12-02 03:34:05 +03:00
$ ( '<div id="warning-connection-lost">' + t ( 'documents' , 'No connection to server. Trying to reconnect.' ) + '<img src="' + OC . imagePath ( 'core' , 'loading-dark.gif' ) + '" alt="" /></div>' ) . prependTo ( '#ocToolbar' ) ;
2013-11-14 19:02:46 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-11-14 19:02:46 +03:00
hideLostConnection : function ( ) {
$ ( '#connection-lost,#warning-connection-lost' ) . remove ( ) ;
2014-12-02 03:34:05 +03:00
$ ( '#ocToolbar' ) . children ( ':not(#document-title,#saving-document)' ) . show ( ) ;
2013-11-14 19:02:46 +03:00
$ ( '#memberList .memberListButton' ) . css ( { opacity : 1 } ) ;
2014-04-02 23:18:39 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-04-02 23:18:39 +03:00
notify : function ( message ) {
OC . Notification . show ( message ) ;
2014-04-07 17:18:14 +03:00
setTimeout ( OC . Notification . hide , 10000 ) ;
2013-09-18 18:48:52 +03:00
}
} ,
2015-10-29 19:05:36 +02:00
2013-07-12 16:00:07 +02:00
onStartup : function ( ) {
2013-10-22 16:18:03 +03:00
var fileId ;
2013-10-10 21:27:19 +03:00
documentsMain . useUnstable = $ ( '#webodf-unstable' ) . val ( ) === 'true' ;
2013-09-18 18:48:52 +03:00
documentsMain . UI . init ( ) ;
2015-10-29 19:05:36 +02:00
2013-09-25 16:34:35 +03:00
if ( ! OC . currentUser ) {
documentsMain . isGuest = true ;
2015-10-29 19:05:36 +02:00
2014-03-14 20:49:17 +03:00
if ( $ ( "[name='document']" ) . val ( ) ) {
2014-12-02 03:34:05 +03:00
$ ( documentsMain . toolbar ) . appendTo ( '#header' ) ;
2014-03-14 20:49:17 +03:00
documentsMain . prepareSession ( ) ;
documentsMain . joinSession (
$ ( "[name='document']" ) . val ( )
) ;
}
2013-09-25 16:34:35 +03:00
} else {
// Does anything indicate that we need to autostart a session?
2013-10-22 16:18:03 +03:00
fileId = parent . location . hash . replace ( /\W*/g , '' ) ;
2013-09-25 16:34:35 +03:00
}
2015-10-29 19:05:36 +02:00
2013-10-24 19:40:59 +03:00
documentsMain . show ( ) ;
2015-10-30 16:51:44 +01:00
2013-10-24 19:40:59 +03:00
if ( fileId ) {
2014-04-30 13:20:36 +03:00
documentsMain . overlay . documentOverlay ( 'show' ) ;
2015-10-30 16:51:44 +01:00
documentsMain . prepareSession ( ) ;
documentsMain . joinSession ( fileId ) ;
2013-09-18 23:41:43 +03:00
}
2015-10-29 19:05:36 +02:00
2015-10-29 20:41:54 -04:00
documentsMain . ready = true ;
2013-07-12 16:00:07 +02:00
} ,
2015-10-29 19:05:36 +02:00
2013-09-17 18:06:37 +03:00
prepareSession : function ( ) {
documentsMain . isEditorMode = true ;
2014-04-30 13:20:36 +03:00
documentsMain . overlay . documentOverlay ( 'show' ) ;
2013-10-24 19:23:59 +03:00
$ ( window ) . on ( 'beforeunload' , function ( ) {
2015-10-29 19:05:36 +02:00
return t ( 'documents' , "Leaving this page in Editor mode might cause unsaved data. It is recommended to use 'Close' button instead." ) ;
2013-10-24 19:23:59 +03:00
} ) ;
2014-03-21 20:14:57 +03:00
$ ( window ) . on ( "unload" , documentsMain . onTerminate ) ;
2013-09-18 18:48:52 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-09-18 18:48:52 +03:00
prepareGrid : function ( ) {
documentsMain . isEditorMode = false ;
2014-04-30 13:20:36 +03:00
documentsMain . overlay . documentOverlay ( 'hide' ) ;
2013-09-17 18:06:37 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-08-10 01:49:20 +03:00
initSession : function ( response ) {
2013-12-05 01:02:08 +03:00
if ( response && ( response . id && ! response . es _id ) ) {
return documentsMain . view ( response . id ) ;
2014-04-25 20:21:42 +03:00
}
2015-10-29 19:05:36 +02:00
2014-12-02 03:34:05 +03:00
$ ( 'footer,nav' ) . hide ( ) ;
$ ( documentsMain . toolbar ) . appendTo ( '#header' ) ;
2015-10-29 19:05:36 +02:00
2013-12-05 01:02:08 +03:00
if ( ! response || ! response . status || response . status === 'error' ) {
2014-01-21 22:23:45 +03:00
documentsMain . onEditorShutdown ( t ( 'documents' , 'Failed to load this document. Please check if it can be opened with an external odt editor. This might also mean it has been unshared or deleted recently.' ) ) ;
2013-08-19 14:18:48 +03:00
return ;
}
2015-10-29 19:05:36 +02:00
//Wait for 3 sec if editor is still loading
2013-10-22 16:18:03 +03:00
if ( ! documentsMain . ready ) {
2013-10-24 19:23:59 +03:00
setTimeout ( function ( ) { documentsMain . initSession ( response ) ; } , 3000 ) ;
2013-10-22 16:18:03 +03:00
console . log ( 'Waiting for the editor to start...' ) ;
return ;
}
2013-08-07 02:35:46 +03:00
2015-10-29 19:05:36 +02:00
var pollUrl = documentsMain . isGuest
2015-09-18 00:15:18 +03:00
? OC . generateUrl ( 'apps/documents/session/guest/poll/{token}' , { 'token' : $ ( "[name='document']" ) . val ( ) } )
: OC . generateUrl ( 'apps/documents/session/user/poll' ) ,
2015-10-29 19:05:36 +02:00
saveUrl = documentsMain . isGuest
2015-09-18 00:15:18 +03:00
? OC . generateUrl ( 'apps/documents/session/guest/save/{token}' , { 'token' : $ ( "[name='document']" ) . val ( ) } )
: OC . generateUrl ( 'apps/documents/session/user/save' )
;
2014-12-02 03:34:05 +03:00
documentsMain . canShare = ! documentsMain . isGuest
&& typeof OC . Share !== 'undefined' && response . permissions & OC . PERMISSION _SHARE ;
2015-10-26 12:16:44 +01:00
/*require({ }, ["owncloud/ServerFactory", "webodf/editor/Editor"], function (ServerFactory, Editor) {*/
2013-08-13 19:38:30 +03:00
// fade out file list and show WebODF canvas
2015-06-11 01:31:01 +03:00
$ ( '#content-wrapper' ) . fadeOut ( 'fast' ) . promise ( ) . done ( function ( ) {
2015-10-29 19:05:36 +02:00
2013-12-05 14:04:36 +01:00
documentsMain . fileId = response . file _id ;
2014-06-04 02:08:31 +03:00
documentsMain . fileName = response . title ;
2015-10-29 19:05:36 +02:00
2015-10-28 10:46:36 +01:00
documentsMain . esId = response . es _id ;
documentsMain . memberId = response . member _id ;
documentsMain . loadDocument ( ) ;
2014-03-14 20:49:17 +03:00
if ( documentsMain . isGuest ) {
$ ( '#odf-close' ) . text ( t ( 'documents' , 'Save' ) ) ;
2014-12-23 15:16:14 +01:00
$ ( '#odf-close' ) . removeClass ( 'icon-view-close' ) ;
2014-03-14 20:49:17 +03:00
}
2015-10-26 12:16:44 +01:00
//var serverFactory = new ServerFactory();
/ *
2013-11-09 18:01:43 +01:00
// TODO: set webodf translation system, by passing a proper function translate(!string):!string in "runtime.setTranslator(translate);"
2015-09-18 00:15:18 +03:00
documentsMain . webodfServerInstance = serverFactory . createServer ( {
url : pollUrl ,
sessionStateToFileUrl : saveUrl
} ) ;
2013-09-02 17:38:35 +02:00
documentsMain . webodfServerInstance . setToken ( oc _requesttoken ) ;
2015-06-11 01:31:01 +03:00
documentsMain . webodfEditorInstance = new Editor (
{
allFeaturesEnabled : true ,
collabEditingEnabled : true
} ,
documentsMain . webodfServerInstance , serverFactory
) ;
2015-10-29 19:05:36 +02:00
2014-01-21 21:42:52 +03:00
documentsMain . webodfEditorInstance . addEventListener ( Editor . EVENT _BEFORESAVETOFILE , documentsMain . UI . showSave ) ;
documentsMain . webodfEditorInstance . addEventListener ( Editor . EVENT _SAVEDTOFILE , documentsMain . UI . hideSave ) ;
2014-01-21 22:23:45 +03:00
documentsMain . webodfEditorInstance . addEventListener ( Editor . EVENT _ERROR , documentsMain . onEditorShutdown ) ;
2014-01-21 22:47:23 +03:00
documentsMain . webodfEditorInstance . addEventListener ( Editor . EVENT _HASSESSIONHOSTCONNECTIONCHANGED , function ( has ) {
if ( has ) {
documentsMain . UI . hideLostConnection ( ) ;
} else {
documentsMain . UI . showLostConnection ( ) ;
}
} ) ;
2013-08-30 17:35:35 +02:00
// load the document and get called back when it's live
2013-10-28 20:01:30 +03:00
documentsMain . webodfEditorInstance . openSession ( documentsMain . esId , documentsMain . memberId , function ( ) {
2013-08-30 17:35:35 +02:00
documentsMain . webodfEditorInstance . startEditing ( ) ;
2014-04-30 13:20:36 +03:00
documentsMain . overlay . documentOverlay ( 'hide' ) ;
2013-09-18 23:41:43 +03:00
parent . location . hash = response . file _id ;
2013-08-30 17:35:35 +02:00
} ) ;
2015-10-26 12:16:44 +01:00
* /
2013-07-12 22:50:39 +02:00
} ) ;
2015-10-26 12:16:44 +01:00
/*});*/
2013-07-01 22:36:17 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-08-07 21:14:36 +03:00
2013-09-18 23:41:43 +03:00
joinSession : function ( fileId ) {
console . log ( 'joining session ' + fileId ) ;
2013-09-25 16:34:35 +03:00
var url ;
if ( documentsMain . isGuest ) {
2015-09-18 00:15:18 +03:00
url = OC . generateUrl ( 'apps/documents/session/guest/join/{token}' , { token : fileId } ) ;
2013-09-25 16:34:35 +03:00
} else {
2015-09-18 00:15:18 +03:00
url = OC . generateUrl ( 'apps/documents/session/user/join/{file_id}' , { file _id : fileId } ) ;
2013-09-25 16:34:35 +03:00
}
2013-08-22 16:43:14 +02:00
$ . post (
2013-09-25 16:34:35 +03:00
url ,
2013-09-27 19:33:04 +03:00
{ name : $ ( "[name='memberName']" ) . val ( ) } ,
2013-08-28 12:02:27 +02:00
documentsMain . initSession
2013-08-22 16:43:14 +02:00
) ;
2013-08-07 02:35:46 +03:00
} ,
2015-10-29 19:05:36 +02:00
2013-12-05 01:02:08 +03:00
view : function ( id ) {
OC . addScript ( 'documents' , 'viewer/viewer' , function ( ) {
documentsMain . prepareGrid ( ) ;
$ ( window ) . off ( 'beforeunload' ) ;
2014-03-21 20:14:57 +03:00
$ ( window ) . off ( 'unload' ) ;
2013-12-05 01:02:08 +03:00
var path = $ ( 'li[data-id=' + id + ']>a' ) . attr ( 'href' ) ;
odfViewer . isDocuments = true ;
odfViewer . onView ( path ) ;
} ) ;
} ,
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
onCreate : function ( event ) {
event . preventDefault ( ) ;
2013-11-13 19:50:29 +03:00
var docElem = $ ( '.documentslist .template' ) . clone ( ) ;
docElem . removeClass ( 'template' ) ;
docElem . addClass ( 'document' ) ;
docElem . insertAfter ( '.documentslist .template' ) ;
docElem . show ( ) ;
2013-09-13 13:18:45 +03:00
$ . post (
2014-03-02 23:42:38 +01:00
OC . generateUrl ( 'apps/documents/ajax/documents/create' ) ,
2013-09-13 13:18:45 +03:00
{ } ,
2014-03-17 23:48:15 +03:00
function ( response ) {
if ( response && response . fileid ) {
documentsMain . prepareSession ( ) ;
documentsMain . joinSession ( response . fileid ) ;
} else {
2014-04-02 23:18:39 +03:00
if ( response && response . message ) {
documentsMain . UI . notify ( response . message ) ;
}
2014-03-17 23:48:15 +03:00
documentsMain . show ( ) ;
}
}
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
) ;
} ,
2015-10-29 19:05:36 +02:00
2014-03-14 20:49:17 +03:00
changeNick : function ( memberId , name , node ) {
2014-10-23 23:08:55 +03:00
var url = OC . generateUrl ( 'apps/documents/ajax/user/rename' ) ;
$ . ajax ( {
url : url ,
type : "POST" ,
2015-10-29 19:05:36 +02:00
data : JSON . stringify ( {
2014-10-23 23:08:55 +03:00
name : name ,
memberId : memberId
} ) ,
contentType : 'application/json; charset=utf-8' ,
dataType : "json" ,
success : function ( result ) {
2014-03-14 20:49:17 +03:00
if ( result && result . status === 'error' ) {
if ( result . message ) {
2014-04-02 23:18:39 +03:00
documentsMain . UI . notify ( result . message ) ;
2014-03-14 20:49:17 +03:00
}
return ;
}
}
2014-10-23 23:08:55 +03:00
} ) ;
2014-03-14 20:49:17 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-03-14 20:49:17 +03:00
onNickChange : function ( memberId , fullNameNode ) {
if ( ! documentsMain . isGuest || memberId !== documentsMain . memberId ) {
return ;
}
if ( $ ( fullNameNode . parentNode ) . children ( 'input' ) . length !== 0 ) {
return ;
}
2015-10-29 19:05:36 +02:00
2014-03-14 20:49:17 +03:00
var input = $ ( '<input type="text"/>' ) . val ( $ ( fullNameNode ) . attr ( 'fullname' ) ) ;
$ ( fullNameNode . parentNode ) . append ( input ) ;
$ ( fullNameNode ) . hide ( ) ;
input . on ( 'blur' , function ( ) {
var newName = input . val ( ) ;
if ( ! newName || newName === name ) {
input . tipsy ( 'hide' ) ;
input . remove ( ) ;
$ ( fullNameNode ) . show ( ) ;
return ;
}
else {
try {
input . tipsy ( 'hide' ) ;
input . removeClass ( 'error' ) ;
input . tipsy ( 'hide' ) ;
input . remove ( ) ;
$ ( fullNameNode ) . show ( ) ;
documentsMain . changeNick ( memberId , newName , fullNameNode ) ;
}
catch ( error ) {
input . attr ( 'title' , error ) ;
input . tipsy ( { gravity : 'n' , trigger : 'manual' } ) ;
input . tipsy ( 'show' ) ;
input . addClass ( 'error' ) ;
}
}
} ) ;
input . on ( 'keyup' , function ( event ) {
if ( event . keyCode === 27 ) {
// cancel by putting in an empty value
$ ( this ) . val ( '' ) ;
$ ( this ) . blur ( ) ;
event . preventDefault ( ) ;
}
if ( event . keyCode === 13 ) {
$ ( this ) . blur ( ) ;
event . preventDefault ( ) ;
}
} ) ;
input . focus ( ) ;
input . selectRange ( 0 , name . length ) ;
} ,
2013-12-05 14:04:36 +01:00
2015-10-28 10:46:36 +01:00
loadDocument : function ( ) {
2015-10-30 16:51:44 +01:00
var url = OC . generateUrl ( 'apps/documents/load/{file_id}' , { file _id : documentsMain . fileId } ) ;
2015-10-28 10:46:36 +01:00
$ . post (
url ,
{ } ,
function ( result ) {
if ( result && result . status === 'error' ) {
if ( result . message ) {
documentsMain . IU . notify ( result . message ) ;
}
documentsMain . onEditorShutdown ( t ( 'documents' , 'Failed to load this document. Please check if it can be opened with an external odt editor. This might also mean it has been unshared or deleted recently.' ) ) ;
return ;
}
documentsMain . url = 'file://' + result . filename ;
documentsMain . baseName = result . basename ;
documentsMain . UI . showEditor ( documentsMain . fileName ) ;
}
) ;
} ,
2013-12-05 14:04:36 +01:00
renameDocument : function ( name ) {
2014-03-02 23:42:38 +01:00
var url = OC . generateUrl ( 'apps/documents/ajax/documents/rename/{file_id}' , { file _id : documentsMain . fileId } ) ;
2013-12-05 14:04:36 +01:00
$ . post (
url ,
{ name : name } ,
function ( result ) {
if ( result && result . status === 'error' ) {
if ( result . message ) {
2014-04-02 23:18:39 +03:00
documentsMain . IU . notify ( result . message ) ;
2013-12-05 14:04:36 +01:00
}
return ;
}
documentsMain . fileName = name ;
$ ( 'title' ) . text ( documentsMain . UI . mainTitle + '| ' + name ) ;
2014-12-02 03:34:05 +03:00
$ ( '#document-title' ) . text ( name ) ;
2013-12-05 14:04:36 +01:00
}
) ;
} ,
2014-01-21 22:23:45 +03:00
onEditorShutdown : function ( message ) {
OC . Notification . show ( message ) ;
$ ( window ) . off ( 'beforeunload' ) ;
2014-03-21 20:14:57 +03:00
$ ( window ) . off ( 'unload' ) ;
2014-01-21 22:23:45 +03:00
if ( documentsMain . isEditorMode ) {
documentsMain . isEditorMode = false ;
parent . location . hash = "" ;
} else {
setTimeout ( OC . Notification . hide , 7000 ) ;
}
documentsMain . prepareGrid ( ) ;
2015-10-29 20:46:57 -04:00
documentsMain . UI . hideEditor ( ) ;
2015-10-29 19:05:36 +02:00
2014-01-21 22:23:45 +03:00
documentsMain . show ( ) ;
2014-12-02 03:34:05 +03:00
$ ( 'footer,nav' ) . show ( ) ;
2014-03-21 20:14:57 +03:00
} ,
2015-10-29 19:05:36 +02:00
2014-01-21 22:23:45 +03:00
2013-07-01 22:36:17 +03:00
onClose : function ( ) {
2013-09-08 11:43:38 +03:00
if ( ! documentsMain . isEditorMode ) {
return ;
}
documentsMain . isEditorMode = false ;
2013-09-27 19:54:31 +03:00
$ ( window ) . off ( 'beforeunload' ) ;
2014-03-21 20:14:57 +03:00
$ ( window ) . off ( 'unload' ) ;
2013-09-17 18:06:37 +03:00
parent . location . hash = "" ;
2013-09-18 18:48:52 +03:00
2015-10-29 20:32:59 -04:00
$ ( 'footer,nav' ) . show ( ) ;
documentsMain . UI . hideEditor ( ) ;
2015-10-29 21:52:52 -04:00
$ ( '#ocToolbar' ) . remove ( ) ;
2015-10-29 20:32:59 -04:00
documentsMain . show ( ) ;
2013-08-28 16:51:17 +02:00
} ,
2015-10-29 19:05:36 +02:00
2014-03-21 20:14:57 +03:00
onTerminate : function ( ) {
var url = '' ;
if ( documentsMain . isGuest ) {
url = OC . generateUrl ( 'apps/documents/ajax/user/disconnectGuest/{member_id}' , { member _id : documentsMain . memberId } ) ;
} else {
url = OC . generateUrl ( 'apps/documents/ajax/user/disconnect/{member_id}' , { member _id : documentsMain . memberId } ) ;
}
$ . ajax ( {
type : "POST" ,
url : url ,
data : { esId : documentsMain . esId } ,
dataType : "json" ,
async : false // Should be sync to complete before the page is closed
} ) ;
if ( documentsMain . isGuest ) {
2014-12-02 03:34:05 +03:00
$ ( 'footer,nav' ) . show ( ) ;
2014-03-21 20:14:57 +03:00
}
} ,
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
show : function ( ) {
2013-09-25 16:34:35 +03:00
if ( documentsMain . isGuest ) {
2013-09-23 22:56:27 +03:00
return ;
}
2013-12-06 03:12:58 +03:00
documentsMain . UI . showProgress ( t ( 'documents' , 'Loading documents...' ) ) ;
2014-04-26 02:05:11 +03:00
documentsMain . docs . documentGrid ( 'render' ) ;
documentsMain . UI . hideProgress ( ) ;
2013-07-01 22:36:17 +03:00
}
} ;
2013-10-22 16:18:03 +03:00
//init
2014-02-05 11:48:23 +03:00
var Files = Files || {
// FIXME: copy/pasted from Files.isFileNameValid, needs refactor into core
isFileNameValid : function ( name ) {
if ( name === '.' ) {
throw t ( 'files' , '\'.\' is an invalid file name.' ) ;
} else if ( name . length === 0 ) {
throw t ( 'files' , 'File name cannot be empty.' ) ;
}
// check for invalid characters
var invalid _characters = [ '\\' , '/' , '<' , '>' , ':' , '"' , '|' , '?' , '*' ] ;
for ( var i = 0 ; i < invalid _characters . length ; i ++ ) {
if ( name . indexOf ( invalid _characters [ i ] ) !== - 1 ) {
throw t ( 'files' , "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed." ) ;
}
}
return true ;
} ,
2015-10-29 19:05:36 +02:00
2014-02-05 11:48:23 +03:00
updateStorageStatistics : function ( ) { }
2014-03-10 22:36:26 +03:00
} ,
FileList = FileList || { } ;
FileList . getCurrentDirectory = function ( ) {
return $ ( '#dir' ) . val ( ) || '/' ;
2014-02-05 11:48:23 +03:00
} ;
2015-10-29 14:23:32 -04:00
FileList . highlightFiles = function ( files , highlightFunction ) {
} ;
2015-10-31 18:21:59 -04:00
FileList . findFile = function ( filename ) {
var documents = documentsMain . docs . documentGrid ( 'option' ) . documents ;
return _ . find ( documents , function ( aFile ) {
return ( aFile . name === filename ) ;
} ) || false ;
} ;
2013-07-01 22:36:17 +03:00
$ ( document ) . ready ( function ( ) {
2015-10-31 18:21:59 -04:00
if ( ! OCA . Files ) {
OCA . Files = { } ;
OCA . Files . App = { } ;
OCA . Files . App . fileList = FileList ;
}
if ( ! OC . Share ) {
OC . Share = { } ;
}
2014-04-26 02:05:11 +03:00
documentsMain . docs = $ ( '.documentslist' ) . documentGrid ( ) ;
2014-04-30 13:20:36 +03:00
documentsMain . overlay = $ ( '<div id="documents-overlay" class="icon-loading"></div><div id="documents-overlay-below" class="icon-loading-dark"></div>' ) . documentOverlay ( ) ;
2015-10-29 19:05:36 +02:00
2014-09-24 22:29:38 +03:00
$ ( 'li.document a' ) . tipsy ( { fade : true , live : true } ) ;
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
$ ( '.documentslist' ) . on ( 'click' , 'li:not(.add-document)' , function ( event ) {
2013-07-01 22:36:17 +03:00
event . preventDefault ( ) ;
2013-10-22 16:18:03 +03:00
2013-09-04 20:58:31 +03:00
if ( documentsMain . isEditorMode ) {
return ;
}
2015-10-29 19:05:36 +02:00
2013-09-17 18:10:10 +03:00
documentsMain . prepareSession ( ) ;
2013-09-18 23:41:43 +03:00
if ( $ ( this ) . attr ( 'data-id' ) ) {
documentsMain . joinSession ( $ ( this ) . attr ( 'data-id' ) ) ;
2013-08-18 17:11:22 +03:00
}
2013-07-01 22:36:17 +03:00
} ) ;
2015-10-29 19:05:36 +02:00
2013-09-13 13:18:45 +03:00
$ ( '.add-document' ) . on ( 'click' , '.add' , documentsMain . onCreate ) ;
2013-08-19 14:18:48 +03:00
2013-09-13 17:04:52 +03:00
var file _upload _start = $ ( '#file_upload_start' ) ;
2014-03-12 19:41:21 +03:00
if ( typeof supportAjaxUploadWithProgress !== 'undefined' && supportAjaxUploadWithProgress ( ) ) {
2014-03-10 22:36:26 +03:00
file _upload _start . on ( 'fileuploadstart' , function ( e , data ) {
2014-03-14 14:52:29 +01:00
$ ( '#upload' ) . addClass ( 'icon-loading' ) ;
2014-04-25 20:21:42 +03:00
$ ( '.add-document .upload' ) . css ( { opacity : 0 } ) ;
2014-03-10 22:36:26 +03:00
} ) ;
}
file _upload _start . on ( 'fileuploaddone' , function ( ) {
2014-03-14 14:52:29 +01:00
$ ( '#upload' ) . removeClass ( 'icon-loading' ) ;
2014-04-25 20:21:42 +03:00
$ ( '.add-document .upload' ) . css ( { opacity : 0.7 } ) ;
2014-03-10 22:36:26 +03:00
documentsMain . show ( ) ;
} ) ;
2015-10-26 13:07:30 +01:00
2015-10-29 20:41:54 -04:00
documentsMain . onStartup ( ) ;
2013-09-12 23:02:56 +03:00
} ) ;