Catalyst-Plugin-AutoCRUD

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/AutoCRUD/templates/extjs2/javascript/grid/columns.tt  view on Meta::CPAN

    // the column model has information about grid columns.
    // dataIndex maps the column to the specific data field in
    // the data store

    // returns a little icon which, when clicked on, redirects the browser to
    // the grid of a related table
    function createGridButton(id, table, url, pk) {
        new Ext.Button({
            cls: 'x-btn-text-icon'
            ,tooltip: 'Jump to this ' + table
            ,handler: function(btn, e) {
                Ext.DomHelper.append(
                    Ext.getBody()
                    ,{
                        tag: 'form'
                        ,name: 'transportform'
                        ,id: 'transportform'
                        ,action: url
                        ,method: 'POST'
                        ,cn: pk
                    }
                ).submit();
            }
            ,icon: '[% c.uri_for( c.controller('AutoCRUD::Static').action_for('cpacstatic'),
                                            "table_go.png" ) %]'
        }).render(document.body, id);
    }

    // text added to FK combo list to hint for the full-text-search option
    var fk_combo_comment = ' (all matches)';

    // create reusable renderer
    Ext.util.Format.comboRenderer = function(combo, table, field, url) {
        return function(value,metadata,record,rowindex,colindex,store) {
            // choose what to render from the combobox store's data
            if (rowindex === 0) {
                var rec = combo.findRecord(combo.valueField, value);
                var retval = rec ? rec.get(combo.displayField) : value;
                return (retval.indexOf(fk_combo_comment) !== -1) ? value : retval;
            }

            if (value) {
                // create an id, attached to a span, and hang a button off it
                var id = Ext.id();
                // if the related table is "hidden" we are not passed field,
                // so this get() fails and the plain value is displayed
                var pk = record.get('cpac__pk_for_' + field);
                if (pk) {
                    createGridButton.defer(1, this, [id, table, url, pk]);
                    return ('<span id="' + id + '"></span>' + value);
                }
                return value;
            }
            return '';
        }
    };

    // used to hack an extra entry into the store results on filters
    var sfyRecord = Ext.data.Record.create([
        { name: 'dbid' }, { name: 'stringified' }
    ]);

    // create the combo instances
    [% FOREACH col IN cpac.tc.cols %]
      [% NEXT UNLESS cpac.tm.f.$col.is_foreign_key OR cpac.tm.f.$col.extra('is_reverse') %]
      [% NEXT IF cpac.tm.f.$col.extra('masked_by') %]
      var fk_combo_[% col | replace('\'', '\\\'') %] = new Ext.form.ComboBox ({
          valueField: 'dbid'
          ,displayField: 'stringified'
          ,hiddenName: 'combobox.[% col | replace('\'', '\\\'') %]'
          ,hiddenId: 'fk_combo_[% col | replace('\'', '\\\'') %]'
          ,loadingText: 'Searching...'
          ,forceSelection: true
          ,selectOnFocus: true
          ,typeAhead: false
          ,pageSize: 10
          ,triggerAction: 'all'
          ,lazyRender: true
          ,listClass: 'x-combo-list-small'
          ,lastQuery: ''
          ,store: new Ext.data.JsonStore ({
              [% IF cpac.tm.f.$col.extra('rel_type') == 'many_to_many' %]
                url: '[% c.uri_for(
                            c.controller('AutoCRUD::DisplayEngine::ExtJS2').action_for('list_stringified'),
                            [ cpac.g.site, cpac_db, cpac.tm.f.$col.extra('via').0 ]
                     ) %]'
              [% ELSE %]
                url: '[% c.uri_for(
                            c.controller('AutoCRUD::DisplayEngine::ExtJS2').action_for('list_stringified'),
                            [ cpac.g.site, cpac_db, cpac_table ]
                     ) %]'
              [% END %]
              ,root: 'rows'
              ,totalProperty: 'total'
              ,fields: [ 'dbid', 'stringified' ]
              ,listeners: {
                  beforeload: function(store, options) {
                      var start = options.params.start;
                      var limit = options.params.limit;
                      options.params.page = Math.floor(start / limit) + 1;
                      [% IF cpac.tm.f.$col.extra('rel_type') == 'many_to_many' %]
                        options.params.fkname = '[% cpac.tm.f.$col.extra('via').1 | replace('\'', '\\\'') %]';
                      [% ELSE %]
                        options.params.fkname = '[% col | replace('\'', '\\\'') %]';
                      [% END %]
                      if (fk_combo_[% col | replace('\'', '\\\'') %].getRawValue() && (options.params.page === 1)) {
                          // so that we can hack in an extra row
                          options.params.limit = 9;
                      }
                      return true;
                  }
                  ,load: function(store, records, options) {
                      var queryText = fk_combo_[% col | replace('\'', '\\\'') %].getRawValue();
                      if (queryText.length && (options.params.page === 1)) {
                          // be sure not to accidentally add a 2nd copy of fk_combo_comment
                          if (queryText.indexOf(fk_combo_comment) !== -1) {
                              queryText = queryText.replace(fk_combo_comment,'');
                          }
                          // insert an extra row with full-text-search option
                          store.insert(0, new sfyRecord({
                              dbid: queryText
                              ,stringified: queryText + fk_combo_comment
                          }));
                      }
                  }
              }
          })
          ,listeners: {
              // delete the previous query in the beforequery event or set
              // combo.lastQuery = null (this will reload the store the next time it expands)



( run in 1.976 second using v1.01-cache-2.11-cpan-97f6503c9c8 )