All files DayGridElement.js

80.86% Statements 93/115
60% Branches 6/10
53.33% Functions 8/15
80.35% Lines 90/112

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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 5701x 1x 1x       1x   1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               1x   1x   2x       19x 19x 19x   19x     19x 19x         19x       19x             19x   19x 19x         19x                       29199x                                                               19x 19x 19x       38x 38x 38x   38x 38x 38x 38x         38x 38x 38x 38x 38x 38x         38x 13870x 13870x     13870x               13870x 13870x   13870x     13870x                 13870x       13870x 13870x             13870x                                                                             13870x   13870x 13870x   13870x       13870x     13870x   456x 456x   456x           456x 456x 456x 456x           13870x     13870x           13870x 13870x 13870x 13870x             38x 190x 190x 190x       38x 38x 38x 38x                                         38x 456x   456x   456x           38x       266x     266x         19x   19x 19x         19x       1x      
const dayTemplate = document.createElement('template');
dayTemplate.id = 'day-grid-day-template';
dayTemplate.innerHTML = `<div class="day">
<div class="day-text"></div>
<div class="day-popup"></div>
</div>`;
document.body.append(dayTemplate);
 
const template = document.createElement('template');
template.id = 'day-grid-template';
template.innerHTML = `
<style>
  :host, :root {
   --day-size: 15px;
   --day-gap: 2px; 
   --day-border-radius: 8px;
   --day-font-size: 10px;
  
    --sg-color-1: 146, 182, 34;
    --sg-color-1: 46, 102, 234;
    --sg-color-1: 112, 59, 194;
    --sg-color-3: 239, 239, 239;
    --sg-opacity-1: 1;
    --sg-opacity-2: 0.75;
    --sg-opacity-3: 0.5;
    --sg-opacity-4: 0.25;
  
  }
  
  .contribution-grid {
    display: grid;
    grid-gap: var(--day-gap);
    grid-template-areas:
      " .  mon mon"
      "day sqr sqr"
      " .  key dyc";
    grid-template-columns: repeat(3, min-content);
    overflow-x: scroll;
    position: relative;
    padding: 30px 5px;
  }
  
  
  .contribution-grid-months {
    grid-area: mon;
  }
  
  .contribution-grid-days {
    grid-area: day;
  }
  
  .contribution-grid-squares {
    grid-area: sqr;
  }
  
  .contribution-grid-key {
    grid-area: key;
    display: flex;
    justify-items: center;
    align-items: center;
  }
  
  .contribution-grid-key-day {
    width: var(--day-size); 
    height: var(--day-size); 
    margin: var(--day-gap) var(--day-gap) var(--day-gap) 0;
    border-radius: var(--day-border-radius);
  }
  
  
  .contribution-grid-daycount {
    grid-area: dyc;
    place-self: end;
    display: flex;
    justify-items: center;
    align-items: center;
  }
  
  .contribution-grid-daycount-input {
    width: min-content;
    height: var(--day-size); 
    line-height: var(--day-size); 
  }
  
  
  .contribution-grid-months {
    display: flex;
    height: var(--day-size); 
  }
  
  .contribution-grid-months-month {
    position: absolute;
    transform: translate(8px,0) rotate(-25deg) ;
  }
  
  .contribution-grid-days {
    display: grid;
    grid-template-columns: 1fr; 
    width: var(--day-size);
    place-items: center;
    align: center;
    position: sticky;
    left: 0px;
 
  }
  
  .contribution-grid-days-day {
 
    /*position: absolute;*/
    align: center;
  }
  
  
  .contribution-grid-squares {
    display: grid;
    grid-gap: var(--day-gap);
    /* grid-template-columns: repeat(52, minmax( 0, 1fr));*/
    grid-template-rows: repeat(7, var(--day-size));
    grid-auto-flow: column; 
    grid-auto-columns: var(--day-size);
  }
  
  .day {
    border-radius: var(--day-border-radius);
    
    text-align: center;
    cursor:pointer;
  }
  
  .day-text {
    opacity: 1;
  }
  
  .day-popup {
    display: none;
  }
  
  * {
    font-size: var(--day-font-size);
  }
  
  /* levels */
  .level-0 {
    background: #eeeeee;
  }
  
  .level-1 {
    background: rgba(var(--sg-color-1), var(--sg-opacity-4)); 
  }
  
  .level-2 {
    background: rgba(var(--sg-color-1), var(--sg-opacity-3)); 
  }
  
  .level-3 {
    background: rgba(var(--sg-color-1), var(--sg-opacity-2)); 
  }
  
  .level-4 {
    background: rgba(var(--sg-color-1), var(--sg-opacity-1)); 
  }
  
  
  .contribution-grid-popup {
    display: none;
    min-height: calc( 2 * var(day-size));
    position: absolute;
    pointer-events: none;
  }
  
  .contribution-grid-popup-arrow {
    width: 15px;
    height: 15px;
  }
  
  .contribution-grid-popup-arrow path {
    fill: rgb(var(--sg-color-3));
    fill: black;
  }
  
  
  .contribution-grid-popup-contents {
    background: black;
    color: rgb(var(--sg-color-3));
    border-radius: var(--day-border-radius);
    padding: 8px;
    margin-bottom: -6px;
    font-size: 12px;
  }
  
  .contribution-grid-popup-date {
    font-size: 10px;
  }
  
  .contribution-grid-popup-content {
    font-size: 12px;
  }
  
  
  .arrow-down {
   transform: rotate(0deg); 
  }
  
  .arrow-left {
   transform: rotate(90deg); 
  }
  
  .arrow-right {
   transform: rotate(-90deg); 
  }
  
  .arrow-up {
   transform: rotate(180deg); 
  }
</style>
 
<div class="contribution-grid">
  <div class="contribution-grid-months"></div>
  <div class="contribution-grid-days"></div>
  <div class="contribution-grid-squares"></div>
  <div class="contribution-grid-key">
    <div class="contribution-grid-key-day">
      less
    </div>
    <div class="contribution-grid-key-day level-0">
    </div>
    <div class="contribution-grid-key-day level-1">
    </div>
    <div class="contribution-grid-key-day level-2">
    </div>
    <div class="contribution-grid-key-day level-3">
    </div>
    <div class="contribution-grid-key-day level-4">
    </div>
    <div class="contribution-grid-key-day">
     more 
    </div>
  </div>
 
  <div class="contribution-grid-daycount">
    <div class="contribution-grid-daycount-label">Days:</div>
    <input type="number" min="30" max="600" class="contribution-grid-daycount-input"></input>
  </div>
 
  <div class="contribution-grid-popup">
    <div class="contribution-grid-popup-contents">
      <div class="contribution-grid-popup-date">
      </div>
      <div class="contribution-grid-popup-content">
      </div>
    </div>
    <svg class="contribution-grid-popup-arrow arrow-down" width="50" height="50" version="1.1" viewBox="0 0 13.229 13.229" xmlns="http://www.w3.org/2000/svg">
      <path d="m3.3021 3.1259a0.61949 0.61949 0 0 0-0.53713 0.92893l3.3124 5.7375a0.61949 0.61949 0 0 0 1.0743 0l3.3124-5.7375a0.61949 0.61949 0 0 0-0.53713-0.92893z" color="#000000" color-rendering="auto" dominant-baseline="auto" image-rendering="auto" shape-rendering="auto" solid-color="#000000"/>
    </svg> 
  </div>
</div>
`;
 
