1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15/**
16 * Class Sticky
17 * Manage the stickiness of component
18 * with https://sticksy.js.org/
19 * @package ComboStrap
20 */
21class Position
22{
23    const STICKY_ATTRIBUTE = "sticky";
24    const STICKY = "sticky";
25    const STICKY_CLASS = "combo-sticky";
26
27    const POSITION_ATTRIBUTE = "position";
28    const POSITION_SNIPPET_ID = "position";
29    const POSITION_QUARTILE_SNIPPET_ID = "position-quartile";
30
31    /**
32     * Process stickiness
33     * @param TagAttributes $attributes
34     */
35    public static function processStickiness(&$attributes)
36    {
37        if ($attributes->hasComponentAttribute(self::STICKY_ATTRIBUTE)) {
38            $sticky = strtolower($attributes->getValueAndRemove(self::STICKY_ATTRIBUTE));
39            if ($sticky == "true") {
40                $stickyClass = self::STICKY_CLASS;
41                $attributes->addClassName($stickyClass);
42                $snippetManager = PluginUtility::getSnippetManager();
43                $snippetManager->attachRemoteJavascriptLibrary(
44                    self::STICKY,
45                    "https://cdn.jsdelivr.net/npm/sticksy@0.2.0/dist/sticksy.min.js",
46                    "sha256-H6uQ878/jyt6w1oBNhL6s01iAfWxACrWvVXCBjZsrGM="
47                );
48                /**
49                 * If top bar
50                 */
51                $jsSnippet = <<<EOF
52let fixedNavbar = document.querySelector(".navbar.fixed-top");
53let topSpacing = fixedNavbar.offsetHeight;
54var stickyElements = Sticksy.initializeAll('.$stickyClass',{topSpacing: topSpacing})
55EOF;
56                $snippetManager
57                    ->attachJavascriptFromComponentId(self::STICKY)
58                    ->setInlineContent($jsSnippet);
59            }
60
61        }
62
63    }
64
65    /**
66     * @param TagAttributes $attributes
67     */
68    public static function processPosition(TagAttributes &$attributes)
69    {
70        if ($attributes->hasComponentAttribute(self::POSITION_ATTRIBUTE)) {
71            $position = strtolower($attributes->getValueAndRemove(self::POSITION_ATTRIBUTE));
72            if (Bootstrap::getBootStrapMajorVersion() < Bootstrap::BootStrapFiveMajorVersion) {
73                $snippetManager = PluginUtility::getSnippetManager();
74                $snippetManager->attachCssInternalStyleSheet(self::POSITION_SNIPPET_ID);
75            }
76
77            // Class value comes from
78            // https://getbootstrap.com/docs/5.0/utilities/position/#center-elements
79            switch ($position) {
80                case "top-left":
81                case "left-top":
82                    $attributes->addClassName("position-absolute top-0 start-0 translate-middle");
83                    break;
84                case "top-quartile-1":
85                case "quartile-1-top":
86                    self::addQuartileCss();
87                    $attributes->addClassName("position-absolute top-0 start-25 translate-middle");
88                    break;
89                case "top-center":
90                case "center-top":
91                    $attributes->addClassName("position-absolute top-0 start-50 translate-middle");
92                    break;
93                case "top-quartile-3":
94                case "quartile-3-top":
95                    self::addQuartileCss();
96                    $attributes->addClassName("position-absolute top-0 start-75 translate-middle");
97                    break;
98                case "top-right":
99                case "right-top":
100                    $attributes->addClassName("position-absolute top-0 start-100 translate-middle");
101                    break;
102                case "left-quartile-1":
103                case "quartile-1-left":
104                    self::addQuartileCss();
105                    $attributes->addClassName("position-absolute top-25 start-0 translate-middle");
106                    break;
107                case "left-center":
108                case "center-left":
109                    $attributes->addClassName("position-absolute top-50 start-0 translate-middle");
110                    break;
111                case "center-center":
112                case "center":
113                    $attributes->addClassName("position-absolute top-50 start-50 translate-middle");
114                    break;
115                case "right-center":
116                case "center-right":
117                    $attributes->addClassName("position-absolute top-50 start-100 translate-middle");
118                    break;
119                case "bottom-left":
120                case "left-bottom":
121                    $attributes->addClassName("position-absolute top-100 start-0 translate-middle");
122                    break;
123                case "bottom-center":
124                case "center-bottom":
125                    $attributes->addClassName("position-absolute top-100 start-50 translate-middle");
126                    break;
127                case "bottom-right":
128                case "right-bottom":
129                    $attributes->addClassName("position-absolute top-100 start-100 translate-middle");
130                    break;
131                case "relative":
132                case "absolute":
133                case "fixed":
134                case "static":
135                    /**
136                     * The css value are also supported
137                     * (to define that for instance the parent of a background should be at minimum relative)
138                     */
139                    $attributes->addStyleDeclarationIfNotSet("position", $position);
140                    break;
141
142            }
143        }
144
145    }
146
147    private static function addQuartileCss()
148    {
149        $snippetManager = PluginUtility::getSnippetManager();
150        $snippetManager->attachCssInternalStyleSheet(self::POSITION_QUARTILE_SNIPPET_ID);
151    }
152}
153