summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/user_callout.js
blob: 74b869502a4a37d18a5158065a30f977e67f5f30 (plain)
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
/* global Cookies */

const userCalloutElementName = '.user-callout';
const closeButton = '.close-user-callout';
const userCalloutBtn = '.user-callout-btn';
const userCalloutSvgAttrName = 'callout-svg';

const USER_CALLOUT_COOKIE = 'user_callout_dismissed';

const USER_CALLOUT_TEMPLATE = `
  <div class="bordered-box landing content-block">
    <button class="btn btn-default close close-user-callout" type="button">
      <i class="fa fa-times dismiss-icon"></i>
    </button>
    <div class="row">
      <div class="col-sm-3 col-xs-12 svg-container">
      </div>
      <div class="col-sm-8 col-xs-12 inner-content">
        <h4>
          Customize your experience
        </h4>
        <p>
          Change syntax themes, default project pages, and more in preferences.
        </p>
        <a class="btn user-callout-btn" href="/profile/preferences">Check it out</a>
      </div>
  </div>
</div>`;

class UserCallout {
  constructor() {
    this.isCalloutDismissed = Cookies.get(USER_CALLOUT_COOKIE);
    this.userCalloutBody = $(userCalloutElementName);
    this.userCalloutSvg = $(userCalloutElementName).attr(userCalloutSvgAttrName);
    $(userCalloutElementName).removeAttr(userCalloutSvgAttrName);
    this.init();
  }

  init() {
    const $template = $(USER_CALLOUT_TEMPLATE);
    if (!this.isCalloutDismissed || this.isCalloutDismissed === 'false') {
      $template.find('.svg-container').append(this.userCalloutSvg);
      this.userCalloutBody.append($template);
      $template.find(closeButton).on('click', e => this.dismissCallout(e));
      $template.find(userCalloutBtn).on('click', e => this.dismissCallout(e));
    }
  }

  dismissCallout(e) {
    Cookies.set(USER_CALLOUT_COOKIE, 'true');
    const $currentTarget = $(e.currentTarget);
    if ($currentTarget.hasClass('close-user-callout')) {
      this.userCalloutBody.empty();
    }
  }
}

module.exports = UserCallout;