document.body.append(template);
 
class DayGridElement extends HTMLElement {
  static get observedAttributes() {
    return ['founded', 'icon-only'];
  }
 
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this._template();
 
    let day_size = getComputedStyle(document.documentElement)
      .getPropertyValue('--day-size')
      .split('px')[0];
    Eif (day_size === '') {
      day_size = getComputedStyle(this._$Map.$cont)
        .getPropertyValue('--day-size')
        .split('px')[0];
    }
 
    this.css = {
      day_size: day_size,
    };
 
    this.stateMap = {
      day_count: 365,
      months: {},
      days: {},
      squares: {},
    };
 
    this.renderSquares();
 
    this._$Map.$daycount_input.value = this.stateMap.day_count;
    this._$Map.$daycount_input.addEventListener('change', () => {
      this.stateMap.day_count = parseInt(this._$Map.$daycount_input.value);
      this.renderSquares();
    });
 
    this.initialized = this._initModule();
  }
 
  attributeChangedCallback(name, oldValue, newValue) {
    this._template();
  }
 
  connectedCallback() {}
 
  disconnectedCallback() {}
 
  get _$Map() {
    return {
      $root: this.shadowRoot,
      $template: template,
      $day_template: dayTemplate,
 
      $cont: this.shadowRoot.querySelector('.contribution-grid'),
      $months: this.shadowRoot.querySelector('.contribution-grid-months'),
      $days: this.shadowRoot.querySelector('.contribution-grid-days'),
      $day: this.shadowRoot.querySelector('.contribution-grid-days-day'),
      $squares: this.shadowRoot.querySelector('.contribution-grid-squares'),
      $key: this.shadowRoot.querySelector('.contribution-grid-key'),
      $daycount: this.shadowRoot.querySelector('.contribution-grid-daycount'),
      $daycount_input: this.shadowRoot.querySelector(
        '.contribution-grid-daycount-input'
      ),
      $popup: this.shadowRoot.querySelector('.contribution-grid-popup'),
      $popup_contents: this.shadowRoot.querySelector(
        '.contribution-grid-popup-contents'
      ),
      $popup_date: this.shadowRoot.querySelector(
        '.contribution-grid-popup-date'
      ),
      $popup_content: this.shadowRoot.querySelector(
        '.contribution-grid-popup-content'
      ),
    };
  }
 
  /**
   * Clone and append template to $cont.
   */
  _template() {
    const $clone = this._$Map.$template.content.cloneNode(true);
    this._$Map.$root.innerHTML = '';
    this._$Map.$root.appendChild($clone);
  }
 
  renderSquares() {
    var month_year = new Date().getMonth() + '_' + new Date().getFullYear();
    var day_mod = new Date().getDay() % 7;
    var day_mod_fill = day_mod !== 0 ? 7 - day_mod : 0;
 
    var daycount_mod = (this.stateMap.day_count + day_mod_fill) % 7;
    var daycount_mod_fill = daycount_mod !== 0 ? 7 - daycount_mod : 0;
    daycount_mod_fill = daycount_mod_fill;
    Iif (daycount_mod_fill === 7) {
      daycount_mod_fill = 0;
    }
 
    // Clear Data.
    this._$Map.$squares.innerHTML = '';
    this._$Map.$months.innerHTML = '';
    this._$Map.$days.innerHTML = '';
    this.stateMap.months = {};
    this.stateMap.days = {};
    this.stateMap.squares = {};
 
    // Day Modulus.
 
    // Loop Days.
    for (var i = 0; i < this.stateMap.day_count; i++) {
      var d = new Date();
      d.setDate(d.getDate() - i);
 
      // Data Object
      this.stateMap.squares[i] = {
        day_number: i,
        date: d,
        level: Math.floor(Math.random() * 5),
        events: [],
      };
 
      // Squares Map.
      const $clone = this._$Map.$day_template.content.cloneNode(true);
      this.stateMap.squares[i].$cont = document.createElement('div');
      // this.stateMap.squares[i].$cont.classList.add("contribution-grid-months-month")
      this.stateMap.squares[i].$cont.appendChild($clone);
 
      // this.stateMap.squares[i].$cont = $clone;
      this.stateMap.squares[i].j_q = {
        $cont: this.stateMap.squares[i].$cont,
        $text: this.stateMap.squares[i].$cont.querySelector('.day-text'),
        $popup: this.stateMap.squares[i].$cont.querySelector('.day-popup'),
      };
 
      // Display
      // stateMap.squares[i].$cont.html(stateMap.squares[i].date.getDate());
      // stateMap.squares[i].$cont.html(stateMap.squares[i].date.toLocaleDateString("default", {"weekday": "short"}));
      this.stateMap.squares[i].$cont.setAttribute(
        'title',
        this.stateMap.squares[i].date.toLocaleDateString()
      );
      this.stateMap.squares[i].$cont.setAttribute('data-day-index', i);
      this.stateMap.squares[i].$cont.classList.add(
        'day',
        'day-' + i,
        'level-' + this.stateMap.squares[i].level
      );
 
      // Popup.
      let squareEvent = (e) => {
        var $target = e.target;
        var idx = $target.getAttribute('data-day-index');
 
        this._$Map.$popup_date.innerHTML =
          this.stateMap.squares[idx].date.toDateString();
        this._$Map.$popup_content.innerHTML =
          'Contributions ' + this.stateMap.squares[idx].level;
 
        this._$Map.$popup.style.display = 'initial';
 
        // Calculate postion.
        var day_size = parseInt(this.css.day_size);
        var popup_width = this._$Map.$popup.offsetWidth;
        var popup_height = this._$Map.$popup.offsetHeight;
        var square_top = this.stateMap.squares[idx].$cont.offsetTop;
        var square_left = this.stateMap.squares[idx].$cont.offsetLeft;
 
        var popup_position_left = square_left + day_size / 2 - popup_width / 2;
        var popup_position_top = square_top + day_size / 2 - popup_height;
 
        // this._$Map.$popup.css({
        //   display: "grid",
        //   "place-items" : "center",
        //   left: popup_position_left,
        //   top: popup_position_top
        // });
 
        let cssMap = {
          'display': 'grid',
          'place-items': 'center',
          'left': `${popup_position_left}px`,
          'top': `${popup_position_top}px`,
        };
        Object.entries(cssMap).map(([k, v]) => {
          this._$Map.$popup.style[k] = v;
        });
      };
 
      this.stateMap.squares[i].j_q.$popup.innerHTML =
        this.stateMap.squares[i].date.toDateString();
      this.stateMap.squares[i].$cont.addEventListener('click', squareEvent);
      this.stateMap.squares[i].$cont.addEventListener('mouseover', squareEvent);
 
      this.stateMap.squares[i].$cont.addEventListener('mouseout', (e) => {
        this._$Map.$popup.style.display = 'none';
      });
 
      this._$Map.$squares.prepend(this.stateMap.squares[i].$cont);
 
      // Month.
      if (d.getDate() === 1) {
        // var pos = this.stateMap.squares[i].$cont.offset();
        var month_date = new Date();
        month_date.setDate(month_date.getDate() - i);
 
        this.stateMap.months[month_year] = {
          month_number: month_date.getMonth(),
          date: month_date,
          day_index: i,
        };
 
        let $month = document.createElement('div');
        $month.classList.add('contribution-grid-months-month');
        this.stateMap.months[month_year].$cont = $month;
        this.stateMap.months[month_year].$cont.innerHTML =
          month_date.toLocaleDateString('default', { month: 'short' });
 
        // this.stateMap.months[month_year].$cont = document.createElement('div').classList.add("contribution-grid-months-month")
        // this.stateMap.months[month_year].$cont.html(month_date.toLocaleDateString("default", {"month": "short"}));
      }
      month_year = d.getMonth() + '_' + d.getFullYear();
 
      // Day.
      this.stateMap.days[d.getDay()] = {
        day_number: d.getDay(),
        date: d,
        day_index: i,
      };
 
      let $day = document.createElement('div');
      $day.classList.add('contribution-grid-days-day', 'day');
      this.stateMap.days[d.getDay()].$cont = $day;
      this.stateMap.days[d.getDay()].$cont.innerHTML = d.toLocaleDateString(
        'default',
        { weekday: 'short' }
      );
    }
 
    // Day padding.
    for (var i = 0; i < day_mod_fill; i++) {
      var $padding_cont = this._$Map.$day_template.content.cloneNode(true);
      $padding_cont.querySelector('.day').classList.add('level-0');
      this._$Map.$squares.appendChild($padding_cont);
    }
 
    // Daycount padding.
    for (var i = 0; i < daycount_mod_fill; i++) {
      var $padding_cont = this._$Map.$day_template.content.cloneNode(true);
      $padding_cont.querySelector('.day').classList.add('level-0');
      this._$Map.$squares.prepend($padding_cont);
    }
 
    // for (var i = 0; i < mod; i++){
    //   var $padding_cont = $(configMap.new_day_html);
    //   $padding_cont.addClass("level-0" );
    //   this._$Map.$squares.prepend($padding_cont);
    // }
 
    // Month Offest.
    // for (let [key, month_year] of Object.entries(this.stateMap.months)) {
    //   // month_year.$cont.css({
    //   //   left: this.stateMap.squares[month_year.day_index].$cont.offsetLeft
    //   // })
    //   month_year.$cont.style.left =
    //     this.stateMap.squares[month_year.day_index].$cont.offsetLeft;
 
    //   this._$Map.$months.prepend(month_year.$cont);
    // }
 
    // Month Offest.
    Object.entries(this.stateMap.months).map(([key, month_year]) => {
      let month_info = this.stateMap.months[key];
 
      this._$Map.$months.prepend(month_info.$cont);
 
      month_info.$cont.style.left = `${
        this.stateMap.squares[month_info.day_index].$cont.offsetLeft
      }px`;
    });
 
    // Day Offest.
    for (let [key, day] of Object.entries(this.stateMap.days)) {
      // day.$cont.css({
      //   top: this.stateMap.squares[day.day_index].$cont.offsetTop
      // })
      day.$cont.style.top = `${
        this.stateMap.squares[day.day_index].$cont.offsetTop
      }px`;
      this._$Map.$days.prepend(day.$cont);
    }
  }
 
  async _initModule() {
    this.renderSquares();
 
    this._$Map.$daycount_input.value = this.stateMap.day_count;
    this._$Map.$daycount_input.addEventListener('change', () => {
      this.stateMap.day_count = parseInt(this._$Map.$daycount_input.value);
      this.renderSquares();
    });
 
    return true;
  }
}
 
customElements.define('day-grid', DayGridElement);
 
export { DayGridElement };