summaryrefslogtreecommitdiff
path: root/components/jetpack/sdk/context-menu/readers.js
blob: 5078f8f29d2128e92786d5bb1e2c8aafb99a433c (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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { Class } = require("../core/heritage");
const { extend } = require("../util/object");
const { memoize, method, identity } = require("../lang/functional");

const serializeCategory = ({type}) => ({ category: `reader/${type}()` });

const Reader = Class({
  initialize() {
    this.id = `reader/${this.type}()`
  },
  toJSON() {
    return serializeCategory(this);
  }
});


const MediaTypeReader = Class({ extends: Reader, type: "MediaType" });
exports.MediaType = MediaTypeReader;

const LinkURLReader = Class({ extends: Reader, type: "LinkURL" });
exports.LinkURL = LinkURLReader;

const SelectionReader = Class({ extends: Reader, type: "Selection" });
exports.Selection = SelectionReader;

const isPageReader = Class({ extends: Reader, type: "isPage" });
exports.isPage = isPageReader;

const isFrameReader = Class({ extends: Reader, type: "isFrame" });
exports.isFrame = isFrameReader;

const isEditable = Class({ extends: Reader, type: "isEditable"});
exports.isEditable = isEditable;



const ParameterizedReader = Class({
  extends: Reader,
  readParameter: function(value) {
    return value;
  },
  toJSON: function() {
    var json = serializeCategory(this);
    json[this.parameter] = this[this.parameter];
    return json;
  },
  initialize(...params) {
    if (params.length) {
      this[this.parameter] = this.readParameter(...params);
    }
    this.id = `reader/${this.type}(${JSON.stringify(this[this.parameter])})`;
  }
});
exports.ParameterizedReader = ParameterizedReader;


const QueryReader = Class({
  extends: ParameterizedReader,
  type: "Query",
  parameter: "path"
});
exports.Query = QueryReader;


const AttributeReader = Class({
  extends: ParameterizedReader,
  type: "Attribute",
  parameter: "name"
});
exports.Attribute = AttributeReader;

const SrcURLReader = Class({
  extends: AttributeReader,
  name: "src",
});
exports.SrcURL = SrcURLReader;

const PageURLReader = Class({
  extends: QueryReader,
  path: "ownerDocument.URL",
});
exports.PageURL = PageURLReader;

const SelectorMatchReader = Class({
  extends: ParameterizedReader,
  type: "SelectorMatch",
  parameter: "selector"
});
exports.SelectorMatch = SelectorMatchReader;

const extractors = new WeakMap();
extractors.id = 0;


var Extractor = Class({
  extends: ParameterizedReader,
  type: "Extractor",
  parameter: "source",
  initialize: function(f) {
    this[this.parameter] = String(f);
    if (!extractors.has(f)) {
      extractors.id = extractors.id + 1;
      extractors.set(f, extractors.id);
    }

    this.id = `reader/${this.type}.for(${extractors.get(f)})`
  }
});
exports.Extractor = Extractor;