aboutsummaryrefslogtreecommitdiff
path: root/src/js/fiveui/js/options.js
blob: c4ec16d306335da836712f2531dff692281a7113 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * Module     : options.js
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Portable
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var fiveui = fiveui || {};

(function() {


/*****************************************************************
 *
 * Misc. utility functions
 *
 ****************************************************************/

var setClickHandler = function(sel, fn, scope) {
  var boundFn = fn;
  if (scope) {
    boundFn = _.bind(fn, scope);
  }

  sel.on('click', boundFn)
};

/**
 * @param {!string} selectEltId The id of the select element to
 * search for selected items.
 * @return {!Array.<!number>} An array of 'value' entries from the
 * selected elements.  This may be empty, but it will not be null.
 */
var findSelectedIds = function(elt) {
  var opts = elt.find('option:checked');
  return _.map(opts, function(o) { return o.value; });
};


fiveui.options = fiveui.options || {};

/**
 * @param {{on: function(!string, function(*)), emit: function(!string, *)}} port
 */
fiveui.options.init = function(port) {

  var msg    = new fiveui.Messenger(port);
  var update = new fiveui.UpdateManager(msg);

  ruleSets = new fiveui.RuleSets([], { url: msg });


  /** RuleSet list entries ***************************************************/

  var ruleSetEntries = jQuery('#ruleSetEntries');

  // handle clicks to the 'add' button on the rule sets page
  jQuery('#addRsButton')
    .button({
      icons: { primary: 'ui-icon-plus' },
    })
    .on('click', function() {
      ruleSets.add(new fiveui.RuleSetModel({}, { url : msg }));
    });

  // render a ruleset added to the collection
  ruleSets.on('add', function(model) {
    var entry = new fiveui.RuleSetEntry({ model: model })
    ruleSetEntries.append(entry.$el);

    if(model.isNew()) {
      entry.edit();
    } else {
      entry.render();
    }
  });


  /** Basics *****************************************************************/

  var windowDisplayDefault = jQuery('#windowDisplayDefault');

  windowDisplayDefault.on('change', function() {
    msg.send('setDisplayDefault', windowDisplayDefault.prop('checked'))
  });


  /** Tab Management *********************************************************/

  /**
   * Select a tab header by Element reference.
   *
   * @param {!Element} clicked The navigation element to focus.
   * @return {void}
   */
  var selectNav = function(clicked) {
    var nav = clicked.parent();

    nav.find('div.selected', nav).removeClass('selected');

    jQuery('div.editorPane').hide();

    clicked.addClass('selected');
  };

  /**
   * Focus a tab, by Element reference.
   *
   * @param {!Element} el The tab element to focus.
   * @return {void}
   */
  var selectSection = function(el) {
    var cont = el.parent();

    // hide all sections
    cont.find('>section').removeClass('selected').hide();

    // display this section
    el.addClass('selected').show();
  };

  /**
   * A combination of selectNav and selectSection that will first lookup the
   * elements associated with a tab, then return a new function that when
   * called, will focus both the navigation element, and the tab it controls.
   *
   * @param {!string} id The id of the tab to focus.
   * @return {function()}
   */
  var select = function(id) {
    var sel = jQuery(id);
    return function() {
      selectNav(jQuery(this));
      selectSection(sel);
    };
  };

  // listen to click events on navigation elements
  setClickHandler(jQuery('#rule-sets'),    select('#tab-rule-sets'));
  setClickHandler(jQuery('#basics'),       select('#tab-basics'));

  // select the rule sets tab
  selectNav(jQuery('#rule-sets'));
  selectSection(jQuery('#tab-rule-sets'));


  /** Pre-populate UI elements ***********************************************/

  msg.send('getDisplayDefault', null, function(def) {
    jQuery('#windowDisplayDefault').prop('checked', def);
  });

  // pre-populate the rule sets
  ruleSets.fetch();
};


  /** Rule-Set Views *********************************************************/

var editable = function(el, placeholder, onEnter) {

  el.prop('contenteditable', true).addClass('editable');

  // prevent newlines
  if(onEnter) {
    el.on('keypress', function(e) {
      if(e.which == 13) {
        onEnter();
        return false;
      } else {
        return true;
      }
    });
  } else {
    el.on('keypress', function(e) {
      return e.which != 13;
    });
  }

  var addPlaceholder = function() {
    el.addClass('placeholder')
      .text(placeholder)
      .one('click keypress paste', remPlaceholder);
  };

  var remPlaceholder = function() {
    el.removeClass('placeholder').text('');
  };

  // if the model is new, set the placeholder, and a listener to clear it
  if(el.text() == '') {
    addPlaceholder();
  }

  el.on('blur', function() {
    if(el.text() == '') {
      addPlaceholder();
    }
  });

  el.focus();

};

var button = function(el, icon) {
  el.button({ icons: icon, text: false });
};


/** Rule Set View ************************************************************/

fiveui.RulesView = Backbone.View.extend({

  tagName: 'select',

  initialize:function() {
    this.listenTo(this.model, 'sync',   this.update);
    this.listenTo(this.model, 'remove', this.update);
  },

  optionTemplate:_.template(
    '<option value="<%= id %>"><%= name %></option>'
  ),

  update:function() {
    if(this.model.length == 0) {
      return this.remove();
    } else {
      return this.render();
    }
  },

  remove:function() {
    this.stopListening();
    this.$el.remove();

    this.trigger('remove');

    return this;
  },

  render:function() {

    var scope = this;

    this.$el.children().remove();

    var text = this.model.foldl(function(body,ruleSet) {
      return body + scope.optionTemplate(ruleSet.attributes);
    }, '');

    this.$el.html(text);

    return this;
  },

});


/** Rule Entry Elements ******************************************************/

fiveui.RuleSetEntry = Backbone.View.extend({

  tagName: 'li',

  className: 'entry',

  // setup the skeleton for the rule set editor.
  initialize:function() {
    this.$el.html(
      [ '<div class="rule-set">'
      , '</div>'
      , '<ul class="patterns"></ul>'
      , '<div class="pattern-control">'
      , '  <div class="pattern-input"></div>'
      , '  <button class="add-pattern">add url pattern</button'
      , '</div>'
      ].join(''));

    this.$rs     = this.$el.find('.rule-set');
    this.$pat    = this.$el.find('.patterns');
    this.$urlpat = this.$el.find('.pattern-input');
    this.$addpat = this.$el.find('.add-pattern');

    // setup the url pattern editor
    this.$addpat.button({ icons: { primary: 'ui-icon-plus' } });
    editable(this.$urlpat, 'http://example.com/*', _.bind(function() {
      this.$addpat.click();
      this.$urlpat.focus();
    }, this));
  },

  events: {
    'click .save'        : 'save',
    'click .remove'      : 'remove',
    'click .edit'        : 'edit',
    'click .reload'      : 'reload',
    'click .add-pattern' : 'addPattern',
  },

  viewRsTemplate: _.template(
    [ '<div class="container">'
    , '  <button class="remove">remove</button>'
    , '  <button class="edit">edit</button>'
    , '  <button class="reload">reload</button>'
    , '</div>'
    , '<span class="title"><%= name %><%= license %></span>'
    ].join('')),

  // render the rule set as its title, with some buttons to edit/remove/reload
  // it.
  render:function() {

    // render the rule set
    var attrs = _.clone(this.model.attributes);

    // add a comma to the license field, if it's present.
    if(!_.isEmpty(attrs.license)) {
      attrs.license = ', ' + attrs.license;
    }

    this.$rs.html(this.viewRsTemplate(attrs));

    button(this.$rs.find('.edit'),   { primary: 'ui-icon-pencil'  });
    button(this.$rs.find('.reload'), { primary: 'ui-icon-refresh' });
    button(this.$rs.find('.remove'), { primary: 'ui-icon-close'   });

    this.renderPats(this.model.get('patterns'));

    this.$addpat.prop('disabled', false);

    return this;
  },

  editTemplate: _.template(
    [ '<div class="container">'
    , '  <button class="remove">x</button>'
    , '  <button class="save">save</button>'
    , '</div>'
    , '<span class="source"><%= source %></span>'
    ].join('')),

  // rework the rule set display area to a single input field for the url, and a
  // remove and save button.
  edit:function() {
    var attrs = this.model.attributes;
    this.$rs.html(this.editTemplate(attrs));

    button(this.$rs.find('.remove'), { primary: 'ui-icon-close' });

    var save = this.$rs.find('.save');
    button(save, { primary: 'ui-icon-disk' });

    this.$addpat.prop('disabled', true);

    editable(this.$rs.find('.source'), 'http://example.com/manifest.json',
      _.bind(save.click, save));

    return this;
  },

  errorTemplate: _.template('<div class="error"><%= message %></div>'),

  // render an error message below the edit ui for the result sets, but before
  // any url patterns.
  editError:function(target, message) {
    this.edit();
    this.$rs.append(this.errorTemplate({ message: message }));

    return this;
  },

  // save the current model, falling back on the editor dialog when errors show
  // up.  it's assumed that this is only called from the editor dialog.
  save: function() {
    var source = this.$el.find('.source').text();
    this.model.set('source', source);
    this.model.save({}, {
      success: _.bind(this.render,    this),
      error:   _.bind(this.editError, this)
    });
  },

  // reaload the model, and render.  on failure, display the edit dialog with a
  // message.
  reload:function() {
    this.model.save({}, {
      success: _.bind(this.render,    this),
      error:   _.bind(this.editError, this)
    });
  },

  // remove the model, and remove the element from the list.
  remove:function() {
    this.model.destroy();
    this.$el.remove();
    this.stopListening();
  },



  viewPatTemplate: _.template(
    [ '<li>'
    , '  <button class="remove-pat">remove</button>'
    , '  <span class="pattern"><%= pattern %></span>'
    , '</li>'
    ].join('')),

  addUrlPat:function(pat) {
    var el = jQuery(this.viewPatTemplate({ pattern: pat }));

    var remove = el.find('.remove-pat');

    button(remove, { primary: 'ui-icon-close' });
    remove.on('click', _.bind(this.removePattern, this, pat));

    this.$pat.append(el);
  },

  // render the url patterns of the rule set into this.$pat.
  renderPats:function(pats) {
    this.$pat.children().remove();

    _.each(pats, _.bind(this.addUrlPat, this))
    return this;
  },

  // add a pattern to the underlying collection of patterns
  addPattern:function() {
    var pat  = this.$urlpat.text();
    var pats = this.model.get('patterns');

    pats.push(pat);

    this.model.save({ patterns : pats }, {
      wait:    true,
      patch:   true,
      success: _.bind(function() {
        this.$urlpat.text('').blur();
        this.render();
      }, this),
      error: function(msg) {
        debugger;
      },
    });
  },

  removePattern:function(pat) {
    var pats = _.filter(this.model.get('patterns'), function(p) {
      return p != pat;
    });

    this.model.save({ patterns: pats }, {
      wait:   true,
      patch:  true,
      success:_.bind(this.render, this),
      // XXX make this report an actual error
      error:  _.bind(this.render, this)
    });
  },

});

})();