aboutsummaryrefslogtreecommitdiff
path: root/src/js/fiveui/js/url-pat.js
blob: d723021d5da106bdd05195757dd3aad02cd18fd3 (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
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

(function() {

/**
 * Create a new Url Pattern to map urls to Rule Sets.
 *
 * @constructor
 * @param {!number} id New id for this UrlPat.
 * @param {!string} regex The pattern that is used to match Urls.
 * @param {!number} rule_id Unique id of the RuleSet to use for matching URLs.
 */
fiveui.UrlPat = function(id, regex, rule_id) {
  this.id = id;
  this.regex = regex;
  this.rule_id = rule_id;
};

/**
 * Create a Url Pattern from a JSON object.
 *
 * @param {!number} id The id to use for the restored object.
 * @param {!Object} obj The object to take settings from.
 * @return {!fiveui.UrlPat} A populated UrlPat object.
 */
fiveui.UrlPat.fromJSON = function(id, obj) {
  return new fiveui.UrlPat(id, obj.regex, obj.rule_id);
};

/**
 * Create a regular expression from a globbed pattern.
 *
 * @param {!string} str The globbed url.
 * @return {!RegExp} A compiled regular expression.
 */
fiveui.UrlPat.compile = function(str) {
  var regex = str.replace(/\./g, '\.')
                 .replace(/\*/g, '.*');
  return new RegExp(regex);
};

/**
 * Test a string Url against the regular expression held in a Url Pattern.
 *
 * @param {!string} url The Url the string to test.
 * @return {!boolean} If the Url matched the regular expression.
 */
fiveui.UrlPat.prototype.match = function(url) {
  var pat = fiveui.UrlPat.compile(this.regex);
  return pat.test(url);
};



fiveui.UrlPatModel = Backbone.Model.extend({

  defaults: {
    id:       null,
    regex:    '',
    rule_id:  null,
  },

  sync:function(method, model, options) {
    _.defaults(options, {
      success:function() {},
      error:function() {}
    });

    var msg = model.url;
    var id  = model.get('id');

    switch(method) {
      case 'read':
        msg.send('getUrlPat', id, function(pat) {
          model.set(pat);
          options.success();
        });
        break;

      case 'update':
        msg.send('updateUrlPat', _.clone(model.attributes), options.success);
        break;

      case 'create':
        msg.send('addUrlPat', _.clone(model.attributes), options.success);
        break;

      case 'delete':
        msg.send('remUrlPat', id, function(res) {
          if(res) {
            options.success({});
          } else {
            options.error({});
          }
        });
        break;
    }
  }

}, {

  fromUrlPat: function(pat, msg) {
    return new fiveui.UrlPatModel({
      id:      pat.id,
      regex:   pat.regex,
      rule_id: pat.rule_id
    }, { url : msg });
  }

});


fiveui.UrlPats = Backbone.Collection.extend({

  model: fiveui.UrlPatModel,

  sync:function(method, collection, options) {

    _.defaults(options, {
      success:function() {},
      error:function() {}
    });

    var msg = this.url;

    switch(method) {

      case 'read':
        msg.send('getUrlPats', null, function(pats) {
          options.success(_.map(pats, function(pat) {
            return fiveui.UrlPatModel.fromUrlPat(pat, msg);
          }));
        });
        break;
    }

  }

});

})();