2014-01-20 12:48:21 +01:00
package org.gcube.portlets.user.shareupdates.client.view ;
2013-06-02 16:26:58 +02:00
import java.util.ArrayList ;
2016-01-12 19:06:10 +01:00
import java.util.Iterator ;
import java.util.List ;
2013-06-02 16:26:58 +02:00
2013-01-26 20:02:51 +01:00
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapter ;
import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapterException ;
2016-01-19 14:35:51 +01:00
2013-01-26 20:02:51 +01:00
import org.gcube.portal.databook.shared.ClientFeed ;
import org.gcube.portal.databook.shared.ClientFeed.ClientFeedJsonizer ;
import org.gcube.portal.databook.shared.FeedType ;
import org.gcube.portal.databook.shared.PrivacyLevel ;
import org.gcube.portal.databook.shared.UserInfo ;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateService ;
import org.gcube.portlets.user.shareupdates.client.ShareUpdateServiceAsync ;
import org.gcube.portlets.user.shareupdates.shared.LinkPreview ;
2016-01-15 18:24:52 +01:00
import org.gcube.portlets.user.shareupdates.shared.UploadedFile ;
2013-04-11 18:46:58 +02:00
import org.gcube.portlets.user.shareupdates.shared.UserSettings ;
2016-04-28 19:06:17 +02:00
import org.gcube.portlets.widgets.fileupload.client.events.FileTooLargeEvent ;
import org.gcube.portlets.widgets.fileupload.client.events.FileTooLargeEventHandler ;
2014-01-17 19:21:35 +01:00
import org.gcube.portlets.widgets.fileupload.client.events.FileUploadCompleteEvent ;
import org.gcube.portlets.widgets.fileupload.client.events.FileUploadCompleteEventHandler ;
2016-01-12 19:06:10 +01:00
import org.gcube.portlets.widgets.fileupload.client.view.FileSubmit ;
2014-01-17 19:21:35 +01:00
import org.gcube.portlets.widgets.fileupload.client.view.UploadProgressPanel ;
2013-01-26 20:02:51 +01:00
import org.jsonmaker.gwt.client.Jsonizer ;
2016-01-12 19:06:10 +01:00
import com.github.gwtbootstrap.client.ui.Button ;
2016-04-28 19:06:17 +02:00
import com.github.gwtbootstrap.client.ui.ListBox ;
import com.github.gwtbootstrap.client.ui.constants.Device ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.core.client.GWT ;
2015-12-21 18:33:23 +01:00
import com.google.gwt.dom.client.Document ;
2016-01-12 19:06:10 +01:00
import com.google.gwt.dom.client.Style.BorderStyle ;
import com.google.gwt.dom.client.Style.FontWeight ;
2014-03-11 14:56:41 +01:00
import com.google.gwt.dom.client.Style.Unit ;
2014-01-20 00:23:08 +01:00
import com.google.gwt.dom.client.Style.Visibility ;
2016-01-28 13:55:35 +01:00
import com.google.gwt.event.dom.client.ChangeEvent ;
import com.google.gwt.event.dom.client.ChangeHandler ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.event.dom.client.ClickEvent ;
2016-01-12 19:06:10 +01:00
import com.google.gwt.event.dom.client.DragLeaveEvent ;
import com.google.gwt.event.dom.client.DragLeaveHandler ;
import com.google.gwt.event.dom.client.DragOverEvent ;
import com.google.gwt.event.dom.client.DragOverHandler ;
2014-01-17 19:21:35 +01:00
import com.google.gwt.event.shared.HandlerManager ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.uibinder.client.UiBinder ;
2014-01-17 19:21:35 +01:00
import com.google.gwt.uibinder.client.UiFactory ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.uibinder.client.UiField ;
import com.google.gwt.uibinder.client.UiHandler ;
2016-04-28 19:06:17 +02:00
import com.google.gwt.user.client.Timer ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.user.client.Window ;
import com.google.gwt.user.client.rpc.AsyncCallback ;
import com.google.gwt.user.client.ui.Composite ;
2014-01-20 00:23:08 +01:00
import com.google.gwt.user.client.ui.FileUpload ;
2013-04-11 18:46:58 +02:00
import com.google.gwt.user.client.ui.HTMLPanel ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.user.client.ui.Image ;
2016-01-12 19:06:10 +01:00
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.user.client.ui.Widget ;
/ * *
2016-01-15 18:24:52 +01:00
* The main share update class .
2016-01-12 19:06:10 +01:00
* @author Massimiliano Assante at ISTI CNR
* @author Costantino Perciante at ISTI CNR
2013-01-26 20:02:51 +01:00
* /
public class ShareUpdateForm extends Composite {
2016-01-12 19:06:10 +01:00
//Create a remote service proxy to talk to the server-side Greeting service.
2013-01-26 20:02:51 +01:00
private final ShareUpdateServiceAsync shareupdateService = GWT
. create ( ShareUpdateService . class ) ;
final PageBusAdapter pageBusAdapter = new PageBusAdapter ( ) ;
2016-01-12 19:06:10 +01:00
2014-03-05 00:38:20 +01:00
// the label for all Vres/channels
private final static String ALL_VRES = " Share with: your Virtual Research Environments " ;
2015-07-06 15:11:58 +02:00
2016-01-15 18:24:52 +01:00
// maximum number of files that can be attached
private static final int MAX_NUMBER_ATTACHMENTS = 10 ;
2016-01-12 19:06:10 +01:00
// Labels
2014-10-07 16:41:56 +02:00
protected final static String SHARE_UPDATE_TEXT = " Share an update or a link, use “@” to mention and “#” to add a topic " ;
2013-01-26 20:02:51 +01:00
protected final static String ERROR_UPDATE_TEXT = " Looks like empty to me! " ;
2014-01-30 16:39:42 +01:00
public final static String NO_TEXT_FILE_SHARE = " _N0_73X7_SH4R3_ " ;
2013-01-26 20:02:51 +01:00
private final static String LISTBOX_LEVEL = " - " ;
2016-01-12 19:06:10 +01:00
public static final String DROP_FILE_HERE_TEXT = " Drop your file(s) here! " ;
public static final String ATTACHMENT_LOADED = " Attachment loaded! " ;
public static final String ATTACHMENT_NOT_LOADED = " Attachment not loaded! " ;
private static final String DELETE_LINK_PREVIEW = " The link preview will be removed. Would you like to continue? " ;
private static final String DELETE_ATTACHMENTS = " The attachment(s) will be removed. Would you like to continue? " ;
2016-01-15 18:24:52 +01:00
private static final String TOO_MUCH_ATTACHMENT_ALERT = " Sorry, but you cannot upload more than " + MAX_NUMBER_ATTACHMENTS + " attachments! " ;
2016-01-27 17:49:48 +01:00
private static final String WAIT_CURRENT_UPLOAD_FINISHING = " Please, wait the current upload to finish " ;
2016-01-12 19:06:10 +01:00
// image urls
2013-01-26 20:02:51 +01:00
public static final String loading = GWT . getModuleBaseURL ( ) + " ../images/avatarLoader.gif " ;
public static final String avatar_default = GWT . getModuleBaseURL ( ) + " ../images/Avatar_default.png " ;
2016-01-15 18:24:52 +01:00
public static final String attachImageUrl = GWT . getModuleBaseURL ( ) + " ../images/attach.png " ;
public static final String attachedDefaultImageUrl = GWT . getModuleBaseURL ( ) + " ../images/attachment_default.png " ;
public static final String loadedAttachmentImageUrl = GWT . getModuleBaseURL ( ) + " ../images/load.png " ;
public static final String notLoadedAttachmentImageUrl = GWT . getModuleBaseURL ( ) + " ../images/not_load.png " ;
2016-01-12 19:06:10 +01:00
// remember the previous text in the textarea (while handling drag and drop)
private static String previousText ;
// list of attachedFiles (both correctly or not correctly uploaded)
private List < AttachedFile > listOfAttachedFiles = new ArrayList < > ( ) ;
2015-07-06 15:11:58 +02:00
2014-01-17 19:21:35 +01:00
private HandlerManager eventBus = new HandlerManager ( null ) ;
2014-01-14 23:44:43 +01:00
2013-01-26 20:02:51 +01:00
private static ShareUpdateFormUiBinder uiBinder = GWT
. create ( ShareUpdateFormUiBinder . class ) ;
2016-01-12 19:06:10 +01:00
// The link previewer
2016-01-15 18:24:52 +01:00
private LinkPreviewer linkPreviewer ;
2013-01-26 20:02:51 +01:00
2016-01-12 19:06:10 +01:00
// panel that show the in progress upload of an attachment
2014-01-17 19:21:35 +01:00
private UploadProgressPanel uploadProgress ;
2013-01-26 20:02:51 +01:00
interface ShareUpdateFormUiBinder extends UiBinder < Widget , ShareUpdateForm > {
}
2016-01-12 19:06:10 +01:00
// this instance
2013-01-26 20:02:51 +01:00
private static ShareUpdateForm singleton ;
2016-01-15 18:24:52 +01:00
/ * *
* Get this ShareUpdateForm object
* @return
* /
2013-01-26 20:02:51 +01:00
public static ShareUpdateForm get ( ) {
return singleton ;
}
2016-01-12 19:06:10 +01:00
2013-04-11 18:46:58 +02:00
@UiField
HTMLPanel mainPanel ;
2016-01-12 19:06:10 +01:00
2013-06-02 16:26:58 +02:00
@UiField
2014-01-20 19:35:57 +01:00
Placeholder preview ;
2013-06-02 16:26:58 +02:00
@UiField
2016-04-28 19:06:17 +02:00
Button submitButtonScreen ;
2014-01-17 19:21:35 +01:00
2014-01-14 19:02:43 +01:00
@UiField
2016-04-28 19:06:17 +02:00
Button attachButtonScreen ;
@UiField
Button submitButtonPhoneTablet ;
@UiField
Button attachButtonPhoneTablet ;
2013-06-02 16:26:58 +02:00
@UiField
Image avatarImage ;
2016-01-12 19:06:10 +01:00
@UiField
SuperPosedTextArea shareTextArea ;
2013-06-02 16:26:58 +02:00
2014-03-05 00:38:20 +01:00
@UiField
2015-07-06 15:11:58 +02:00
ListBox privacyLevel = new ListBox ( ) ;
2014-03-05 00:38:20 +01:00
@UiField
2015-07-06 15:11:58 +02:00
ListBox notifyListbox = new ListBox ( ) ;
2013-01-26 20:02:51 +01:00
2016-01-15 18:24:52 +01:00
@UiField
SaveInWorkspaceBox saveInWorkspaceCheckbox ;
2016-01-12 19:06:10 +01:00
// requested user's information
2014-01-30 16:39:42 +01:00
private UserInfo myUserInfo ;
2016-01-12 19:06:10 +01:00
/ * *
* Constructor
* /
2013-01-26 20:02:51 +01:00
public ShareUpdateForm ( ) {
initWidget ( uiBinder . createAndBindUi ( this ) ) ;
singleton = this ;
2014-01-17 19:21:35 +01:00
bind ( ) ;
2013-01-26 20:02:51 +01:00
avatarImage . setUrl ( loading ) ;
shareTextArea . setText ( SHARE_UPDATE_TEXT ) ;
2015-07-06 15:11:58 +02:00
2016-04-28 19:06:17 +02:00
// set the style and on which devices we show the buttons
submitButtonScreen . addStyleName ( " upload-btn-m " ) ;
attachButtonScreen . addStyleName ( " upload-btn-m " ) ;
submitButtonScreen . setShowOn ( Device . DESKTOP ) ;
attachButtonScreen . setShowOn ( Device . DESKTOP ) ;
submitButtonScreen . setHideOn ( Device . TABLET ) ;
attachButtonScreen . setHideOn ( Device . TABLET ) ;
submitButtonScreen . setHideOn ( Device . PHONE ) ;
attachButtonScreen . setHideOn ( Device . PHONE ) ;
submitButtonPhoneTablet . setHideOn ( Device . DESKTOP ) ;
attachButtonPhoneTablet . setHideOn ( Device . DESKTOP ) ;
submitButtonPhoneTablet . setShowOn ( Device . TABLET ) ;
attachButtonPhoneTablet . setShowOn ( Device . TABLET ) ;
submitButtonPhoneTablet . setShowOn ( Device . PHONE ) ;
attachButtonPhoneTablet . setShowOn ( Device . PHONE ) ;
2016-01-12 19:06:10 +01:00
2013-04-11 18:46:58 +02:00
shareupdateService . getUserSettings ( new AsyncCallback < UserSettings > ( ) {
2013-01-26 20:02:51 +01:00
public void onFailure ( Throwable caught ) {
avatarImage . setUrl ( avatar_default ) ;
}
2013-04-11 18:46:58 +02:00
public void onSuccess ( UserSettings userSettings ) {
myUserInfo = userSettings . getUserInfo ( ) ;
avatarImage . getElement ( ) . getParentElement ( ) . setAttribute ( " href " , myUserInfo . getAccountURL ( ) ) ;
avatarImage . setUrl ( myUserInfo . getAvatarId ( ) ) ;
String singleVREName = " " ;
2015-07-06 15:11:58 +02:00
boolean notificationEmail = userSettings . isNotificationViaEmailEnabled ( ) ;
2013-04-11 18:46:58 +02:00
if ( myUserInfo . getOwnVREs ( ) . size ( ) > 1 ) {
2014-03-05 00:38:20 +01:00
privacyLevel . addItem ( ALL_VRES , PrivacyLevel . VRES . toString ( ) ) ;
2013-01-26 20:02:51 +01:00
for ( String vreId : myUserInfo . getOwnVREs ( ) . keySet ( ) )
2014-03-05 00:38:20 +01:00
privacyLevel . addItem ( LISTBOX_LEVEL + " Share with: " + myUserInfo . getOwnVREs ( ) . get ( vreId ) , vreId ) ;
2013-01-26 20:02:51 +01:00
}
else if ( myUserInfo . getOwnVREs ( ) . size ( ) = = 1 )
2013-04-11 18:46:58 +02:00
for ( String vreId : myUserInfo . getOwnVREs ( ) . keySet ( ) ) {
singleVREName = myUserInfo . getOwnVREs ( ) . get ( vreId ) ;
2014-03-05 00:38:20 +01:00
privacyLevel . addItem ( LISTBOX_LEVEL + " Share with: " + singleVREName , vreId ) ;
2013-04-11 18:46:58 +02:00
}
2014-01-17 19:21:35 +01:00
2014-10-02 19:47:15 +02:00
if ( myUserInfo . isAdmin ( ) )
2014-03-05 00:38:20 +01:00
privacyLevel . addItem ( " Share with: Everyone " , PrivacyLevel . PORTAL . toString ( ) ) ;
2016-01-12 19:06:10 +01:00
2013-04-11 18:46:58 +02:00
//change css if deployed in VRE scope
if ( ! userSettings . isInfrastructure ( ) ) {
2015-07-07 18:37:25 +02:00
notifyListbox . addItem ( " Share with: " + singleVREName ) ;
notifyListbox . addItem ( " Share with: " + singleVREName + " + Notification to members " ) ;
2015-07-06 15:11:58 +02:00
if ( notificationEmail ) {
2015-07-07 18:37:25 +02:00
notifyListbox . setSelectedIndex ( 1 ) ;
2015-07-06 15:11:58 +02:00
}
2014-03-05 00:38:20 +01:00
notifyListbox . setVisible ( true ) ;
2013-04-11 18:46:58 +02:00
}
else
privacyLevel . setVisible ( true ) ;
2016-04-28 19:06:17 +02:00
// all visible
attachButtonScreen . setVisible ( true ) ;
submitButtonScreen . setVisible ( true ) ;
attachButtonPhoneTablet . setVisible ( true ) ;
submitButtonPhoneTablet . setVisible ( true ) ;
2016-01-12 19:06:10 +01:00
// check if DND can be activated and enable it if it's possible
if ( checkDNDAvailability ( ) ) {
// add drag over handler on shareTextArea
shareTextArea . addDragOverHandler ( new DragOverHandler ( ) {
@Override
public void onDragOver ( DragOverEvent event ) {
GWT . log ( " Drag over handler " ) ;
2016-01-15 18:24:52 +01:00
// add style change
addDNDStyleEffects ( ) ;
2016-01-12 19:06:10 +01:00
}
} ) ;
// clear drag over effect
shareTextArea . addDragLeaveHandler ( new DragLeaveHandler ( ) {
@Override
public void onDragLeave ( DragLeaveEvent event ) {
GWT . log ( " Drag leave handler " ) ;
// remove style changes
2016-01-18 11:50:10 +01:00
resetDNDStyleEffects ( ) ;
2016-01-12 19:06:10 +01:00
}
} ) ;
// enable shareTextArea as drop target (using native javascript)
addNativeDropHandler ( singleton , FileSubmit . URL ) ;
2016-01-15 18:24:52 +01:00
} else {
GWT . log ( " Drag and drop not supported. " ) ;
2016-01-12 19:06:10 +01:00
}
}
} ) ;
2016-05-12 12:31:56 +02:00
// add onWindowClosing handler to alert the user who didn't share post if really wants to leave
Window . addWindowClosingHandler ( new Window . ClosingHandler ( ) {
public void onWindowClosing ( Window . ClosingEvent closingEvent ) {
String currentText = shareTextArea . getText ( ) ;
2016-05-12 16:03:26 +02:00
if ( ! currentText . isEmpty ( ) & & ! currentText . equals ( SHARE_UPDATE_TEXT ) ) {
2016-05-12 12:31:56 +02:00
closingEvent . setMessage ( " Do you really want to leave the page? " ) ;
}
}
} ) ;
2016-01-12 19:06:10 +01:00
}
/ * *
* Bind events to manage
* /
private void bind ( ) {
//get the uploaded file result
eventBus . addHandler ( FileUploadCompleteEvent . TYPE , new FileUploadCompleteEventHandler ( ) {
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
@Override
public void onUploadComplete ( FileUploadCompleteEvent event ) {
String absolutePathOnServer = event . getUploadedFileInfo ( ) . getAbsolutePath ( ) ;
GWT . log ( " uploaded on Server here: " + absolutePathOnServer ) ;
checkFile ( event . getUploadedFileInfo ( ) . getFilename ( ) , absolutePathOnServer ) ;
2013-01-26 20:02:51 +01:00
}
} ) ;
2016-04-28 19:06:17 +02:00
// on too large files event put to enabled the buttons attach/submit
eventBus . addHandler ( FileTooLargeEvent . TYPE , new FileTooLargeEventHandler ( ) {
@Override
public void onFileTooLargeEvent ( FileTooLargeEvent fileTooLargeEvent ) {
// We need to delay the enable calls because this event is launched in a
// FileUpload.onChange() that is also used for disabling the same buttons.
// In order to be sure they are first disabled and then enabled a delay is needed.
Timer timer = new Timer ( ) {
public void run ( ) {
enableSubmitButtons ( true ) ;
enableAttachButtons ( true ) ;
}
} ;
// Execute the timer to expire 2 seconds in the future
timer . schedule ( 300 ) ;
}
} ) ;
2013-01-26 20:02:51 +01:00
}
2015-07-06 15:11:58 +02:00
2014-01-17 19:21:35 +01:00
/** Used by UiBinder to instantiate UploadProgressView */
@UiFactory
UploadProgressPanel instatiateProgressView ( ) {
uploadProgress = new UploadProgressPanel ( eventBus ) ;
uploadProgress . setVisible ( false ) ;
return uploadProgress ;
}
2013-01-26 20:02:51 +01:00
@UiHandler ( " shareTextArea " )
void onShareUpdateClick ( ClickEvent e ) {
shareTextArea . removeSampleText ( ) ;
2015-12-21 19:18:37 +01:00
if ( shareTextArea . getText ( ) . compareTo ( " " ) = = 0 ) {
Document . get ( ) . getElementById ( " highlighterContainer " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
Document . get ( ) . getElementById ( " highlighter " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
Document . get ( ) . getElementById ( " postTextArea " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
}
2013-01-26 20:02:51 +01:00
}
2016-04-28 19:06:17 +02:00
/ * *
* One handler for the two attach buttons .
* @param e
* /
private void onAttachClickHandlerBody ( ClickEvent e ) {
2016-01-12 19:06:10 +01:00
2016-01-18 11:50:10 +01:00
// check if there is a link preview
2016-01-15 18:24:52 +01:00
if ( linkPreviewer ! = null ) {
2016-01-12 19:06:10 +01:00
2016-01-18 11:50:10 +01:00
// ask the user
boolean delete = wantToDeleteLinkPreview ( ) ;
2016-01-12 19:06:10 +01:00
2016-01-18 11:50:10 +01:00
if ( ! delete )
2016-01-12 19:06:10 +01:00
return ;
2016-01-18 11:50:10 +01:00
// remove link preview
2016-01-12 19:06:10 +01:00
cancelLinkPreview ( ) ;
2014-01-17 19:21:35 +01:00
}
2016-01-12 19:06:10 +01:00
2016-01-15 18:24:52 +01:00
// check the number of already attached files
if ( numberOfAttachments ( ) > = MAX_NUMBER_ATTACHMENTS ) {
Window . alert ( TOO_MUCH_ATTACHMENT_ALERT ) ;
return ;
}
2016-01-12 19:06:10 +01:00
// proceed with the upload
FileUpload up = uploadProgress . initialize ( ) ;
2016-01-28 13:55:35 +01:00
up . click ( ) ;
2016-01-12 19:06:10 +01:00
up . setVisible ( false ) ;
2016-01-28 13:55:35 +01:00
// disable attach button when users select a file
up . addChangeHandler ( new ChangeHandler ( ) {
@Override
public void onChange ( ChangeEvent event ) {
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( false ) ;
enableAttachButtons ( false ) ;
2016-01-28 13:55:35 +01:00
}
} ) ;
2016-01-12 19:06:10 +01:00
uploadProgress . setVisible ( true ) ;
2016-01-28 13:55:35 +01:00
2014-01-17 19:21:35 +01:00
}
2016-01-12 19:06:10 +01:00
2016-04-28 19:06:17 +02:00
@UiHandler ( " attachButtonScreen " )
void onAttachClickScreen ( ClickEvent e ) {
onAttachClickHandlerBody ( e ) ;
}
@UiHandler ( " attachButtonPhoneTablet " )
void onAttachClickOther ( ClickEvent e ) {
onAttachClickHandlerBody ( e ) ;
}
/ * *
* One handler for the two submit buttons .
* @param e
* /
private void onSubmitClickHandlerBody ( ClickEvent e ) {
2016-01-12 19:06:10 +01:00
//because otherwise it looses the other properties setting
2016-04-28 19:06:17 +02:00
attachButtonScreen . getElement ( ) . getStyle ( ) . setVisibility ( Visibility . VISIBLE ) ;
attachButtonPhoneTablet . getElement ( ) . getStyle ( ) . setVisibility ( Visibility . VISIBLE ) ;
2016-01-12 19:06:10 +01:00
2013-04-17 17:29:44 +02:00
shareupdateService . getUserSettings ( new AsyncCallback < UserSettings > ( ) {
public void onFailure ( Throwable caught ) {
2016-01-15 18:24:52 +01:00
2013-04-17 17:29:44 +02:00
Window . alert ( " Ops! we encountered some problems delivering your message, server is not responding, please try again in a short while. " ) ;
2016-01-15 18:24:52 +01:00
2013-04-17 17:29:44 +02:00
}
public void onSuccess ( UserSettings result ) {
2016-01-15 18:24:52 +01:00
2013-04-17 17:29:44 +02:00
if ( result . getUserInfo ( ) . getUsername ( ) . equals ( " test.user " ) ) {
Window . alert ( " Your session has expired, please log out and login again " ) ;
return ;
}
myUserInfo = result . getUserInfo ( ) ;
String toShare = shareTextArea . getText ( ) . trim ( ) ;
2015-07-06 15:11:58 +02:00
2016-01-15 18:24:52 +01:00
// Establish the content of this post
PostContent postContent = PostContent . ONLY_TEXT ;
// check if we are going to send a link preview (The shared text cannot be empty nor it can be an error message)
if ( linkPreviewer ! = null & & ( toShare . equals ( SHARE_UPDATE_TEXT ) | | toShare . equals ( ERROR_UPDATE_TEXT ) | | toShare . equals ( " " ) ) ) {
shareTextArea . addStyleName ( " error " ) ;
shareTextArea . setText ( ERROR_UPDATE_TEXT ) ;
return ;
} else {
if ( linkPreviewer ! = null )
postContent = PostContent . TEXT_AND_LINK ;
if ( numberOfAttachmentsUploaded ( ) > 0 ) {
postContent = PostContent . TEXT_AND_ATTACHMENTS ;
2016-01-18 10:26:36 +01:00
2016-01-19 14:35:51 +01:00
if ( toShare . equals ( " " ) | | toShare . equals ( SHARE_UPDATE_TEXT ) | | toShare . equals ( ERROR_UPDATE_TEXT ) )
2016-01-18 10:26:36 +01:00
toShare = NO_TEXT_FILE_SHARE ;
2016-01-15 18:24:52 +01:00
}
2014-01-30 16:39:42 +01:00
}
2016-01-15 18:24:52 +01:00
// check the text (attachment can be sent without shared text)
2013-04-17 17:29:44 +02:00
if ( toShare . equals ( SHARE_UPDATE_TEXT ) | | toShare . equals ( ERROR_UPDATE_TEXT ) | | toShare . equals ( " " ) ) {
shareTextArea . addStyleName ( " error " ) ;
shareTextArea . setText ( ERROR_UPDATE_TEXT ) ;
return ;
}
2016-01-15 18:24:52 +01:00
2014-01-30 16:39:42 +01:00
//then you can post but you have to pass html checks now
String toPost = toShare ;
2016-01-15 18:24:52 +01:00
postTweet ( toPost , shareTextArea . getMentionedUsers ( ) , postContent ) ;
2013-04-17 17:29:44 +02:00
}
} ) ;
2016-04-28 19:06:17 +02:00
}
@UiHandler ( " submitButtonScreen " )
void onClickSubmitScreen ( ClickEvent e ) {
onSubmitClickHandlerBody ( e ) ;
}
@UiHandler ( " submitButtonPhoneTablet " )
void onClickSubmitOtherDevices ( ClickEvent e ) {
onSubmitClickHandlerBody ( e ) ;
2013-01-26 20:02:51 +01:00
}
2014-01-17 19:21:35 +01:00
2013-01-26 20:02:51 +01:00
/ * *
2016-01-15 18:24:52 +01:00
* Publish a post .
* @param textToPost the text of this port
* @param mentionedUsers list of users mentioned in the text ( if any )
* @param postContent the type of post
2013-01-26 20:02:51 +01:00
* /
2016-01-15 18:24:52 +01:00
private void postTweet ( String textToPost , ArrayList < String > mentionedUsers , PostContent postContent ) {
// escape html text
String toShareText = escapeHtml ( textToPost ) ;
if ( ! checkTextLength ( toShareText ) ) {
2013-01-26 20:02:51 +01:00
Window . alert ( " We found a single word containing more than 50 chars and it's not a link, is it meaningful? " ) ;
return ;
}
2016-01-15 18:24:52 +01:00
// disable text edit and submission button
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( false ) ;
2013-01-26 20:02:51 +01:00
shareTextArea . setEnabled ( false ) ;
2016-01-15 18:24:52 +01:00
// retrieve the vre id
2013-01-26 20:02:51 +01:00
String vreId = " " ;
if ( getPrivacyLevel ( ) = = PrivacyLevel . SINGLE_VRE ) {
vreId = privacyLevel . getValue ( privacyLevel . getSelectedIndex ( ) ) ;
}
2015-07-06 15:11:58 +02:00
2016-01-15 18:24:52 +01:00
// notify group information
boolean notifyGroup = notifyListbox . getSelectedIndex ( ) > 0 ;
// case in which there are no attachments but there could be a link preview
if ( postContent = = PostContent . ONLY_TEXT | | postContent = = PostContent . TEXT_AND_LINK ) {
//preparing to send stuff
String linkTitle = " " , linkDescription = " " , linkUrl = " " , linkUrlThumbnail = " " , linkHost = " " ;
if ( linkPreviewer ! = null ) {
linkTitle = linkPreviewer . getLinkTitle ( ) ;
linkDescription = linkPreviewer . getLinkDescription ( ) ;
linkUrl = linkPreviewer . getUrl ( ) ;
linkUrlThumbnail = linkPreviewer . getUrlThumbnail ( ) ;
linkHost = linkPreviewer . getHost ( ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
LinkPreview preview2Share = new LinkPreview ( linkTitle , linkDescription , linkUrl , linkHost , null ) ;
2016-02-25 19:00:58 +01:00
Long vreOrgId = Long . parseLong ( vreId ) ;
2016-01-15 18:24:52 +01:00
// share post (it could contain a link preview)
2016-02-25 19:00:58 +01:00
shareupdateService . sharePostWithLinkPreview ( toShareText , FeedType . TWEET , getPrivacyLevel ( ) , vreOrgId , preview2Share , linkUrlThumbnail , mentionedUsers , notifyGroup , new AsyncCallback < ClientFeed > ( ) {
2016-01-15 18:24:52 +01:00
public void onFailure ( Throwable caught ) {
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2016-01-15 18:24:52 +01:00
shareTextArea . setEnabled ( true ) ;
shareTextArea . setText ( SHARE_UPDATE_TEXT ) ;
shareTextArea . cleanHighlighterDiv ( ) ;
preview . clear ( ) ;
linkPreviewer = null ;
}
public void onSuccess ( ClientFeed feed ) {
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2016-01-15 18:24:52 +01:00
shareTextArea . setEnabled ( true ) ;
shareTextArea . setText ( SHARE_UPDATE_TEXT ) ;
shareTextArea . cleanHighlighterDiv ( ) ;
preview . clear ( ) ;
linkPreviewer = null ;
if ( feed = = null )
Window . alert ( " Ops! we encountered some problems delivering your message, please try again in a short while. " ) ;
else {
// publish a message with the refresh notification
try {
2016-01-27 17:49:48 +01:00
2016-01-15 18:24:52 +01:00
pageBusAdapter . PageBusPublish ( " org.gcube.portal.databook.shared " , feed , ( Jsonizer ) GWT . create ( ClientFeedJsonizer . class ) ) ;
2016-01-19 14:35:51 +01:00
2016-01-15 18:24:52 +01:00
} catch ( PageBusAdapterException ex ) {
GWT . log ( ex . getMessage ( ) ) ;
}
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
//needed when posting long texts otherwise it stays with the current height
shareTextArea . getElement ( ) . getStyle ( ) . setHeight ( 54 , Unit . PX ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
} ) ;
}
else {
// case with at least one attachment available
ArrayList < UploadedFile > uploadedFiles = new ArrayList < UploadedFile > ( ) ;
// consider only correctly uploaded file(s)
for ( AttachedFile file : listOfAttachedFiles ) {
if ( file . isCorrectlyUploaded ( ) )
uploadedFiles . add (
new UploadedFile (
file . getFileName ( ) ,
file . getFileAbsolutePathOnServer ( ) ,
file . getDescription ( ) ,
file . getDownloadUrl ( ) ,
file . getThumbnailUrl ( ) ,
file . getFormat ( ) ) ) ;
2013-01-26 20:02:51 +01:00
}
2016-02-25 19:00:58 +01:00
Long vreOrgId = Long . parseLong ( vreId ) ;
2016-01-15 18:24:52 +01:00
// share the post
2016-02-25 19:00:58 +01:00
shareupdateService . sharePostWithAttachments ( toShareText , FeedType . TWEET , getPrivacyLevel ( ) , vreOrgId , uploadedFiles , mentionedUsers , notifyGroup , saveInWorkspaceCheckbox . getValue ( ) , new AsyncCallback < ClientFeed > ( ) {
2016-01-15 18:24:52 +01:00
@Override
2016-01-19 14:35:51 +01:00
public void onSuccess ( ClientFeed feed ) {
2016-01-27 17:49:48 +01:00
2016-01-20 17:54:20 +01:00
//GWT.log("Saved feed looks like " + feed.toString());
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2016-01-15 18:24:52 +01:00
shareTextArea . setEnabled ( true ) ;
shareTextArea . setText ( SHARE_UPDATE_TEXT ) ;
shareTextArea . cleanHighlighterDiv ( ) ;
saveInWorkspaceCheckbox . setVisible ( false ) ;
preview . clear ( ) ;
listOfAttachedFiles . clear ( ) ;
2016-01-19 14:35:51 +01:00
if ( feed = = null )
2016-01-15 18:24:52 +01:00
Window . alert ( " Ops! we encountered some problems delivering your message, please try again in a short while. " ) ;
else {
// publish a message with the refresh notification
try {
2016-01-19 14:35:51 +01:00
pageBusAdapter . PageBusPublish ( " org.gcube.portal.databook.shared " , feed , ( Jsonizer ) GWT . create ( ClientFeedJsonizer . class ) ) ;
GWT . log ( " SENT " ) ;
2016-01-15 18:24:52 +01:00
} catch ( PageBusAdapterException ex ) {
GWT . log ( ex . getMessage ( ) ) ;
}
}
//needed when posting long texts otherwise it stays with the current height
shareTextArea . getElement ( ) . getStyle ( ) . setHeight ( 54 , Unit . PX ) ;
}
@Override
public void onFailure ( Throwable caught ) {
2016-01-18 10:26:36 +01:00
2016-01-15 18:24:52 +01:00
GWT . log ( caught . toString ( ) ) ;
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2016-01-15 18:24:52 +01:00
shareTextArea . setEnabled ( true ) ;
shareTextArea . setText ( SHARE_UPDATE_TEXT ) ;
shareTextArea . cleanHighlighterDiv ( ) ;
saveInWorkspaceCheckbox . setVisible ( false ) ;
preview . clear ( ) ;
listOfAttachedFiles . clear ( ) ;
}
} ) ;
}
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
/ * *
* Determines the privacy level of the post to be shared .
* @return
* /
2013-01-26 20:02:51 +01:00
private PrivacyLevel getPrivacyLevel ( ) {
String selected = privacyLevel . getValue ( privacyLevel . getSelectedIndex ( ) ) ;
if ( selected . compareTo ( PrivacyLevel . CONNECTION . toString ( ) ) = = 0 )
return PrivacyLevel . CONNECTION ;
else if ( selected . compareTo ( PrivacyLevel . VRES . toString ( ) ) = = 0 )
return PrivacyLevel . VRES ;
else if ( selected . compareTo ( PrivacyLevel . PRIVATE . toString ( ) ) = = 0 )
return PrivacyLevel . PRIVATE ;
else if ( selected . compareTo ( PrivacyLevel . PORTAL . toString ( ) ) = = 0 )
return PrivacyLevel . PORTAL ;
else
return PrivacyLevel . SINGLE_VRE ;
}
/ * *
* Escape an html string . Escaping data received from the client helps to
* prevent cross - site script vulnerabilities .
*
* @param html the html string to escape
* @return the escaped string
* /
private String escapeHtml ( String html ) {
if ( html = = null ) {
return null ;
}
return html . replaceAll ( " & " , " & " ) . replaceAll ( " < " , " < " )
. replaceAll ( " > " , " > " ) ;
}
/ * *
* called when pasting a possible link
* @param linkToCheck
* /
protected void checkLink ( String textToCheck ) {
2016-01-15 18:24:52 +01:00
if ( linkPreviewer = = null ) {
2013-01-26 20:02:51 +01:00
String [ ] parts = textToCheck . split ( " \\ s " ) ;
// Attempt to convert each item into an URL.
for ( String item : parts ) {
2014-04-01 15:00:37 +02:00
if ( item . startsWith ( " http " ) | | item . startsWith ( " www " ) ) {
2016-01-12 19:06:10 +01:00
// check if there are attachments and inform the user that they will be lost
if ( ! listOfAttachedFiles . isEmpty ( ) ) {
// in this case let the user to choose what to do
boolean confirm = Window . confirm ( DELETE_ATTACHMENTS ) ;
if ( ! confirm )
return ;
// else... remove attachments and continue
listOfAttachedFiles . clear ( ) ;
preview . clear ( ) ;
2016-01-15 18:24:52 +01:00
saveInWorkspaceCheckbox . setVisible ( false ) ;
2016-01-12 19:06:10 +01:00
}
2013-01-26 20:02:51 +01:00
preview . add ( new LinkLoader ( ) ) ;
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( false ) ;
2016-01-15 18:24:52 +01:00
2013-01-26 20:02:51 +01:00
//GWT.log("It's http link:" + linkToCheck);
shareupdateService . checkLink ( textToCheck , new AsyncCallback < LinkPreview > ( ) {
public void onFailure ( Throwable caught ) {
preview . clear ( ) ;
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2013-01-26 20:02:51 +01:00
}
public void onSuccess ( LinkPreview result ) {
2016-01-15 18:24:52 +01:00
// For a link, the LinkPreview object is like this
// LinkPreview [title=ANSA.it - Homepage, description=ANSA.it: Il sito Internet dell'Agenzia ANSA. Ultime notizie, foto, video e approfondimenti su: cronaca, politica, economia, regioni, mondo, sport, calcio, cultura e tecnologia,
// url=http://www.ansa.it/, host=ansa.it, imageUrls=[http://www.ansa.it/sito/img/ico/ansa-57-precomposed.png]]
// GWT.log(result.toString());
2013-01-26 20:02:51 +01:00
preview . clear ( ) ;
if ( result ! = null )
2016-01-12 19:06:10 +01:00
addPreviewLink ( result ) ;
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2013-01-26 20:02:51 +01:00
}
} ) ;
break ;
}
}
} else {
Window . alert ( " You cannot post two links, please remove the previous one first. " ) ;
}
}
2014-01-17 19:21:35 +01:00
/ * *
* called when the file was correctly uploaded on server
2016-01-18 16:22:33 +01:00
* @param fileNameLabel the name of the file
2014-01-17 19:21:35 +01:00
* @param absolutePathOnServer the path of the file ending with its name on the server temp
* /
2014-01-30 16:39:42 +01:00
protected void checkFile ( final String fileName , final String absolutePathOnServer ) {
2016-01-12 19:06:10 +01:00
// create temp view of the attached file and add to the previewer
2016-01-15 18:24:52 +01:00
final AttachmentPreviewer atPrev = new AttachmentPreviewer ( fileName , attachedDefaultImageUrl , preview , this ) ;
2016-01-12 19:06:10 +01:00
2014-01-17 19:21:35 +01:00
shareupdateService . checkUploadedFile ( fileName , absolutePathOnServer , new AsyncCallback < LinkPreview > ( ) {
public void onFailure ( Throwable caught ) {
2016-01-12 19:06:10 +01:00
2016-01-15 18:24:52 +01:00
GWT . log ( " Unable to check uploaded file! " ) ;
2016-01-12 19:06:10 +01:00
2016-01-15 18:24:52 +01:00
// hide progress bar
uploadProgress . setVisible ( false ) ;
2016-01-12 19:06:10 +01:00
2016-01-15 18:24:52 +01:00
// attach the file with error..
listOfAttachedFiles . add (
new AttachedFile (
fileName ,
absolutePathOnServer ,
atPrev ,
null )
) ;
2015-07-06 15:11:58 +02:00
2016-01-15 18:24:52 +01:00
// there is no a linkPreview...
addPreviewAttachment ( null , atPrev ) ;
2015-07-06 15:11:58 +02:00
2016-01-15 18:24:52 +01:00
// enable anyway the button
2016-04-28 19:06:17 +02:00
enableSubmitButtons ( true ) ;
2016-01-27 17:49:48 +01:00
2016-01-20 17:54:20 +01:00
// enable attach button
2016-04-28 19:06:17 +02:00
enableAttachButtons ( true ) ;
2014-01-17 19:21:35 +01:00
}
2016-01-15 18:24:52 +01:00
// it returns a LinkPreview (for compatibility with old code)
2016-01-12 19:06:10 +01:00
public void onSuccess ( LinkPreview result ) {
if ( result = = null )
return ;
2016-01-15 18:24:52 +01:00
listOfAttachedFiles . add (
new AttachedFile (
result . getTitle ( ) ,
absolutePathOnServer ,
result . getDescription ( ) ,
result . getUrl ( ) ,
result . getImageUrls ( ) . get ( 0 ) ,
result . getHost ( ) ,
atPrev ,
true )
) ;
2016-01-12 19:06:10 +01:00
addPreviewAttachment ( result , atPrev ) ;
2016-04-28 19:06:17 +02:00
// enable anyway the button
enableSubmitButtons ( true ) ;
2016-01-27 17:49:48 +01:00
2016-01-20 17:54:20 +01:00
// enable attach button
2016-04-28 19:06:17 +02:00
enableAttachButtons ( true ) ;
2014-01-17 19:21:35 +01:00
}
} ) ;
}
2013-01-26 20:02:51 +01:00
/ * *
* called when pasting . it tries to avoid pasting long non spaced strings
* @param linkToCheck
* /
private boolean checkTextLength ( String textToCheck ) {
String [ ] parts = textToCheck . split ( " \\ s " ) ;
// check the length of tokens
for ( String item : parts ) {
2016-12-15 12:05:27 +01:00
if ( ! item . startsWith ( " http " ) & & ! item . startsWith ( " ftp " ) ) { //url are accepted as they can be trunked
2013-01-26 20:02:51 +01:00
if ( item . length ( ) > 50 ) {
return false ;
}
}
}
return true ;
}
/ * *
* add the link preview in the view
* @param result
* /
2016-01-12 19:06:10 +01:00
private void addPreviewLink ( LinkPreview result ) {
2016-01-15 18:24:52 +01:00
//GWT.log(result.toString());
2016-01-12 19:06:10 +01:00
preview . clear ( ) ;
2014-01-17 19:27:48 +01:00
uploadProgress . setVisible ( false ) ;
2016-01-15 18:24:52 +01:00
linkPreviewer = new LinkPreviewer ( this , result ) ;
preview . add ( linkPreviewer ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-12 19:06:10 +01:00
/ * *
* Call it to show attachment ( s )
* /
private void addPreviewAttachment ( LinkPreview result , AttachmentPreviewer atPrev ) {
2016-01-15 18:24:52 +01:00
// GWT.log(result.toString());
// disable progress bar
uploadProgress . setVisible ( false ) ;
2016-01-12 19:06:10 +01:00
// check the result
if ( result = = null ) {
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// failed upload
2016-01-15 18:24:52 +01:00
atPrev . setResultAttachment ( ATTACHMENT_NOT_LOADED , notLoadedAttachmentImageUrl ) ;
// change the atPrev object and let the user retry the upload
atPrev . retryToUpload ( atPrev ) ;
2016-01-12 19:06:10 +01:00
}
else {
2016-01-15 18:24:52 +01:00
// set the preview information (the first image is the one related to the type of file)
atPrev . setResultAttachment ( ATTACHMENT_LOADED , loadedAttachmentImageUrl ) ;
2016-01-12 19:06:10 +01:00
atPrev . setImagePreview ( result . getImageUrls ( ) . get ( 0 ) ) ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
}
2016-01-27 17:49:48 +01:00
2016-01-12 19:06:10 +01:00
preview . add ( atPrev ) ;
2016-01-15 18:24:52 +01:00
// enable checkbox to save in workspace if it's the case
if ( numberOfAttachments ( ) > 0 & & ! saveInWorkspaceCheckbox . isVisible ( ) )
saveInWorkspaceCheckbox . setVisible ( true ) ;
2016-01-12 19:06:10 +01:00
}
2013-01-26 20:02:51 +01:00
/ * *
2016-01-15 18:24:52 +01:00
* Delete the only link previewer allowed .
2013-01-26 20:02:51 +01:00
* /
2016-01-12 19:06:10 +01:00
protected void cancelLinkPreview ( ) {
2013-01-26 20:02:51 +01:00
preview . clear ( ) ;
2016-01-15 18:24:52 +01:00
linkPreviewer = null ;
2016-04-28 19:06:17 +02:00
attachButtonScreen . getElement ( ) . getStyle ( ) . setVisibility ( Visibility . VISIBLE ) ; //beacuse otherwise it looses the other properties setting
attachButtonPhoneTablet . getElement ( ) . getStyle ( ) . setVisibility ( Visibility . VISIBLE ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-12 19:06:10 +01:00
/ * *
* Handle drop of files within shareTextArea ( native javascript code )
* @param instance
* /
private static native void addNativeDropHandler ( ShareUpdateForm instance ,
String servletUrl ) / * - {
// retrieve textArea by id
2016-01-15 18:24:52 +01:00
var drop = $wnd . $ ( ' # postTextArea ' ) [ 0 ] ;
2016-01-12 19:06:10 +01:00
// check if this file is a folder
function isFolder ( file ) {
if ( file ! = null & & ! file . type & & file . size % 4096 = = 0 ) {
return true ;
}
return false ;
}
// function used to add the handler
function addEventHandler ( obj , evt , handler ) {
if ( obj . addEventListener ) {
// W3C method
obj . addEventListener ( evt , handler , false ) ;
} else if ( obj . attachEvent ) {
// IE method.
obj . attachEvent ( ' on ' + evt , handler ) ;
} else {
// Old school method.
obj [ ' on ' + evt ] = handler ;
}
}
// The real drop handler
addEventHandler (
drop ,
' drop ' ,
function ( e ) {
// get window.event if e argument missing (in IE)
e = e | | window . event ;
// stops the browser from redirecting off to the image.
if ( e . preventDefault ) {
e . preventDefault ( ) ;
}
// opts for the remote call
var opts = {
url : servletUrl ,
type : " POST " ,
processData : false
} ;
// get the file(s)
var dt = e . dataTransfer ;
var files = dt . files ;
2016-01-18 11:50:10 +01:00
// chek if a link preview is already there
var linkPreviewPresent = instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : isLinkPreviewPresent ( ) ( ) ;
if ( linkPreviewPresent ) {
var hasToBeDelete = instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : wantToDeleteLinkPreview ( ) ( ) ;
if ( ! hasToBeDelete ) {
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : resetDNDStyleEffects ( ) ( ) ;
return ;
}
2016-04-28 19:06:17 +02:00
// else delete the link preview, enable the attach buttons and proceeed
2016-01-18 11:50:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : cancelLinkPreview ( ) ( ) ;
2016-04-28 19:06:17 +02:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : enableAttachButtons ( Z ) ( true ) ;
2016-01-18 11:50:10 +01:00
}
2016-01-12 19:06:10 +01:00
// check limit for number of files
var numberOfAlreadyAttachedFiles = instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : numberOfAttachments ( ) ( ) ;
numberOfAlreadyAttachedFiles + = files . length ;
2016-01-21 15:05:02 +01:00
var limitExceeded = ( numberOfAlreadyAttachedFiles > @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : MAX_NUMBER_ATTACHMENTS ) ;
2016-01-12 19:06:10 +01:00
if ( limitExceeded ) {
2016-01-28 11:28:17 +01:00
var msg = " Sorry, you are trying to attach more than " +
+ @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : MAX_NUMBER_ATTACHMENTS " files! "
2016-01-12 19:06:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showAlert ( Ljava / lang / String ; ) ( msg ) ;
console . log ( msg ) ;
// reset text area
2016-01-18 11:50:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : resetDNDStyleEffects ( ) ( ) ;
2016-01-12 19:06:10 +01:00
return ;
}
// reset if no file was dropped (??)
if ( files . length = = 0 ) {
// reset text area
2016-01-18 11:50:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : resetDNDStyleEffects ( ) ( ) ;
2016-01-12 19:06:10 +01:00
return ;
}
console . log ( " Number of dropped file(s): " + files . length ) ;
var numFolder = 0 ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// save maximum allowed size
var maximumSize = @org.gcube.portlets.widgets.fileupload.client.view.FileSubmit : : MAX_SIZE_ATTACHED_FILE_MB ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// msg for ignored (too big files)
var ignoredFilesAlert = " file(s) ignored because larger than " + maximumSize + " MB " ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// number of ignored files
var numberIgnoredFiles = 0 ;
2016-01-27 17:49:48 +01:00
2016-01-27 11:14:32 +01:00
// disable attach button
2016-04-28 19:06:17 +02:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : enableAttachButtons ( Z ) ( false ) ;
// disable submit button
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : enableSubmitButtons ( Z ) ( false ) ;
2016-01-12 19:06:10 +01:00
// for each dropped file
for ( var i = 0 ; i < files . length ; i + + ) {
var file = files [ i ] ;
var fileSelected = file . name + " ; " ;
// be sure it is not a folder
if ( ! isFolder ( file ) ) {
console . log ( " filesSelected: " + fileSelected ) ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// check its size
var fileSize = file . size / 1024 / 1024 ;
2016-01-15 18:24:52 +01:00
console . log ( " File size is " + fileSize + " MB " ) ;
2016-01-12 19:06:10 +01:00
if ( fileSize > maximumSize ) {
numberIgnoredFiles + + ;
continue ;
}
2016-01-27 17:49:48 +01:00
2016-01-12 19:06:10 +01:00
// create new progress bar
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showProgressDND ( ) ( ) ;
// create request
var xhr = new XMLHttpRequest ( ) ;
xhr . open ( opts . type , opts . url , true ) ;
var formdata = new FormData ( ) ;
// append the file
formdata . append ( " fileUpload " , file ) ;
// send data
xhr . send ( formdata ) ;
console . log ( " File " + file . name + " sent at " + servletUrl ) ;
} else {
// increment the number of skipped folders
numFolder + + ;
}
}
2016-01-27 17:49:48 +01:00
2016-01-28 11:28:17 +01:00
// enable attach button (the checkFile method will do this...)
2016-01-12 19:06:10 +01:00
// alert the user that folder(s) can't be uploaded
if ( numFolder > 0 ) {
var msg ;
if ( numFolder = = files . length ) {
msg = " Sorry but it's not possible to upload a folder! " ;
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showAlert ( Ljava / lang / String ; ) ( msg ) ;
// reset text area
2016-01-18 11:50:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : resetDNDStyleEffects ( ) ( ) ;
2016-01-12 19:06:10 +01:00
2016-04-28 19:06:17 +02:00
} else {
2016-01-12 19:06:10 +01:00
2016-04-28 19:06:17 +02:00
// print ignored folders, if any
var msg = " Ignored " ;
msg + = numFolder > 1 ? numFolder + " folders " : numFolder + " folder " ;
msg + = " during upload. " ;
console . log ( msg ) ;
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showAlert ( Ljava / lang / String ; ) ( msg ) ;
}
2016-01-12 19:06:10 +01:00
}
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
// alert for too large files
if ( numberIgnoredFiles > 0 ) {
var msg = numberIgnoredFiles + ignoredFilesAlert ;
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
if ( numberIgnoredFiles = = files . length ) {
2016-01-15 18:24:52 +01:00
msg = file . name + " can't be uploaded because it is too large! " ;
2016-01-12 19:06:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showAlert ( Ljava / lang / String ; ) ( msg ) ;
2016-04-28 19:06:17 +02:00
} else {
2016-05-12 12:31:56 +02:00
2016-04-28 19:06:17 +02:00
var msg = numberIgnoredFiles + ignoredFilesAlert ;
console . log ( msg ) ;
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : showAlert ( Ljava / lang / String ; ) ( msg ) ;
2016-05-12 12:31:56 +02:00
2016-01-12 19:06:10 +01:00
}
}
// reset text area
2016-01-18 11:50:10 +01:00
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : resetDNDStyleEffects ( ) ( ) ;
2016-05-12 12:31:56 +02:00
2016-04-28 19:06:17 +02:00
// enable attach buttons
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : enableAttachButtons ( Z ) ( true ) ;
// enable submit buttons
instance . @org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm : : enableSubmitButtons ( Z ) ( true ) ;
2016-05-12 12:31:56 +02:00
2016-01-12 19:06:10 +01:00
} ) ;
} - * / ;
2016-01-18 10:26:36 +01:00
2016-01-12 19:06:10 +01:00
/ * *
* Check if DND could be enabled ( i . e , it ' s supported by the browser )
* @return
* /
public static native boolean checkDNDAvailability ( ) / * - {
return window . FileReader ;
} - * / ;
2016-01-15 18:24:52 +01:00
/ * *
* Add DND style effect on drag over .
* /
private void addDNDStyleEffects ( ) {
// save current text (note that the DragOverEvent event can be fired several times)
boolean conditionToSave = ! shareTextArea . getText ( ) . equals ( DROP_FILE_HERE_TEXT ) & & ! shareTextArea . getText ( ) . equals ( SHARE_UPDATE_TEXT ) ;
previousText = conditionToSave ? shareTextArea . getText ( ) : previousText ;
// change border properties
shareTextArea . getElement ( ) . getStyle ( ) . setBorderStyle ( BorderStyle . DASHED ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setBorderColor ( " rgba(82, 168, 236, 0.6) " ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setBorderWidth ( 2 . 5 , Unit . PX ) ;
// change background color
shareTextArea . getElement ( ) . getStyle ( ) . setBackgroundColor ( " rgba(82, 168, 236, 0.2) " ) ;
// enlarge the window
Document . get ( ) . getElementById ( " highlighterContainer " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
Document . get ( ) . getElementById ( " highlighter " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
Document . get ( ) . getElementById ( " postTextArea " ) . getStyle ( ) . setHeight ( 52 , Unit . PX ) ;
// add "Drop file here" text
shareTextArea . setText ( DROP_FILE_HERE_TEXT ) ;
shareTextArea . setAlignment ( TextAlignment . CENTER ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setFontWeight ( FontWeight . BOLD ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setPaddingTop (
( Double . parseDouble ( shareTextArea . getElement ( ) . getStyle ( ) . getHeight ( ) . replace ( " px " , " " ) ) + 20 ) / 2 . 0 , Unit . PX ) ;
// set the color of the text if needed to gray
if ( ! previousText . equals ( SHARE_UPDATE_TEXT ) )
shareTextArea . getElement ( ) . getStyle ( ) . setColor ( " #999 " ) ;
}
2016-01-12 19:06:10 +01:00
/ * *
* On dragLeave reset changes on the text area
* /
2016-01-18 11:50:10 +01:00
private void resetDNDStyleEffects ( ) {
2016-01-12 19:06:10 +01:00
// remove border properties
shareTextArea . getElement ( ) . getStyle ( ) . setBorderStyle ( BorderStyle . SOLID ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setBorderColor ( " #333 " ) ;
shareTextArea . getElement ( ) . getStyle ( ) . setBorderWidth ( 1 , Unit . PX ) ;
// change back background color
shareTextArea . getElement ( ) . getStyle ( ) . setBackgroundColor ( " transparent " ) ;
// remove text "Drop file here" and reput the old text
shareTextArea . setText ( previousText ) ;
shareTextArea . setAlignment ( TextAlignment . LEFT ) ;
2016-01-28 11:28:17 +01:00
// change text color if needed
2016-01-12 19:06:10 +01:00
if ( ! previousText . equals ( DROP_FILE_HERE_TEXT ) & & ! previousText . equals ( SHARE_UPDATE_TEXT ) )
shareTextArea . getElement ( ) . getStyle ( ) . setColor ( " #333 " ) ;
// reset padding top
shareTextArea . getElement ( ) . getStyle ( ) . setPaddingTop ( 4 , Unit . PX ) ;
// reset font weight
shareTextArea . getElement ( ) . getStyle ( ) . setFontWeight ( FontWeight . NORMAL ) ;
}
/ * *
* Alert the user about something .
*
* @param msg the msg to show
* /
private void showAlert ( String msg ) {
Window . alert ( msg ) ;
}
/ * *
* Show progress bar and start the ProgressController
* @param e
* /
private void showProgressDND ( ) {
uploadProgress . initializeDND ( ) ;
uploadProgress . setVisible ( true ) ;
}
/ * *
* Remove an attached file from the listOfAttachedFiles
* @param attachmentPreviewer
* /
public void removeAttachedFile ( AttachmentPreviewer attachmentPreviewer ) {
Iterator < AttachedFile > iterator = listOfAttachedFiles . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
AttachedFile attachedFile = ( AttachedFile ) iterator . next ( ) ;
2016-01-15 18:24:52 +01:00
if ( attachedFile . getAtPrev ( ) . equals ( attachmentPreviewer ) ) {
2016-01-12 19:06:10 +01:00
iterator . remove ( ) ;
2016-01-15 18:24:52 +01:00
break ;
2016-01-12 19:06:10 +01:00
}
}
2016-01-15 18:24:52 +01:00
// check the final number of attachments and if it's less than one, set to false
// the save in workspace checkbox visibility
if ( numberOfAttachments ( ) = = 0 )
saveInWorkspaceCheckbox . setVisible ( false ) ;
2016-01-12 19:06:10 +01:00
}
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
/ * *
2016-01-15 18:24:52 +01:00
* Get the number of attached files ( both uploaded and not ) .
2016-01-12 19:06:10 +01:00
* @return number of attached files
* /
2016-01-28 11:28:17 +01:00
private int numberOfAttachments ( ) {
2016-01-15 18:24:52 +01:00
2016-01-12 19:06:10 +01:00
return listOfAttachedFiles . size ( ) ;
2016-01-15 18:24:52 +01:00
}
/ * *
* Retrieve the number of correctly uploaded attached files .
* @return number of attached files correctly uploaded .
* /
private int numberOfAttachmentsUploaded ( ) {
int counter = 0 ;
for ( AttachedFile attachedFile : listOfAttachedFiles ) {
if ( attachedFile . isCorrectlyUploaded ( ) )
counter + + ;
}
return counter ;
2016-01-12 19:06:10 +01:00
}
2016-01-18 11:50:10 +01:00
/ * *
* Is there any link preview ?
* @return < true > if a link preview is already there
* /
private boolean isLinkPreviewPresent ( ) {
return linkPreviewer ! = null ;
}
/ * *
* Asks the user if he / she wants to delete the link preview
* @return
* /
private boolean wantToDeleteLinkPreview ( ) {
return Window . confirm ( DELETE_LINK_PREVIEW ) ;
}
2016-01-27 17:49:48 +01:00
2016-01-27 11:14:32 +01:00
/ * *
2016-04-28 19:06:17 +02:00
* Enables or disable the attach buttons
* @param enable
* /
private void enableAttachButtons ( boolean enable ) {
attachButtonScreen . setEnabled ( enable ) ;
attachButtonPhoneTablet . setEnabled ( enable ) ;
}
/ * *
* Enables or disable the submit buttons
2016-01-27 11:14:32 +01:00
* @param enable
* /
2016-04-28 19:06:17 +02:00
private void enableSubmitButtons ( boolean enable ) {
2016-01-27 17:49:48 +01:00
2016-04-28 19:06:17 +02:00
GWT . log ( " enable is " + enable ) ;
submitButtonScreen . setEnabled ( enable ) ;
submitButtonPhoneTablet . setEnabled ( enable ) ;
2016-01-27 17:49:48 +01:00
2016-01-27 11:14:32 +01:00
}
2013-01-26 20:02:51 +01:00
}