shimmer 0.0.16 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d82a8e015623e83b0381e182752ede22419e26c40b9a730536f6020793818b75
4
- data.tar.gz: 00766a05189358517092bcbc6ef958d26acbfae14efb98b45f81c3b848cfaffa
3
+ metadata.gz: adda5b02ed66f485505f07b24ba314621c5b0dd1cae9ce3093681651f0a1a509
4
+ data.tar.gz: 89c1baa878afdf2bb38c2cc62eeafd298f15916f11466e5ec1e31eb5b42f9be6
5
5
  SHA512:
6
- metadata.gz: d9e72dcab3004ea3c720a1b7041ee0714e4beded68cfa8caa9f5a1213fbe3ece9cf82030437be32c1532c85c67e489b159fa39f13392664c9c5f3520f8ba67f6
7
- data.tar.gz: e8e15ddcc1aedbad180f0b62dd11af6791d8ef35aa5139e7f0e7ffdead8551052f8ccbed36c143ae39868e44ca381ed27737e036d93dca8b9e5b8cffd125fae5
6
+ metadata.gz: f2add223e56adabf705dcb3061950ea47613d50caf4b7c240b3c7aa1907dbb0ef52b179b409183d2d205f21789d3cc15719e0ade1ba17387085eb47f9e2945d1
7
+ data.tar.gz: e496ccb035f571cd070c2af88968d0ab6d9d45bcf23016655a9a7213beccf9c83907f001f2f26c4188ce5ce1bc4affbd5c29d2b16d43d60a7949df0e9446f908
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shimmer
4
+ class ConsentSettings
5
+ SETTINGS = [:essential, :targeting, :statistic].freeze
6
+ DEFAULT = [:essential].freeze
7
+
8
+ SETTINGS.each do |setting|
9
+ attr_accessor setting
10
+ alias_method "#{setting}?", setting
11
+ end
12
+
13
+ def initialize(cookies)
14
+ @cookies = cookies
15
+ allowed = @cookies[:consent].to_s.split(",").map(&:strip)
16
+ SETTINGS.each do |setting|
17
+ instance_variable_set "@#{setting}", DEFAULT.include?(setting) || allowed.include?(setting.to_s)
18
+ end
19
+ end
20
+
21
+ def save
22
+ value = SETTINGS.map { |e| e.to_s if instance_variable_get("@#{e}") }.compact.join(",")
23
+ @cookies.permanent[:consent] = {value: value, expires: 2.years.from_now}
24
+ end
25
+
26
+ def given?
27
+ @cookies[:consent].present?
28
+ end
29
+ end
30
+
31
+ module Consent
32
+ extend ActiveSupport::Concern
33
+
34
+ included do
35
+ helper_method :consent_settings
36
+ def consent_settings
37
+ @consent_settings ||= ConsentSettings.new(cookies)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,6 +2,16 @@
2
2
 
3
3
  module Shimmer
4
4
  module FileHelper
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ ActiveSupport.on_load(:action_view) do
9
+ include Shimmer::FileAdditions
10
+ end
11
+ end
12
+ end
13
+
14
+ module FileAdditions
5
15
  def image_tag(source, **options)
6
16
  return nil if source.blank?
7
17
 
@@ -9,19 +19,27 @@ module Shimmer
9
19
  attachment = source
10
20
  width = options[:width]
11
21
  height = options[:height]
12
- source = image_file_url(source, width: width, height: height)
22
+ source = image_file_path(source, width: width, height: height)
13
23
  options[:loading] = :lazy
14
- options[:srcset] = "#{source} 1x, #{image_file_url(attachment, width: width.to_i * 2, height: height ? height.to_i * 2 : nil)} 2x" if options[:width].present?
24
+ options[:srcset] = "#{source} 1x, #{image_file_path(attachment, width: width.to_i * 2, height: height ? height.to_i * 2 : nil)} 2x" if options[:width].present?
15
25
  end
16
26
  super source, options
17
27
  end
18
28
 
29
+ def image_file_path(source, width: nil, height: nil)
30
+ image_file_proxy(source, width: width, height: height)&.path
31
+ end
32
+
19
33
  def image_file_url(source, width: nil, height: nil)
34
+ image_file_proxy(source, width: width, height: height)&.url
35
+ end
36
+
37
+ def image_file_proxy(source, width: nil, height: nil)
20
38
  return if source.blank?
21
39
  return source if source.is_a?(String)
22
40
 
23
41
  blob = source.try(:blob) || source
24
- Shimmer::FileProxy.new(blob_id: blob.id, width: width, height: height).url
42
+ Shimmer::FileProxy.new(blob_id: blob.id, width: width, height: height)
25
43
  end
26
44
  end
27
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Shimmer
4
- VERSION = "0.0.16"
4
+ VERSION = "0.0.19"
5
5
  end
