[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/application/owners/ -> OwnersPathEditor.js (source)

   1  /**
   2   * @requires multirow-row-manager
   3   *           javelin-install
   4   *           path-typeahead
   5   *           javelin-dom
   6   *           javelin-util
   7   *           phabricator-prefab
   8   * @provides owners-path-editor
   9   * @javelin
  10   */
  11  
  12  JX.install('OwnersPathEditor', {
  13    construct : function(config) {
  14      var root = JX.$(config.root);
  15  
  16      this._rowManager = new JX.MultirowRowManager(
  17        JX.DOM.find(root, 'table', config.table));
  18  
  19      JX.DOM.listen(
  20        JX.DOM.find(root, 'a', config.add_button),
  21        'click',
  22        null,
  23        JX.bind(this, this._onaddpath));
  24  
  25      this._count = 0;
  26      this._repositories = config.repositories;
  27      this._inputTemplate = config.input_template;
  28  
  29      this._completeURI = config.completeURI;
  30      this._validateURI = config.validateURI;
  31      this._repositoryDefaultPaths = config.repositoryDefaultPaths;
  32  
  33      this._initializePaths(config.pathRefs);
  34    },
  35    members : {
  36      /*
  37       * MultirowRowManager for controlling add/remove behavior
  38       */
  39      _rowManager : null,
  40  
  41      /*
  42       * Array of objects with 'name' and 'repo_id' keys for
  43       * selecting the repository of a path.
  44       */
  45      _repositories : null,
  46  
  47      /*
  48       * How many rows have been created, for form name generation.
  49       */
  50      _count : 0,
  51      /*
  52       * URL for the typeahead datasource.
  53       */
  54      _completeURI : null,
  55      /*
  56       * URL for path validation requests.
  57       */
  58      _validateURI : null,
  59      /*
  60       * Template typeahead markup to be copied per row.
  61       */
  62      _inputTemplate : null,
  63      /*
  64       * Most packages will be in one repository, so remember whenever
  65       * the user chooses a repository, and use that repository as the
  66       * default for future rows.
  67       */
  68      _lastRepositoryChoice : null,
  69  
  70      _repositoryDefaultPaths : null,
  71  
  72      /*
  73       * Initialize with 0 or more rows.
  74       * Adds one initial row if none are given.
  75       */
  76      _initializePaths : function(path_refs) {
  77        for (var k in path_refs) {
  78          this.addPath(path_refs[k]);
  79        }
  80        if (!JX.keys(path_refs).length) {
  81          this.addPath();
  82        }
  83      },
  84  
  85      /*
  86       * Build a row.
  87       */
  88      addPath : function(path_ref) {
  89        // Smart default repository. See _lastRepositoryChoice.
  90        if (path_ref) {
  91          this._lastRepositoryChoice = path_ref.repositoryPHID;
  92        }
  93        path_ref = path_ref || {};
  94  
  95        var selected_repository = path_ref.repositoryPHID ||
  96          this._lastRepositoryChoice;
  97        var options = this._buildRepositoryOptions(selected_repository);
  98        var attrs = {
  99          name : 'repo[' + this._count + ']',
 100          className : 'owners-repo'
 101        };
 102        var repo_select = JX.$N('select', attrs, options);
 103  
 104        JX.DOM.listen(repo_select, 'change', null, JX.bind(this, function() {
 105          this._lastRepositoryChoice = repo_select.value;
 106        }));
 107  
 108        var repo_cell = JX.$N('td', {}, repo_select);
 109        var typeahead_cell = JX.$N(
 110          'td',
 111          JX.$H(this._inputTemplate));
 112  
 113        // Text input for path.
 114        var path_input = JX.DOM.find(typeahead_cell, 'input');
 115        JX.copy(
 116          path_input,
 117          {
 118            value : path_ref.path || '',
 119            name : 'path[' + this._count + ']'
 120          });
 121  
 122        // The Typeahead requires a display div called hardpoint.
 123        var hardpoint = JX.DOM.find(
 124          typeahead_cell,
 125          'div',
 126          'typeahead-hardpoint');
 127  
 128        var error_display = JX.$N(
 129          'div',
 130          {
 131            className : 'error-display validating'
 132          },
 133          'Validating...');
 134  
 135        var error_display_cell = JX.$N('td', {}, error_display);
 136  
 137        var exclude = JX.Prefab.renderSelect(
 138          {'0' : 'Include', '1' : 'Exclude'},
 139          path_ref.excluded,
 140          {name : 'exclude[' + this._count + ']'});
 141        var exclude_cell = JX.$N('td', {}, exclude);
 142  
 143        var row = this._rowManager.addRow(
 144          [exclude_cell, repo_cell, typeahead_cell, error_display_cell]);
 145  
 146        new JX.PathTypeahead({
 147          repositoryDefaultPaths : this._repositoryDefaultPaths,
 148          repo_select : repo_select,
 149          path_input : path_input,
 150          hardpoint : hardpoint,
 151          error_display : error_display,
 152          completeURI : this._completeURI,
 153          validateURI : this._validateURI}).start();
 154  
 155        this._count++;
 156        return row;
 157      },
 158  
 159      _onaddpath : function(e) {
 160        e.kill();
 161        this.addPath();
 162      },
 163  
 164      /**
 165       * Helper to build the options for the repository choice dropdown.
 166       */
 167      _buildRepositoryOptions : function(selected) {
 168        var repos = this._repositories;
 169        var result = [];
 170        for (var k in repos) {
 171          var attr = {
 172            value : k,
 173            selected : (selected == k)
 174          };
 175          result.push(JX.$N('option', attr, repos[k]));
 176        }
 177        return result;
 178      }
 179    }
 180  });


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