All files / src SunorhcTimelineTester.ts

0% Statements 0/329
0% Branches 0/1
0% Functions 0/1
0% Lines 0/329

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
export function setupTester(element: HTMLDivElement) {
  element.innerHTML = `
<div class="inline-flex items-center justify-center w-full">
  <hr class="w-full h-px my-4 bg-gray-300 border-0 dark:bg-gray-400">
  <h3 class="absolute px-3 font-medium text-gray-900 -translate-x-1/2 bg-white left-1/2 dark:text-white dark:bg-gray-900">Timeline Instance Controller</h3>
</div>
<div class="mt-4 mb-2 flex flex-wrap justify-start gap-4">
  <div class="flex flex-col items-start w-28 h-max">
    <label for="toggle-rtl" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Direction:</label>
    <label class="inline-flex items-center my-2 cursor-pointer">
      <input id="toggle-rtl" type="checkbox" name="dir-rtl" value="1" class="sr-only peer">
      <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>
      <span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">on RTL</span>
    </label>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-timezone" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">TimeZone:</label>
    <div for="select-timezone" class="w-36 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-timezone" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Example TimeZone</option>
        <option diff="-10"    value="America/Adak">America/Adak</option>
        <option diff="-7"     value="America/Phoenix">America/Phoenix</option>
        <option diff="-5"     value="America/New_York">America/New_York</option>
        <option diff="-3.5"   value="America/St_Johns">America/St_Johns</option>
        <option diff="0"      value="UTC">UTC (default)</option>
        <option diff="+1"     value="Europe/Berlin">Europe/Berlin</option>
        <option diff="+2"     value="Asia/Beirut">Asia/Beirut</option>
        <option diff="+4"     value="Asia/Dubai">Asia/Dubai</option>
        <option diff="+5.75"  value="Asia/Kathmandu">Asia/Kathmandu</option>
        <option diff="+8"     value="Asia/Shanghai">Asia/Shanghai</option>
        <option diff="+9"     value="Asia/Tokyo">Asia/Tokyo</option>
        <option diff="+11"    value="Asia/Vladivostok">Asia/Vladivostok</option>
        <option diff="+12.75" value="Pacific/Chatham">Pacific/Chatham</option>
        <option diff="+13"    value="Pacific/Tongatapu">Pacific/Tongatapu</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-outlined" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Outline:</label>
    <div for="select-outlined" class="w-36 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-outlined" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Outlined</option>
        <option value="none">"none" outline</option>
        <option value="inside">"inside" only</option>
        <option value="outside">"outside" only</option>
        <option value="both">"both" lines</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-elevation" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Elevation:</label>
    <div for="select-elevation" class="w-36 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-elevation" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Elevation None</option>
        <option value="0">Elevation 0</option>
        <option value="1">Elevation 1</option>
        <option value="2">Elevation 2</option>
        <option value="3">Elevation 3</option>
        <option value="4">Elevation 4</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-placement" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Placement:</label>
    <div for="select-placement" class="w-32 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-placement" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Sidebar & Ruler</option>
        <optgroup label="Sidebar">
          <option value="sidebar:both">Both</option>
          <option value="sidebar:left">Left only</option>
          <option value="sidebar:right">Right only</option>
          <option value="sidebar:none">None</option>
        </optgroup>
        <optgroup label="Ruler">
          <option value="ruler:both">Both</option>
          <option value="ruler:top">Top only</option>
          <option value="ruler:bottom">Bottom only</option>
          <option value="ruler:none">None</option>
        </optgroup>
      </select>
    </div>
  </div>

  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-canvas-layout" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Canvas Layout:</label>
    <div for="select-canvas-layout" class="w-32 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-canvas-layout" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Style None</option>
        <option value="striped">Striped</option>
        <option value="grid">Grid</option>
        <option value="toned">Toned</option>
        <option value="plaid">Plaid</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-start-weekday" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">First Day of Week:</label>
    <div for="select-start-weekday" class="w-32 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-start-weekday" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Choose Day</option>
        <option value="0">Sunday (default)</option>
        <option value="1">Monday</option>
        <option value="2">Tuesday</option>
        <option value="3">Wednesday</option>
        <option value="4">Thursday</option>
        <option value="5">Friday</option>
        <option value="6">Saturday</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-36 h-max">
    <label for="select-alignment" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Alignment:</label>
    <div for="select-alignment" class="w-32 h-max border border-blue-500 rounded-lg _focus:ring-blue-500 _focus:border-blue-500 dark:border-gray-600 _dark:focus:ring-blue-500 _dark:focus:border-blue-500">
      <select id="select-alignment" class="bg-gray-50 text-gray-900 text-sm rounded-lg block w-full p-2.5 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white">
        <option hidden>Alignment</option>
        <option value="left">Left or Begin</option>
        <option value="center">Center</option>
        <option value="right">Right or End</option>
        <option value="current">Current or Currently</option>
        <option value="latest">Latest</option>
        <option value="1">Numeric ID (e.g., 1)</option>
      </select>
    </div>
  </div>
  <div class="flex flex-col items-start w-28 h-max">
    <label for="run-reload" class="block mb-2 text-sm font-medium select-none cursor-none">&nbsp;</label>
    <button type="button" id="run-reload" class="h-10 px-3 py-2 text-xs font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Reload Timeline</button>
  </div>
  <div class="flex flex-col items-start w-28 h-max">
    <label for="grain-width" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Grain Width</label>
    <input type="text" id="grain-width" class="alnum-only bg-gray-50 border-solid border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="48px" pattern="[A-Za-z0-9\.]*" inputmode="latin" />
  </div>
  <div class="flex flex-col items-start w-28 h-max">
    <label for="row-height" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Row Height</label>
    <input type="text" id="row-height" class="alnum-only bg-gray-50 border-solid border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="80px" pattern="[A-Za-z0-9\.]*" inputmode="latin" />
  </div>
</div>
`

  const $TARGET_ELEMENTS: NodeListOf<HTMLDivElement> = document.querySelectorAll<HTMLDivElement>('.sunorhc-timeline-container')!
  if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    console.log('Dark mode now')
    $TARGET_ELEMENTS.forEach((elm: HTMLDivElement) => elm.classList.add('dark-theme'))
  } else {
    console.log('Light mode now')
    $TARGET_ELEMENTS.forEach((elm: HTMLDivElement) => elm.classList.remove('dark-theme'))
  }
  window?.matchMedia('(prefers-color-scheme: dark)')?.addEventListener('change', (event: MediaQueryListEvent) => {
    const isDarkMode: boolean = event.matches
    console.log('Changed mode:', isDarkMode ? 'dark' : 'light', event)
    $TARGET_ELEMENTS.forEach((elm: HTMLDivElement) => isDarkMode ? elm.classList.add('dark-theme') : elm.classList.remove('dark-theme'))
  })
  // Hook when changing mode with darkmode.js
  const $BTN_TOGGLE = document.querySelector<HTMLButtonElement>('.darkmode-toggle')!
  Object.assign($BTN_TOGGLE.style, { zIndex: 9999, boxShadow: '0px 5px 20px 1px rgba(0, 0, 0, 0.4)' })
  new MutationObserver(mutations => {
    for (const mutation of mutations) {
      if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
        const targetElm = mutation.target as HTMLBodyElement
        if (targetElm.classList.contains('darkmode--activated')) {
          console.log('Toggled to darkmode with darkmode.js', $TARGET_ELEMENTS)
          $TARGET_ELEMENTS.forEach((elm: HTMLDivElement) => elm.classList.add('dark-theme'))
        } else {
          console.log('Toggled to lightmode with darkmode.js', $TARGET_ELEMENTS)
          $TARGET_ELEMENTS.forEach((elm: HTMLDivElement) => elm.classList.remove('dark-theme'))
        }
      }
    }
  }).observe(document.body, { attributes: true, childList: false, subtree: false })

  // Toggle RTL
  element.querySelector<HTMLInputElement>('#toggle-rtl')!.addEventListener('change', (evt: Event) => {
    const $CHECKBOX = <HTMLInputElement>evt.currentTarget
    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const instance = window.SunorhcTimelineInstances[key]
        const layoutOptions = instance.getOptions().layout!
        layoutOptions.rtl = $CHECKBOX.checked
        //console.log('!!!:', layoutOptions)
        instance.reload({ layouts: layoutOptions })
      }
    }
    //console.log('!!!:', $CHECKBOX, $CHECKBOX.checked)
  })

  // Change TimeZone
  element.querySelector<HTMLSelectElement>('#select-timezone')!.addEventListener('change', (evt: Event) => {
    const $TIMEZONE = <HTMLSelectElement>evt.currentTarget

    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const instance = window.SunorhcTimelineInstances[key]
        console.log('!!!:', $TIMEZONE.value)
        instance.reload({ timezone: $TIMEZONE.value })
      }
    }
  })

  // Change Outlined
  element.querySelector<HTMLSelectElement>('#select-outlined')!.addEventListener('change', (evt: Event) => {
    const $SELECTED = <HTMLSelectElement>evt.currentTarget

    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const instance = window.SunorhcTimelineInstances[key]
        const layoutOptions = instance.getOptions().layout!
        layoutOptions.outlined = $SELECTED.value
        //console.log('!!!:', layoutOptions)
        instance.reload({ layouts: layoutOptions })
      }
    }
  })

  // Change Elevation
  element.querySelector<HTMLSelectElement>('#select-elevation')!.addEventListener('change', (evt: Event) => {
    const $TARGET_TIMELINE_CONTAINER: NodeListOf<HTMLDivElement> = document.querySelectorAll<HTMLDivElement>('.sunorhc-timeline-container')!
    $TARGET_TIMELINE_CONTAINER.forEach((elm: HTMLDivElement) => {
      const targetElm = <HTMLSelectElement>evt.currentTarget
      elm.dataset.timelineElevation = targetElm.value !== '' ? targetElm.value : ''
    })
  })

  // Change Sidebar & Ruler Placement
  element.querySelector<HTMLSelectElement>('#select-placement')!.addEventListener('change', (evt: Event) => {
    const $SELECTED = <HTMLSelectElement>evt.currentTarget
    const [ target, placement ] = $SELECTED.value.split(':')

    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const instance = window.SunorhcTimelineInstances[key]
        const nowOptions = instance.getOptions()
        let newOptions = null
        if (target === 'sidebar') {
          newOptions = { sidebar: nowOptions.sidebar! }
          newOptions.sidebar.placement = placement
        } else {
          newOptions = { ruler: nowOptions.ruler! }
          newOptions.ruler.placement = placement
        }
        console.log('!!!:', target, placement, newOptions)
        instance.reload(newOptions)
      }
    }
  })

  // Change Canvas Style
  element.querySelector<HTMLSelectElement>('#select-canvas-layout')!.addEventListener('change', (evt: Event) => {
    const $TARGET_TIMELINE_NODE_CANVAS: NodeListOf<HTMLDivElement> = document.querySelectorAll<HTMLDivElement>('.sunorhc-timeline-nodes')!
    $TARGET_TIMELINE_NODE_CANVAS.forEach((elm: HTMLDivElement) => {
      const targetElm = <HTMLSelectElement>evt.currentTarget
      elm.dataset.background = targetElm.value !== '' ? targetElm.value : ''
    })
  })

  // Change First Day of Week
  element.querySelector<HTMLSelectElement>('#select-start-weekday')!.addEventListener('change', (evt: Event) => {
    const $FIRST_DAY = <HTMLSelectElement>evt.currentTarget

    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const instance = window.SunorhcTimelineInstances[key]
        const rulerOptions = instance.getOptions().ruler!
        rulerOptions.firstDayOfWeek = Number($FIRST_DAY.value)
        console.log('!!!:', rulerOptions)
        instance.reload({ ruler: rulerOptions })
      }
    }
  })

  // Change Alignment
  element.querySelector<HTMLSelectElement>('#select-alignment')!.addEventListener('change', (evt: Event) => {
    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const targetElm = <HTMLSelectElement>evt.currentTarget
        window.SunorhcTimelineInstances[key].align(targetElm.value, (self: any) => {
          console.log('Do Alignment:', targetElm.value, self)
        })
      }
    }
  })

  // Execute Reload Timeline
  element.querySelector<HTMLButtonElement>('#run-reload')!.addEventListener('click', () => {
    if (window.hasOwnProperty('SunorhcTimelineInstances')) {
      for (const key in window.SunorhcTimelineInstances) {
        const focusComponent = document.getElementById(key)!
        //const cacheOffsetTop = focusComponent.offsetTop
        //window.scrollTo({ left: 0, top: cacheOffsetTop, behavior: 'smooth' })
        focusComponent.classList.add('preparing')
        setTimeout(() => {
          const nowOptions = window.SunorhcTimelineInstances[key].getOptions()
          let newOptions = { effects: { defaultAlignment: 'end' } }
          if (nowOptions.hasOwnProperty('extends') && nowOptions.extends.hasOwnProperty('originOptions')) {
            const originOptions = nowOptions.extends.originOptions
            const addOptions = {
              start:   originOptions.start,
              end:     originOptions.end,
              scale:   originOptions.scale,
              sidebar: originOptions.sidebar,
              ruler:   originOptions.ruler,

            }
            const grainWindth = parseInt(element.querySelector<HTMLInputElement>('#grain-width')!.value || '48', 10)
            const rowHeight   = parseInt(element.querySelector<HTMLInputElement>('#row-height')!.value || '80', 10)
            if (!isNaN(grainWindth)) {
              addOptions.ruler.minGrainWidth = `${grainWindth}px`
            }
            if (!isNaN(rowHeight)) {
              addOptions.sidebar.itemHeight = `${rowHeight}px`
            }
            newOptions = { ...newOptions, ...addOptions }
            console.log('originOptions:', addOptions, newOptions)
          } 
          window.SunorhcTimelineInstances[key].reload(newOptions, (self: any) => {
            console.log('Reloaded:', self)
            window.scrollTo({ left: 0, top: self.targetElement.offsetTop, behavior: 'smooth' })
          })
        }, 300)
      }
    }
  })

  element.querySelector<HTMLInputElement>('.alnum-only')!.addEventListener('input', (e: Event) => {
    const elm = <HTMLInputElement>e.currentTarget
    const value = elm.value
    elm.value = value.replace(/[^a-zA-Z0-9\.]/g, '')
  })

}