<commit_before>import angular from "angular";
import ngAnimate from "angular-animate";
import { actionCreators } from "../actions/actions.js";
import { attach, toAbsoluteURL } from "../utils.js";

import { connect2Redux, generateSvgQrCode } from "../won-utils.js";
import { ownerBaseUrl } from "config";

import "style/_post-share-link.scss";

const serviceDependencies = ["$scope", "$ngRedux", "$element"];
function genComponentConf() {
  let template = `
        <div class="psl__content">
            <p class="psl__text" ng-if="self.post.get('connections').size == 0 && self.post.get('ownNeed')">
                Your posting has no connections yet. Consider sharing the link below in social media, or wait for matchers to connect you with others.
            </p>
            <p class="psl__text" ng-if="(self.post.get('connections').size != 0 && self.post.get('ownNeed')) || !self.post.get('ownNeed')">
                Know someone who might also be interested in this posting? Consider sharing the link below in social media.
            </p>
            <div class="psl__tabs">
              <div class="psl__tabs__tab clickable" ng-class="{'psl__tabs__tab--selected': self.showLink}" ng-click="self.showLink = true">Link</div>
              <div class="psl__tabs__tab clickable" ng-class="{'psl__tabs__tab--selected': !self.showLink}" ng-click="self.showLink = false">QR-Code</div>
            </div>
            <div class="psl__link" ng-if="self.showLink">
              <div class="psl__link__copyfield" ng-if="self.showLink">
                <input class="psl__link__copyfield__input" value="{{self.linkToPost}}" readonly type="text" ng-focus="self.selectLink()" ng-blur="self.clearSelection()">
                <button class="red won-button--filled psl__link__copyfield__copy-button" ng-click="self.copyLink()">
                  <svg class="psl__link__copyfield__copy-button__icon" style="--local-primary:white;">
                    <use xlink:href="{{ self.copied === true ? '#ico16_checkmark' : '#ico16_copy_to_clipboard'}}" href="{{ self.copied ? '#ico16_checkmark' : '#ico16_copy_to_clipboard'}}"></use>
                  </svg>
                </button>
              </div>
            </div>
            <div class="psl__qrcode" ng-if="!self.showLink">
              <img class="psl__qrcode__code" src="data:image/svg+xml;utf8,{{self.svgQrCodeToPost}}"/>
            </div>
        </div>
    `;

  class Controller {
    constructor() {
      attach(this, serviceDependencies, arguments);
      window.postShareLink4dbg = this;

      const selectFromState = state => {
        const post = this.postUri && state.getIn(["needs", this.postUri]);
        this.showLink = true;

        let linkToPost;
        if (ownerBaseUrl && post) {
          const path = "#!post/" + `?postUri=${encodeURI(post.get("uri"))}`;

          linkToPost = toAbsoluteURL(ownerBaseUrl).toString() + path;
        }
        let svgQrCodeToPost = generateSvgQrCode(linkToPost);

        return {
          post,
          linkToPost,
          svgQrCodeToPost,
        };
      };
      connect2Redux(selectFromState, actionCreators, ["self.postUri"], this);
    }

    getLinkField() {
      if (!this._linkField) {
        this._linkField = this.$element[0].querySelector(".psl__link");
      }
      return this._linkField;
    }

    selectLink() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.setSelectionRange(0, linkEl.value.length);
      }
    }

    clearSelection() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.setSelectionRange(0, 0);
      }
    }

    copyLink() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.focus();
        linkEl.setSelectionRange(0, linkEl.value.length);
        if (!document.execCommand("copy")) {
          window.prompt("Copy to clipboard: Ctrl+C", linkEl.value);
        } else {
          linkEl.setSelectionRange(0, 0);
          linkEl.blur();

          //Temprorarily show checkmark
          this.copied = true;<commit_msg>Fixed share link exceptions<commit_after>import angular from "angular";
import ngAnimate from "angular-animate";
import { actionCreators } from "../actions/actions.js";
import { attach, toAbsoluteURL } from "../utils.js";

import { connect2Redux, generateSvgQrCode } from "../won-utils.js";
import { ownerBaseUrl } from "config";

import "style/_post-share-link.scss";

const serviceDependencies = ["$scope", "$ngRedux", "$element"];
function genComponentConf() {
  let template = `
        <div class="psl__content">
            <p class="psl__text" ng-if="self.post.get('connections').size == 0 && self.post.get('ownNeed')">
                Your posting has no connections yet. Consider sharing the link below in social media, or wait for matchers to connect you with others.
            </p>
            <p class="psl__text" ng-if="(self.post.get('connections').size != 0 && self.post.get('ownNeed')) || !self.post.get('ownNeed')">
                Know someone who might also be interested in this posting? Consider sharing the link below in social media.
            </p>
            <div class="psl__tabs">
              <div class="psl__tabs__tab clickable" ng-class="{'psl__tabs__tab--selected': self.showLink}" ng-click="self.showLink = true">Link</div>
              <div class="psl__tabs__tab clickable" ng-class="{'psl__tabs__tab--selected': !self.showLink}" ng-click="self.showLink = false">QR-Code</div>
            </div>
            <div class="psl__link" ng-if="self.showLink">
              <div class="psl__link__copyfield" ng-if="self.showLink">
                <input class="psl__link__copyfield__input" value="{{self.linkToPost}}" readonly type="text" ng-focus="self.selectLink()" ng-blur="self.clearSelection()">
                <button class="red won-button--filled psl__link__copyfield__copy-button" ng-click="self.copyLink()">
                  <svg ng-if="self.copied" class="psl__link__copyfield__copy-button__icon" style="--local-primary:white;">
                    <use xlink:href="#ico16_checkmark" href="#ico16_checkmark"></use>
                  </svg>
                  <svg ng-if="!self.copied" class="psl__link__copyfield__copy-button__icon" style="--local-primary:white;">
                    <use xlink:href="#ico16_copy_to_clipboard" href="#ico16_copy_to_clipboard"></use>
                  </svg>
                </button>
              </div>
            </div>
            <div class="psl__qrcode" ng-if="!self.showLink">
              <img class="psl__qrcode__code" src="data:image/svg+xml;utf8,{{self.svgQrCodeToPost}}"/>
            </div>
        </div>
    `;

  class Controller {
    constructor() {
      attach(this, serviceDependencies, arguments);
      window.postShareLink4dbg = this;

      const selectFromState = state => {
        const post = this.postUri && state.getIn(["needs", this.postUri]);
        this.showLink = true;

        let linkToPost;
        if (ownerBaseUrl && post) {
          const path = "#!post/" + `?postUri=${encodeURI(post.get("uri"))}`;

          linkToPost = toAbsoluteURL(ownerBaseUrl).toString() + path;
        }
        let svgQrCodeToPost = generateSvgQrCode(linkToPost);

        return {
          post,
          linkToPost,
          svgQrCodeToPost,
        };
      };
      connect2Redux(selectFromState, actionCreators, ["self.postUri"], this);
    }

    getLinkField() {
      if (!this._linkField) {
        this._linkField = this.$element[0].querySelector(
          ".psl__link__copyfield__input"
        );
      }
      return this._linkField;
    }

    selectLink() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.setSelectionRange(0, linkEl.value.length);
      }
    }

    clearSelection() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.setSelectionRange(0, 0);
      }
    }

    copyLink() {
      const linkEl = this.getLinkField();
      if (linkEl) {
        linkEl.focus();
        linkEl.setSelectionRange(0, linkEl.value.length);
        if (!document.execCommand("copy")) {
          window.prompt("Copy to clipboard: Ctrl+C", linkEl.value);
        } else {
          linkEl.setSelectionRange(0, 0);
          linkEl.blur();

          //Temprorarily show checkmark
          this.copied = true;
{'fraction_non_alphanumeric': 0.1452346743295019, 'fraction_numerical': 0.005028735632183908, 'mean_word_length': 2.5170526315789474, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2373389', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2493, 'n_words': 489}