[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/application/herald/ -> HeraldRuleEditor.js (source)

   1  /**
   2   * @requires multirow-row-manager
   3   *           javelin-install
   4   *           javelin-util
   5   *           javelin-dom
   6   *           javelin-stratcom
   7   *           javelin-json
   8   *           phabricator-prefab
   9   * @provides herald-rule-editor
  10   * @javelin
  11   */
  12  
  13  JX.install('HeraldRuleEditor', {
  14    construct : function(config) {
  15      var root = JX.$(config.root);
  16      this._root = root;
  17  
  18      JX.DOM.listen(
  19        root,
  20        'click',
  21        'create-condition',
  22        JX.bind(this, this._onnewcondition));
  23  
  24      JX.DOM.listen(
  25        root,
  26        'click',
  27        'create-action',
  28        JX.bind(this, this._onnewaction));
  29  
  30      JX.DOM.listen(root, 'change', null, JX.bind(this, this._onchange));
  31      JX.DOM.listen(root, 'submit', null, JX.bind(this, this._onsubmit));
  32  
  33      var conditionsTable = JX.DOM.find(root, 'table', 'rule-conditions');
  34      var actionsTable = JX.DOM.find(root, 'table', 'rule-actions');
  35  
  36      this._conditionsRowManager = new JX.MultirowRowManager(conditionsTable);
  37      this._conditionsRowManager.listen(
  38        'row-removed',
  39        JX.bind(this, function(row_id) {
  40            delete this._config.conditions[row_id];
  41          }));
  42  
  43      this._actionsRowManager = new JX.MultirowRowManager(actionsTable);
  44      this._actionsRowManager.listen(
  45        'row-removed',
  46        JX.bind(this, function(row_id) {
  47            delete this._config.actions[row_id];
  48          }));
  49  
  50      this._conditionGetters = {};
  51      this._conditionTypes = {};
  52      this._actionGetters = {};
  53      this._actionTypes = {};
  54  
  55      this._config = config;
  56  
  57      var conditions = this._config.conditions;
  58      this._config.conditions = [];
  59  
  60      var actions = this._config.actions;
  61      this._config.actions = [];
  62  
  63      this._renderConditions(conditions);
  64      this._renderActions(actions);
  65    },
  66  
  67    members : {
  68      _config : null,
  69      _root : null,
  70      _conditionGetters : null,
  71      _conditionTypes : null,
  72      _actionGetters : null,
  73      _actionTypes : null,
  74      _conditionsRowManager : null,
  75      _actionsRowManager : null,
  76  
  77      _onnewcondition : function(e) {
  78        this._newCondition();
  79        e.kill();
  80      },
  81      _onnewaction : function(e) {
  82        this._newAction();
  83        e.kill();
  84      },
  85      _onchange : function(e) {
  86        var target = e.getTarget();
  87  
  88        var row = e.getNode(JX.MultirowRowManager.getRowSigil());
  89        if (!row) {
  90          // Changing the "when all of / any of these..." dropdown.
  91          return;
  92        }
  93  
  94        if (JX.Stratcom.hasSigil(target, 'field-select')) {
  95          this._onfieldchange(row);
  96        } else if (JX.Stratcom.hasSigil(target, 'condition-select')) {
  97          this._onconditionchange(row);
  98        } else if (JX.Stratcom.hasSigil(target, 'action-select')) {
  99          this._onactionchange(row);
 100        }
 101      },
 102      _onsubmit : function() {
 103        var rule = JX.DOM.find(this._root, 'input', 'rule');
 104  
 105        var k;
 106  
 107        for (k in this._config.conditions) {
 108          this._config.conditions[k][2] = this._getConditionValue(k);
 109        }
 110  
 111        for (k in this._config.actions) {
 112          this._config.actions[k][1] = this._getActionTarget(k);
 113        }
 114        rule.value = JX.JSON.stringify({
 115          conditions: this._config.conditions,
 116          actions: this._config.actions
 117        });
 118      },
 119  
 120      _getConditionValue : function(id) {
 121        if (this._conditionGetters[id]) {
 122          return this._conditionGetters[id]();
 123        }
 124        return this._config.conditions[id][2];
 125      },
 126  
 127      _getActionTarget : function(id) {
 128        if (this._actionGetters[id]) {
 129          return this._actionGetters[id]();
 130        }
 131        return this._config.actions[id][1];
 132      },
 133  
 134      _onactionchange : function(r) {
 135        var target = JX.DOM.find(r, 'select', 'action-select');
 136        var row_id = this._actionsRowManager.getRowID(r);
 137  
 138        this._config.actions[row_id][0] = target.value;
 139  
 140        var target_cell = JX.DOM.find(r, 'td', 'target-cell');
 141        var target_input = this._renderTargetInputForRow(row_id);
 142  
 143        JX.DOM.setContent(target_cell, target_input);
 144      },
 145      _onfieldchange : function(r) {
 146        var target = JX.DOM.find(r, 'select', 'field-select');
 147        var row_id = this._actionsRowManager.getRowID(r);
 148  
 149        this._config.conditions[row_id][0] = target.value;
 150  
 151        var condition_cell = JX.DOM.find(r, 'td', 'condition-cell');
 152        var condition_select = this._renderSelect(
 153          this._selectKeys(
 154            this._config.info.conditions,
 155            this._config.info.conditionMap[target.value]),
 156          this._config.conditions[row_id][1],
 157          'condition-select');
 158  
 159        JX.DOM.setContent(condition_cell, condition_select);
 160  
 161        this._onconditionchange(r);
 162  
 163        var condition_name = this._config.conditions[row_id][1];
 164        if (condition_name == 'unconditionally') {
 165          JX.DOM.hide(condition_select);
 166        }
 167      },
 168      _onconditionchange : function(r) {
 169        var target = JX.DOM.find(r, 'select', 'condition-select');
 170        var row_id = this._conditionsRowManager.getRowID(r);
 171  
 172        this._config.conditions[row_id][1] = target.value;
 173  
 174        var value_cell = JX.DOM.find(r, 'td', 'value-cell');
 175        var value_input = this._renderValueInputForRow(row_id);
 176        JX.DOM.setContent(value_cell, value_input);
 177      },
 178  
 179      _renderTargetInputForRow : function(row_id) {
 180        var action = this._config.actions[row_id];
 181        var type = this._config.info.targets[action[0]];
 182  
 183        var input = this._buildInput(type);
 184        var node = input[0];
 185        var get_fn = input[1];
 186        var set_fn = input[2];
 187  
 188        if (node) {
 189          JX.Stratcom.addSigil(node, 'action-target');
 190        }
 191  
 192        var old_type = this._actionTypes[row_id];
 193        if (old_type == type || !old_type) {
 194          set_fn(this._getActionTarget(row_id));
 195        }
 196  
 197        this._actionTypes[row_id] = type;
 198        this._actionGetters[row_id] = get_fn;
 199  
 200        return node;
 201      },
 202  
 203      _buildInput : function(type) {
 204        var input;
 205        var get_fn;
 206        var set_fn;
 207        switch (type) {
 208          case 'rule':
 209            input = this._renderSelect(this._config.template.rules);
 210            get_fn = function() { return input.value; };
 211            set_fn = function(v) { input.value = v; };
 212            break;
 213          case 'email':
 214          case 'user':
 215          case 'repository':
 216          case 'tag':
 217          case 'package':
 218          case 'project':
 219          case 'userorproject':
 220          case 'buildplan':
 221          case 'taskpriority':
 222          case 'taskstatus':
 223          case 'arcanistprojects':
 224          case 'legaldocuments':
 225            var tokenizer = this._newTokenizer(type);
 226            input = tokenizer[0];
 227            get_fn = tokenizer[1];
 228            set_fn = tokenizer[2];
 229            break;
 230          case 'none':
 231            input = '';
 232            get_fn = JX.bag;
 233            set_fn = JX.bag;
 234            break;
 235          case 'contentsource':
 236          case 'flagcolor':
 237          case 'value-ref-type':
 238          case 'value-ref-change':
 239            input = this._renderSelect(this._config.select[type].options);
 240            get_fn = function() { return input.value; };
 241            set_fn = function(v) { input.value = v; };
 242            set_fn(this._config.select[type]['default']);
 243            break;
 244          default:
 245            input = JX.$N('input', {type: 'text'});
 246            get_fn = function() { return input.value; };
 247            set_fn = function(v) { input.value = v; };
 248            break;
 249        }
 250  
 251        return [input, get_fn, set_fn];
 252      },
 253  
 254      _renderValueInputForRow : function(row_id) {
 255        var cond = this._config.conditions[row_id];
 256        var type = this._config.info.values[cond[0]][cond[1]];
 257  
 258        var input = this._buildInput(type);
 259        var node = input[0];
 260        var get_fn = input[1];
 261        var set_fn = input[2];
 262  
 263        if (node) {
 264          JX.Stratcom.addSigil(node, 'condition-value');
 265        }
 266  
 267        var old_type = this._conditionTypes[row_id];
 268        if (old_type == type || !old_type) {
 269          set_fn(this._getConditionValue(row_id));
 270        }
 271  
 272        this._conditionTypes[row_id] = type;
 273        this._conditionGetters[row_id] = get_fn;
 274  
 275        return node;
 276      },
 277  
 278      _newTokenizer : function(type) {
 279        var template = JX.$N(
 280          'div',
 281          JX.$H(this._config.template.markup));
 282        template = template.firstChild;
 283        template.id = '';
 284  
 285        var tokenizerConfig = {
 286          root : template,
 287          src : this._config.template.source[type].uri,
 288          placeholder: this._config.template.source[type].placeholder,
 289          icons : this._config.template.icons,
 290          username : this._config.username
 291        };
 292  
 293        var build = JX.Prefab.buildTokenizer(tokenizerConfig);
 294        build.tokenizer.start();
 295  
 296        return [
 297          template,
 298          function() {
 299            return build.tokenizer.getTokens();
 300          },
 301          function(map) {
 302            for (var k in map) {
 303              build.tokenizer.addToken(k, map[k]);
 304            }
 305          }];
 306      },
 307      _selectKeys : function(map, keys) {
 308        var r = {};
 309        for (var ii = 0; ii < keys.length; ii++) {
 310          r[keys[ii]] = map[keys[ii]];
 311        }
 312        return r;
 313      },
 314      _renderConditions : function(conditions) {
 315        for (var k in conditions) {
 316          this._newCondition(conditions[k]);
 317        }
 318      },
 319      _newCondition : function(data) {
 320        var row = this._conditionsRowManager.addRow([]);
 321        var row_id = this._conditionsRowManager.getRowID(row);
 322        this._config.conditions[row_id] = data || [null, null, ''];
 323        var r = this._conditionsRowManager.updateRow(
 324          row_id,
 325          this._renderCondition(row_id));
 326  
 327        this._onfieldchange(r);
 328      },
 329      _renderCondition : function(row_id) {
 330        var field_select = this._renderSelect(
 331          this._config.info.fields,
 332          this._config.conditions[row_id][0],
 333          'field-select');
 334        var field_cell = JX.$N('td', {sigil: 'field-cell'}, field_select);
 335  
 336        var condition_cell = JX.$N('td', {sigil: 'condition-cell'});
 337        var value_cell = JX.$N('td', {className : 'value', sigil: 'value-cell'});
 338  
 339        return [field_cell, condition_cell, value_cell];
 340      },
 341      _renderActions : function(actions) {
 342        for (var k in actions) {
 343          this._newAction(actions[k]);
 344          delete actions[k];
 345        }
 346      },
 347      _newAction : function(data) {
 348        data = data || [];
 349        var temprow = this._actionsRowManager.addRow([]);
 350        var row_id = this._actionsRowManager.getRowID(temprow);
 351        this._config.actions[row_id] = data;
 352        var r = this._actionsRowManager.updateRow(row_id,
 353                                                  this._renderAction(data));
 354        this._onactionchange(r);
 355      },
 356      _renderAction : function(action) {
 357        var action_select = this._renderSelect(
 358          this._config.info.actions,
 359          action[0],
 360          'action-select');
 361        var action_cell = JX.$N('td', {sigil: 'action-cell'}, action_select);
 362  
 363        var target_cell = JX.$N(
 364          'td',
 365          {className : 'target', sigil : 'target-cell'});
 366  
 367        return [action_cell, target_cell];
 368      },
 369      _renderSelect : function(map, selected, sigil) {
 370        var attrs = {
 371          sigil : sigil
 372        };
 373        return JX.Prefab.renderSelect(map, selected, attrs);
 374      }
 375    }
 376  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1