aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/assets/js/context_a.js
blob: b39a6542a2ea1c26882e54919dcd6ebb00f8380a (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
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
"use strict";

(function () {
  // context menu for links
  // largely mimics click_a.js
  // we will also bind a listener here to notify the activity not to deal with viewpager scrolls
  var _frostAContext, longClick;

  longClick = false;

  _frostAContext = function _frostAContext(e) {
    /*
     * Commonality; check for valid target
     */
    var element, image, imageUrl, img, imgUrl, text, url;
    if (typeof Frost !== "undefined" && Frost !== null) {
      Frost.longClick(true);
    }
    longClick = true;
    element = e.target || e.currentTarget || e.srcElement;
    if (!element) {
      return;
    }
    if (element.tagName !== "A") {
      element = element.parentNode;
    }
    //Notifications is two layers under
    if (element.tagName !== "A") {
      element = element.parentNode;
    }
    if (element.tagName === "A" && element.getAttribute("href") !== "#") {
      url = element.getAttribute("href");
      if (!url) {
        return;
      }
      text = element.parentNode.innerText;
      // check if image item exists, first in children and then in parent
      image = element.querySelector("[style*=\"background-image: url(\"]");
      if (!image) {
        image = element.parentNode.querySelector("[style*=\"background-image: url(\"]");
      }
      if (image) {
        imageUrl = window.getComputedStyle(image, null).backgroundImage.trim().slice(4, -1);
        console.log("Context image: " + imageUrl);
        if (typeof Frost !== "undefined" && Frost !== null) {
          Frost.loadImage(imageUrl, text);
        }
        e.stopPropagation();
        e.preventDefault();
        return;
      }
      // check if true img exists
      img = element.querySelector("img[src*=scontent]");
      if (img) {
        imgUrl = img.src;
        console.log("Context img " + imgUrl);
        if (typeof Frost !== "undefined" && Frost !== null) {
          Frost.loadImage(imgUrl, text);
        }
        e.stopPropagation();
        e.preventDefault();
        return;
      }
      console.log("Context Content " + url + " " + text);
      if (typeof Frost !== "undefined" && Frost !== null) {
        Frost.contextMenu(url, text);
      }
      e.stopPropagation();
      e.preventDefault();
    }
  };

  document.addEventListener("contextmenu", _frostAContext, true);

  document.addEventListener("touchend", function (e) {
    if (longClick) {
      if (typeof Frost !== "undefined" && Frost !== null) {
        Frost.longClick(false);
      }
      longClick = false;
    }
  }, true);
}).call(undefined);