Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

341609 Add delivery block to Site #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ group :production do
gem "passenger"
end

gem "active_storage_validations"
gem "acts_as_list"
gem "friendly_id", "~> 5.2"
gem "record_tag_helper", "~> 1.0" # Add helpers removed from Rails core in Rails 5
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ GEM
rails-html-sanitizer (~> 1.6)
active_record_doctor (1.15.0)
activerecord (>= 4.2.0)
active_storage_validations (1.3.0)
activejob (>= 6.1.4)
activemodel (>= 6.1.4)
activestorage (>= 6.1.4)
activesupport (>= 6.1.4)
activejob (7.2.1.1)
activesupport (= 7.2.1.1)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -588,6 +593,7 @@ PLATFORMS

DEPENDENCIES
active_record_doctor
active_storage_validations
acts_as_list
bootsnap
bootstrap (~> 5.3.2)
Expand Down
8 changes: 6 additions & 2 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,11 @@ body {
}

.export-button {
max-width: 77px;
max-width: 70px;
}

.export-pdf-button {
max-width: 117px;
}

.export-button-move {
Expand All @@ -771,7 +775,7 @@ body {
.ts-dropdown {
z-index: 9999;
}

/* Tom-Select fix for bootstrap floating label */
.filters-component {
.form-floating > .ts-wrapper {
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def set_site

# Never trust parameters from the scary internet, only allow the white list through.
def site_params
params.require(:site).permit(:name, :description, :position, :street, :country, :city, :latitude, :longitude)
params.require(:site).permit(
:name, :description,
:position,
:street, :country, :city, :latitude, :longitude,
:delivery_address, :delivery_times, :delivery_map
)
end
end
9 changes: 9 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# frozen_string_literal: true

module ApplicationHelper
def accepted_format_for_attachment(model_klass, attribute_name)
validator = model_klass.validators_on(attribute_name.to_sym).find do |v|
v.is_a?(ActiveStorageValidations::ContentTypeValidator)
end

return unless validator

validator.options[:in].map { |f| Mime[f].to_s }.uniq.join(", ")
end
end
66 changes: 13 additions & 53 deletions app/javascript/controllers/export_pdf_controller.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
import { Controller } from "@hotwired/stimulus"

import { get } from "@rails/request.js"
import { html2pdf, saveAs, PDFDocument } from "html2pdf.js"
import { html2pdf } from "html2pdf.js"

const exportOptions = {
margin: 10,
image: {
type: "jpeg",
quality: 1.0
},
pagebreak: { avoid: ".server" },
enableLinks: false,
html2canvas: { windowWidth: 1200, width: 1200, scale: 1.25 },
html2canvas: { scale: 1.5 },
jsPDF: { format: "legal" }
}

export default class extends Controller {
static targets = ["spinner"]
static values = {
modelIds: Array,
filename: String,
isMove: Boolean
}

async export(event) {
const viewTarget = event.target.closest("a").dataset.viewTarget
if (!viewTarget) return

const bgWiring = event.target.dataset.bgWiring
static values = { filename: String }

async export() {
this.showSpinner()

const pdfDoc = await this.generatePDF(viewTarget, bgWiring)
const pdfBytes = await pdfDoc.save()
const blob = new Blob([pdfBytes], { type: "application/pdf" })

saveAs(blob, `${this.filenameValue}_${viewTarget}${ bgWiring ? "_wiring" : ""}.pdf`)

this.hideSpinner()
}

async generatePDF(viewTarget, bgWiring) {
const pdfDoc = await PDFDocument.create();

for (let i = 0; i < this.modelIdsValue.length; i++) {
const modelId = this.modelIdsValue[i]


const url = this.isMoveValue ? `moves/print/${modelId}`:
`/visualization/frames/${modelId}/print?view=${viewTarget}${ bgWiring ? "&bg=wiring" : ""}`

const response = await get(url, {
responseKind: "application/pdf"
})

if (response.ok) {
const html = await response.text

const framePage = await html2pdf()
.set(exportOptions)
.from(html)
.output("arraybuffer")

const tmpDoc = await PDFDocument.load(framePage)
const tmpPage = await pdfDoc.copyPages(tmpDoc, tmpDoc.getPageIndices())

tmpPage.forEach((page) => pdfDoc.addPage(page))
}
}
const opt = { ...exportOptions, ...{
filename: `export_${this.filenameValue}`
}}

return pdfDoc
var element = document.getElementById("export-to-pdf").cloneNode(true)
element.prepend(document.getElementsByTagName("h1")[0].cloneNode(true))
await html2pdf().set(opt)
.from(element)
.save()
.finally(() => this.hideSpinner())
}

showSpinner() {
Expand Down
69 changes: 69 additions & 0 deletions app/javascript/controllers/frame_export_pdf_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ExportPdfController from "./export_pdf_controller.js"

import { get } from "@rails/request.js"
import { html2pdf, saveAs, PDFDocument } from "html2pdf.js"

const exportOptions = {
margin: 10,
image: {
type: "jpeg",
quality: 1.0
},
pagebreak: { avoid: ".server" },
enableLinks: false,
html2canvas: { windowWidth: 1200, width: 1200, scale: 1.25 },
jsPDF: { format: "legal" }
}

export default class extends ExportPdfController {
static values = {
modelIds: Array,
isMove: Boolean
}

async export(event) {
const viewTarget = event.target.closest("a").dataset.viewTarget
if (!viewTarget) return

const bgWiring = event.target.dataset.bgWiring

this.showSpinner()

const pdfDoc = await this.generatePDF(viewTarget, bgWiring)
const pdfBytes = await pdfDoc.save()
const blob = new Blob([pdfBytes], { type: "application/pdf" })

saveAs(blob, `${this.filenameValue}_${viewTarget}${ bgWiring ? "_wiring" : ""}.pdf`)

this.hideSpinner()
}

async generatePDF(viewTarget, bgWiring) {
const pdfDoc = await PDFDocument.create();

for (let i = 0; i < this.modelIdsValue.length; i++) {
const modelId = this.modelIdsValue[i]

const url = this.isMoveValue ? `moves/print/${modelId}`:
`/visualization/frames/${modelId}/print?view=${viewTarget}${ bgWiring ? "&bg=wiring" : ""}`

const response = await get(url)

if (response.ok) {
const html = await response.text

const framePage = await html2pdf()
.set(exportOptions)
.from(html)
.output("arraybuffer")

const tmpDoc = await PDFDocument.load(framePage)
const tmpPage = await pdfDoc.copyPages(tmpDoc, tmpDoc.getPageIndices())

tmpPage.forEach((page) => pdfDoc.addPage(page))
}
}

return pdfDoc
}
}
6 changes: 5 additions & 1 deletion app/models/site.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

class Site < ApplicationRecord
geocoded_by :address
has_changelog

has_many :rooms, dependent: :restrict_with_error
has_many :frames, through: :rooms, dependent: :restrict_with_error

has_one_attached :delivery_map
nicolas-brousse marked this conversation as resolved.
Show resolved Hide resolved

validates :delivery_map, content_type: %i[png jpg jpeg gif pdf]

geocoded_by :address
after_validation :geocode

scope :sorted, -> { order(:position) }
Expand Down
20 changes: 10 additions & 10 deletions app/views/bays/_export_button.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<div class="btn-group btn-group-sm"
data-controller="export-pdf"
data-export-pdf-model-ids-value="<%= bay.frames.order(:name).pluck(:id) %>"
data-export-pdf-filename-value="bay-<%= bay.id %>">
data-controller="frame-export-pdf"
data-frame-export-pdf-model-ids-value="<%= bay.frames.order(:name).pluck(:id) %>"
data-frame-export-pdf-filename-value="bay-<%= bay.id %>">
<button type="button"
class="btn btn-outline-primary dropdown-toggle d-flex align-items-center export-button"
data-bs-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
<span class="spinner-border spinner-border-sm flex-shrink-0 d-flex d-none" role="status" aria-hidden="true"
data-export-pdf-target="spinner"></span>
<span class="text-truncate d-inline-block ms-2">
<span class="spinner-border spinner-border-sm flex-shrink-0 d-flex d-none me-" role="status" aria-hidden="true"
data-frame-export-pdf-target="spinner"></span>
<span class="text-truncate d-inline-block">
<%= t("export_button.label") %>
</span>
</button>
Expand All @@ -19,26 +19,26 @@
<h6 class="dropdown-header"><%= t("export_button.exports.pdf") %></h6>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="front">
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="front">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.front") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="back">
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="back">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.back") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="front"
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="front"
data-bg-wiring="true">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.front_wiring") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="back"
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="back"
data-bg-wiring="true">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.back_wiring") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/bays/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<% end %>
<% end %>

<% table.with_column(style: "min-width: 140px; width: 140px") do |bay| %>
<% table.with_column(style: "min-width: 132px; width: 132px") do |bay| %>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<%= render partial: "bays/export_button", locals: { bay: bay } %>
<%= link_to visualization_bay_path(bay), class: "btn btn-primary", data: { turbo_frame: :_top } do %>
Expand Down
20 changes: 10 additions & 10 deletions app/views/islets/_export_button.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<div class="dropdown btn-group btn-group-sm"
data-controller="export-pdf"
data-export-pdf-model-ids-value="<%= islet.frames.order(:name).pluck(:id) %>"
data-export-pdf-filename-value="islet-<%= islet.name %>">
data-controller="frame-export-pdf"
data-frame-export-pdf-model-ids-value="<%= islet.frames.order(:name).pluck(:id) %>"
data-frame-export-pdf-filename-value="islet-<%= islet.name %>">
<button type="button"
class="btn btn-outline-primary dropdown-toggle d-flex align-items-center export-button"
data-bs-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
<span class="spinner-border spinner-border-sm flex-shrink-0 d-flex d-none" role="status" aria-hidden="true"
data-export-pdf-target="spinner"></span>
<span class="text-truncate d-inline-block ms-2">
<span class="spinner-border spinner-border-sm flex-shrink-0 d-flex d-none me-2" role="status" aria-hidden="true"
data-frame-export-pdf-target="spinner"></span>
<span class="text-truncate d-inline-block">
<%= t("export_button.label") %>
</span>
</button>
Expand All @@ -19,26 +19,26 @@
<h6 class="dropdown-header"><%= t("export_button.exports.pdf") %></h6>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="front">
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="front">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.front") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="back">
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="back">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.back") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="front"
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="front"
data-bg-wiring="true">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.front_wiring") %>
</a>
</li>
<li>
<a role="button" class="dropdown-item" data-action="click->export-pdf#export" data-view-target="back"
<a role="button" class="dropdown-item" data-action="click->frame-export-pdf#export" data-view-target="back"
data-bg-wiring="true">
<span class="bi bi-file-earmark-pdf" aria-hidden="true"></span>
<%= t("export_button.faces.back_wiring") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/islets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<%= Islet.human_attribute_name(:frames_count, count: islet.frames.count) %>
<% end %>

<% table.with_column(style: "min-width: 140px; width: 140px") do |islet| %>
<% table.with_column(style: "min-width: 132px; width: 132px") do |islet| %>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<%= render partial: "islets/export_button", locals: { islet: islet } %>
<%= link_to edit_islet_path(islet), class: "btn btn-info", data: { turbo_frame: :_top } do %>
Expand Down
Loading