Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy committed Oct 30, 2024
1 parent f582e91 commit 73492e6
Show file tree
Hide file tree
Showing 19 changed files with 767 additions and 194 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[![Published on Vaadin Directory](https://img.shields.io/badge/Vaadin%20Directory-published-00b4f0.svg)](https://vaadin.com/directory/component/template-addon)
[![Stars on vaadin.com/directory](https://img.shields.io/vaadin-directory/star/template-addon.svg)](https://vaadin.com/directory/component/template-addon)
[![Build Status](https://jenkins.flowingcode.com/job/template-addon/badge/icon)](https://jenkins.flowingcode.com/job/template-addon)
[![Maven Central](https://img.shields.io/maven-central/v/com.flowingcode.vaadin.addons/template-addon)](https://mvnrepository.com/artifact/com.flowingcode.vaadin.addons/template-addon)
[![Javadoc](https://img.shields.io/badge/javadoc-00b4f0)](https://javadoc.flowingcode.com/artifact/com.flowingcode.vaadin.addons/template-addon)
[![Published on Vaadin Directory](https://img.shields.io/badge/Vaadin%20Directory-published-00b4f0.svg)](https://vaadin.com/directory/component/upload-helper-add-on)
[![Stars on vaadin.com/directory](https://img.shields.io/vaadin-directory/star/upload-helper-addon.svg)](https://vaadin.com/directory/component/upload-helper-add-on)
[![Build Status](https://jenkins.flowingcode.com/job/upload-helper-addon/badge/icon)](https://jenkins.flowingcode.com/job/upload-helper-addon)
[![Maven Central](https://img.shields.io/maven-central/v/com.flowingcode.vaadin.addons/upload-helper-addon)](https://mvnrepository.com/artifact/com.flowingcode.vaadin.addons/upload-helper-addon)
[![Javadoc](https://img.shields.io/badge/javadoc-00b4f0)](https://javadoc.flowingcode.com/artifact/com.flowingcode.vaadin.addons/upload-helper-addon)

# Template Add-on
# Upload helper Add-on

This is a template project for building new Vaadin 24 add-ons
Flow API for `vaadin-upload-file`

## Features

* List the features of your add-on in here
* Provide an API for manipulating the [vaadin-upload-file](https://cdn.vaadin.com/vaadin-web-components/24.5.0/#/elements/vaadin-upload-file) element.

## Online demo

[Online demo here](http://addonsv24.flowingcode.com/template)
[Online demo here](http://addonsv24.flowingcode.com/upload-helper)

## Download release

[Available in Vaadin Directory](https://vaadin.com/directory/component/template-addon)
[Available in Vaadin Directory](https://vaadin.com/directory/component/upload-helper-add-on)

### Maven install

Expand All @@ -27,7 +27,7 @@ Add the following dependencies in your pom.xml file:
```xml
<dependency>
<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>template-addon</artifactId>
<artifactId>upload-helper-addon</artifactId>
<version>X.Y.Z</version>
</dependency>
```
Expand All @@ -44,7 +44,7 @@ To see the demo, navigate to http://localhost:8080/

## Release notes

See [here](https://github.com/FlowingCode/TemplateAddon/releases)
See [here](https://github.com/FlowingCode/UploadHelper/releases)

## Issue tracking

Expand All @@ -69,7 +69,7 @@ Then, follow these steps for creating a contribution:

This add-on is distributed under Apache License 2.0. For license terms, see LICENSE.txt.

TEMPLATE_ADDON is written by Flowing Code S.A.
Upload Helper Add-On is written by Flowing Code S.A.

# Developer Guide

Expand Down
32 changes: 22 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>template-addon</artifactId>
<artifactId>upload-helper-addon</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Template Add-on</name>
<description>Template Add-on for Vaadin Flow</description>
<name>Upload Helper Add-on</name>
<description>Upload Helper Add-on for Vaadin Flow</description>
<url>https://www.flowingcode.com/en/open-source/</url>

<properties>
<vaadin.version>24.4.6</vaadin.version>
<vaadin.version>24.5.0</vaadin.version>
<selenium.version>4.10.0</selenium.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand All @@ -29,7 +29,7 @@
<url>https://www.flowingcode.com</url>
</organization>

<inceptionYear>2024</inceptionYear>
<inceptionYear>2022</inceptionYear>
<licenses>
<license>
<name>Apache 2</name>
Expand All @@ -39,9 +39,9 @@
</licenses>

<scm>
<url>https://github.com/FlowingCode/AddonStarter24</url>
<connection>scm:git:git://github.com/FlowingCode/AddonStarter24.git</connection>
<developerConnection>scm:git:ssh://[email protected]:/FlowingCode/AddonStarter24.git</developerConnection>
<url>https://github.com/FlowingCode/UploadHelper</url>
<connection>scm:git:git://github.com/FlowingCode/UploadHelper.git</connection>
<developerConnection>scm:git:ssh://[email protected]:/FlowingCode/UploadHelper.git</developerConnection>
<tag>master</tag>
</scm>

Expand Down Expand Up @@ -122,6 +122,17 @@
<artifactId>vaadin-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.flowingcode.vaadin.test</groupId>
<artifactId>testbench-rpc</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
Expand Down Expand Up @@ -313,9 +324,9 @@
<configuration>
<quiet>true</quiet>
<doclint>none</doclint>
<failOnWarnings>true</failOnWarnings>
<failOnWarnings>true</failOnWarnings>
<links>
<link>https://javadoc.io/doc/com.vaadin/vaadin-platform-javadoc/${vaadin.version}</link>
<link>https://javadoc.io/doc/com.vaadin/vaadin-platform-javadoc/${vaadin.version}</link>
</links>
</configuration>
</plugin>
Expand Down Expand Up @@ -496,6 +507,7 @@
<exclude>**/it/*</exclude>
<exclude>**/DemoView.class</exclude>
<exclude>**/DemoLayout.class</exclude>
<exclude>**/AppShellConfiguratorImpl.class</exclude>
</excludes>
</configuration>
</execution>
Expand Down
212 changes: 212 additions & 0 deletions src/main/java/com/flowingcode/vaadin/addons/uploadhelper/FileInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*-
* #%L
* Upload Helper Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.addons.uploadhelper;

import com.vaadin.flow.component.upload.FinishedEvent;
import com.vaadin.flow.component.upload.Upload;
import elemental.json.Json;
import elemental.json.JsonObject;
import java.io.Serializable;
import lombok.AccessLevel;
import lombok.Getter;

/** Represents a file in the file list of an {@link Upload} component. */
@SuppressWarnings("serial")
public final class FileInfo implements Serializable {

private final String name;

@Getter(AccessLevel.PACKAGE)
private Upload upload;

private Boolean complete;
private Boolean indeterminate;
private String errorMessage;
private Integer progress;
private String status;

/**
* Constructs a {@code FileInfo} object with the specified upload and file name.
*
* @param upload the {@code Upload} instance representing the file upload source
* @param name the name of the file being uploaded
*/
public FileInfo(Upload upload, String name) {
this.upload = upload;
this.name = name;
}

/**
* Constructs a {@code FileInfo} object from the specified finished upload event.
*
* @param ev the {@code FinishedEvent} representing the succeeded or failed event
*/
public FileInfo(FinishedEvent ev) {
upload = ev.getSource();
name = ev.getFileName();
}

/** Updates this file in the upload component. */
public void update() {
update(false);
}

/** Adds a new file to the upload component. */
public void create() {
update(true);
}

private void update(boolean createIfNotExists) {
upload.getElement().executeJs(
"""
var d = this;
var i = d.files.findIndex(f=>f.name==$0.name);
if (i<0) {
if ($1) d.files = [... d.files, $0]; else return;
} else {
if (d.files.some((e,j)=>e.name==$0.name && j>i)) d.files=d.files.filter((e,j)=>e.name!=$0.name || j<=i);
d.files[i] = Object.assign(d.files[i], $0);
}
d.files = Array.from(d.files);
""",
toJson(), createIfNotExists);
}

private JsonObject toJson() {
JsonObject json = Json.createObject();
json.put("name", name);
if (complete != null) {
json.put("complete", complete);
}
if (indeterminate != null) {
json.put("indeterminate", indeterminate);
}
if (errorMessage != null) {
json.put("error", errorMessage);
}
if (progress != null) {
json.put("progress", progress);
}
if (status != null) {
json.put("status", status);
}
return json;
}

/**
* True if uploading is completed, false otherwise.
*
* @return This instance for method chaining
*/
public FileInfo complete(Boolean complete) {
this.complete = complete;
return this;
}

/**
* Configure the upload in complete state (i.e. uploading is completed)
*
* @return This instance for method chaining
*/
public FileInfo complete() {
complete = true;
return this;
}

/**
* Configure the upload in indeterminate state (i.e. the remaining time is unknown)
*
* @return This instance for method chaining
*/
public FileInfo indeterminate() {
return indeterminate(true);
}

/**
* True if the remaining time is unknown, false otherwise.
*
* @return This instance for method chaining
*/
public FileInfo indeterminate(Boolean indeterminate) {
this.indeterminate = indeterminate;
return this;
}

/**
* Error message returned by the server, if any.
*
* @return This instance for method chaining
*/
public FileInfo errorMessage(String errorMessage) {
this.errorMessage = errorMessage;
return this;
}

/**
* Number representing the uploading progress.
*
* @return This instance for method chaining
*/
public FileInfo progress(Integer progress) {
this.progress = progress;
return this;
}

/**
* Uploading status message.
*
* @return This instance for method chaining
*/
public FileInfo status(String status) {
this.status = status;
return this;
}

/** Returns the name of the uploaded file. */
public String getName() {
return name;
}

/** Returns {@code true} if uploading is completed, false otherwise. */
public Boolean getComplete() {
return complete;
}

/** Returns {@code true} if the remaining progress is unknown, false otherwise. */
public Boolean getIndeterminate() {
return indeterminate;
}

/** Returns the error message returned by the server. */
public String getErrorMessage() {
return errorMessage;
}

/** Returns a number between 0 and 100, representing the uploading progress. */
public Integer getProgress() {
return progress;
}

/** Returns the uploading status. */
public String getStatus() {
return status;
}

}
19 changes: 19 additions & 0 deletions src/main/resources/META-INF/VAADIN/package.properties
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
###
# #%L
# Upload Helper Add-on
# %%
# Copyright (C) 2022 - 2024 Flowing Code
# %%
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# #L%
###
vaadin.allowed-packages=com.flowingcode
Loading

0 comments on commit 73492e6

Please sign in to comment.