data/src/consent.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { getCookie, setCookie } from "./util";
2
+
3
+ export class Consent {
4
+ get permitted(): string[] {
5
+ return (getCookie("consent") ?? "").split(",");
6
+ }
7
+
8
+ set permitted(settings: string[]) {
9
+ setCookie("consent", settings.join(","));
10
+ }
11
+
12
+ get given(): boolean {
13
+ return getCookie("consent") !== undefined;
14
+ }
15
+
16
+ permitAll(): void {
17
+ this.permitted = ["essential", "targeting", "statistic"];
18
+ }
19
+
20
+ denyAll(): void {
21
+ this.permitted = ["essential"];
22
+ }
23
+
24
+ showSettings(): void {
25
+ const element = document.getElementById("personalization-settings");
26
+ if (element) element.hidden = false;
27
+ }
28
+ }
@@ -0,0 +1,51 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ export default class extends Controller {
4
+ static targets = ["check"];
5
+ declare checkTargets: HTMLInputElement[];
6
+
7
+ connect(): void {
8
+ this.checkTargets.forEach((input) => {
9
+ input.checked =
10
+ window.ui?.consent.permitted.includes(input.name) ||
11
+ input.name === "essential";
12
+ });
13
+ }
14
+
15
+ permitAll(event: Event): void {
16
+ event.preventDefault();
17
+ window.ui?.consent.permitAll();
18
+ this.closeAll();
19
+ }
20
+
21
+ denyAll(event: Event): void {
22
+ event.preventDefault();
23
+ window.ui?.consent.denyAll();
24
+ this.closeAll();
25
+ }
26
+
27
+ save(event: Event): void {
28
+ event.preventDefault();
29
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
30
+ window.ui!.consent.permitted = this.checkTargets
31
+ .filter((e) => e.checked)
32
+ .map((e) => e.name);
33
+ this.closeAll();
34
+ }
35
+
36
+ manage(event: Event): void {
37
+ event.preventDefault();
38
+ const div = document.body.querySelector(
39
+ "#personalization-settings"
40
+ ) as HTMLDivElement;
41
+ if (!div) return;
42
+ (this.element as HTMLElement).hidden = true;
43
+ div.hidden = false;
44
+ }
45
+
46
+ closeAll(): void {
47
+ document
48
+ .querySelectorAll("[data-controller='consent']")
49
+ .forEach((e: HTMLElement) => (e.hidden = true));
50
+ }
51
+ }
data/src/index.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import type { Application } from "@hotwired/stimulus";
2
2
  import { ModalPresenter } from "./modal";
3
3
  import { PopoverPresenter } from "./popover";
4
+ import { Consent } from "./consent";
4
5
  import RemoteNavigationController from "./controllers/remote-navigation";
6
+ import ConsentController from "./controllers/consent";
5
7
  import "./touch";
6
8
 
7
9
  export { registerServiceWorker } from "./serviceworker";
@@ -12,6 +14,7 @@ declare global {
12
14
  ui?: {
13
15
  modal: ModalPresenter;
14
16
  popover: PopoverPresenter;
17
+ consent: Consent;
15
18
  };
16
19
  }
17
20
  }
@@ -33,8 +36,10 @@ export async function start({
33
36
  window.addEventListener("turbo:load", createRemoteDestination);
34
37
  createRemoteDestination();
35
38
  application.register("remote-navigation", RemoteNavigationController);
39
+ application.register("consent", ConsentController);
36
40
  window.ui = {
37
41
  modal: new ModalPresenter(),
38
42
  popover: new PopoverPresenter(),
43
+ consent: new Consent(),
39
44
  };
40
45
  }
data/src/util.ts CHANGED
@@ -44,3 +44,22 @@ export function wait(seconds: number): Promise<void> {
44
44
  setTimeout(res, seconds * 1000);
45
45
  });
46
46
  }
47
+
48
+ export function getCookie(key: string): string | null {
49
+ if (!document.cookie) return null;
50
+ return (
51
+ document.cookie
52
+ .split(";")
53
+ .map((v) => v.split("="))
54
+ .reduce((acc, v) => {
55
+ acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
56
+ return acc;
57
+ }, {})[key] ?? null
58
+ );
59
+ }
60
+
61
+ export function setCookie(key: string, value: string): void {
62
+ const date = new Date();
63
+ date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000);
64
+ document.cookie = `${key}=${value}; expires=${date.toUTCString()}`;
65
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shimmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ravens
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-01 00:00:00.000000000 Z
11
+ date: 2022-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -154,6 +154,7 @@ files:
154
154
  - lib/shimmer/tasks/lint.rake
155
155
  - lib/shimmer/tasks/s3.rake
156
156
  - lib/shimmer/utils/config.rb
157
+ - lib/shimmer/utils/consent_settings.rb
157
158
  - lib/shimmer/utils/file_helper.rb
158
159
  - lib/shimmer/utils/file_proxy.rb
159
160
  - lib/shimmer/utils/localizable.rb
@@ -163,6 +164,8 @@ files:
163
164
  - lib/shimmer/version.rb
164
165
  - package.json
165
166
  - rollup.config.js
167
+ - src/consent.ts
168
+ - src/controllers/consent.ts
166
169
  - src/controllers/remote-navigation.ts
167
170
  - src/index.ts
168
171
  - src/locale.ts