diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb
index 84f22d6..ed45204 100644
--- a/app/controllers/vehicles_controller.rb
+++ b/app/controllers/vehicles_controller.rb
@@ -56,7 +56,6 @@ class VehiclesController < ApplicationController
end
def show
- @vin = @vehicle.vin.scan(/.{1,9}/) if @vehicle.vin
@issues = @vehicle.issues
.joins(:status)
.includes(:estimate, :invoices, :status, :project, :tracker, :priority)
diff --git a/app/views/issues/_show_issue_view_right.html.erb b/app/views/issues/_show_issue_view_right.html.erb
index 4301b78..e47a82b 100644
--- a/app/views/issues/_show_issue_view_right.html.erb
+++ b/app/views/issues/_show_issue_view_right.html.erb
@@ -6,7 +6,7 @@
-
<%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%>
+ <%= render partial: 'vehicles/vin', locals: { vin: vehicle.vin } %>
<% if show_customer %>
<%= vehicle.customer %>
<% end %>
diff --git a/app/views/vehicles/_vin.html.erb b/app/views/vehicles/_vin.html.erb
new file mode 100644
index 0000000..3da6497
--- /dev/null
+++ b/app/views/vehicles/_vin.html.erb
@@ -0,0 +1,10 @@
+<%#
+ A VIN that can be clicked (or Enter/Space) to copy it. 17-character VINs show the last 8 in bold,
+ which is what dealerships ask for when ordering parts; older or short VINs are shown plain.
+ The copy script is in vehicles/_copy_vin_script.
+ locals: vin
+%>
+<% if vin.present? %>
+ <% vin = vin.to_s %>
+
<% if vin.length == 17 %><%= vin[0, 9] %><%= vin[9, 8] %><% else %><%= vin %><% end %>
+<% end %>
diff --git a/assets/javascripts/copy.js b/assets/javascripts/copy.js
deleted file mode 100644
index ec2a85c..0000000
--- a/assets/javascripts/copy.js
+++ /dev/null
@@ -1,45 +0,0 @@
-async function handleCopy(event) {
- console.log("Copy link clicked");
-
- let text;
- let link;
- // Grab the text from our clicked link
- if(event.target.tagName.toLowerCase() === 'b'){
- text = event.target.parentElement.innerText;
- link = event.target.parentElement;
- } else {
- text = event.target.innerText;
- link = event.target;
- }
-
- // If the text is already "Copied!", don't do anything
- if (text == "Copied!") {
- return;
- }
-
- try {
- // Write to clipboard
- await navigator.clipboard.writeText(text);
-
- // Update the UI to show it worked
- const originalText = link.innerText;
- link.innerHTML = "
Copied!";
- link.style.color = "#4CAF50"; // Turn green
-
- // Reset after 2 seconds
- setTimeout(() => {
- // Check if the text is long enough to prevent errors
- if (originalText.length >= 8) {
- const firstPart = originalText.slice(0, -8);
- const lastEight = originalText.slice(-8);
- link.innerHTML = `${firstPart}
${lastEight}`;
- } else {
- link.innerText = originalText;
- }
- link.style.color = "";
- }, 2000);
-
- } catch (err) {
- console.error('Unable to copy', err);
- }
-}
\ No newline at end of file
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0bdd2d9..e73aed8 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -59,3 +59,6 @@ en:
notice_vin_decode_timed_out: "VIN decode timed out. Please refresh or try again."
text_mileage_custom_field_info: "Issue custom field used for \"Last serviced\" mileage. Leave blank to use a field named Mileage."
text_vin_already_on_file: "A vehicle with this VIN is already on file:"
+ text_click_to_copy: "Click to copy"
+ text_copied: "Copied!"
+ text_copy_failed: "Couldn't copy"
diff --git a/lib/vehicles/hooks/issues_show_hook_listener.rb b/lib/vehicles/hooks/issues_show_hook_listener.rb
index 1f72331..f130a07 100644
--- a/lib/vehicles/hooks/issues_show_hook_listener.rb
+++ b/lib/vehicles/hooks/issues_show_hook_listener.rb
@@ -19,8 +19,8 @@ module Vehicles
context[:controller].send(:render_to_string, {
partial: 'issues/show_issue_view_right',
locals: {
- vehicle: context[:issue].vehicle ? link_to(context[:issue].vehicle) : nil,
- split_vin: context[:issue].vehicle ? context[:issue].vehicle.vin.to_s.scan(/.{1,9}/) : nil
+ vehicle: context[:issue].vehicle ? link_to(context[:issue].vehicle) : nil,
+ vin: context[:issue].vehicle&.vin
}
})
end
diff --git a/lib/vehicles/hooks/pdf_hook_listener.rb b/lib/vehicles/hooks/pdf_hook_listener.rb
index bd6a1a8..3f8365b 100644
--- a/lib/vehicles/hooks/pdf_hook_listener.rb
+++ b/lib/vehicles/hooks/pdf_hook_listener.rb
@@ -20,10 +20,18 @@ module Vehicles
def pdf_left(context={})
output = []
output << [l(:field_vehicles), context[:issue].vehicle ? context[:issue].vehicle.to_s : nil]
- output << [l(:field_vin), context[:issue].vehicle ? context[:issue].vehicle.vin.gsub(/(.{9})/, '\1 ') : nil]
+ output << [l(:field_vin), context[:issue].vehicle ? pdf_vin(context[:issue].vehicle.vin) : nil]
output << [l(:field_notes), context[:issue].vehicle ? context[:issue].vehicle.notes : nil]
return output
end
+
+ private
+
+ # 17-character VINs are split before the last 8 (what dealerships ask for); others stay whole
+ def pdf_vin(vin)
+ vin = vin.to_s
+ vin.length == 17 ? "#{vin[0, 9]} #{vin[9, 8]}" : vin
+ end
end
end
diff --git a/lib/vehicles/hooks/view_hook_listener.rb b/lib/vehicles/hooks/view_hook_listener.rb
index c02eefc..2723d7d 100644
--- a/lib/vehicles/hooks/view_hook_listener.rb
+++ b/lib/vehicles/hooks/view_hook_listener.rb
@@ -15,7 +15,8 @@ module Vehicles
def view_layouts_base_html_head(context = {})
safe_join([
stylesheet_link_tag('style', plugin: :redmine_qbo_vehicles, media: :all),
- javascript_include_tag('copy', plugin: :redmine_qbo_vehicles)
+ # VIN click-to-copy, written into the page (see vehicles/_copy_vin_script)
+ context[:hook_caller].render(partial: 'vehicles/copy_vin_script')
])
end