1 Ext.ns('Gilbert.lib.plugins.models.ui');
4 Ext.override(Gilbert.lib.models.Model, {
5 create_new_form: function (callback, config) {
8 model.api.get_form({}, function (formspec) {
9 var formspec = formspec;
10 for (var item_index in formspec.items) {
11 var item = formspec.items[item_index];
16 var form_panel = new Gilbert.lib.ui.DjangoForm(Ext.applyIf(Ext.applyIf(config||{},{
17 title: 'New '+model.verbose_name,
23 submit: model.api.save_form,
29 create_edit_form: function (callback, pk, config) {
32 model.api.get_form({'pk': pk}, function (formspec) {
33 var formspec = formspec;
34 for (var item_index in formspec.items) {
35 var item = formspec.items[item_index];
40 callback(new Gilbert.lib.ui.DjangoForm(Ext.applyIf(Ext.applyIf(config||{},{
41 title: 'Editing '+model.verbose_name+' ('+pk+')',
43 iconCls: 'icon-pencil',
47 submit: model.api.save_form,
58 Gilbert.lib.plugins.models.ui.ForeignKeyColumn = Ext.extend(Ext.grid.Column, {
59 renderer: function(v) {
65 Ext.grid.Column.types['foreignkeycolumn'] = Gilbert.lib.plugins.models.ui.ForeignKeyColumn
68 Gilbert.lib.plugins.models.ui.ModelPanel = Ext.extend(Ext.Panel, {
69 constructor: function (model, plugin, config) {
70 var model = this.model = model;
71 var plugin = this.plugin = plugin;
72 var application = this.application = plugin.application;
75 var store = this.store = model.create_store({
85 var grid = this.grid = new Ext.grid.GridPanel({
86 ddGroup: model.drag_drop_group,
90 columns: model.columns,
96 selModel: new Ext.grid.RowSelectionModel(),
97 bbar: new Ext.PagingToolbar({
101 displayMsg: 'Displaying '+model.verbose_name_plural+' {0} - {1} of {2}',
102 emptyMsg: 'No '+model.verbose_name_plural+' to display',
103 items: (function () {
104 if (model.searchable) {
107 xtype: 'tbseparator',
109 new Ext.ux.form.SearchField({
120 var new_action = this.new_action = new Ext.Action({
121 text: 'New ' + model.verbose_name,
122 iconCls: 'icon-plus',
123 handler: function () {
124 plugin.create_instance_window(model, undefined, function (win) {
125 win.on('saved', function () {
133 var edit_action = this.edit_action = new Ext.Action({
136 iconCls: 'icon-pencil',
137 handler: function () {
138 Ext.each(grid.getSelectionModel().getSelections(), function (record, index) {
139 plugin.create_instance_window(model, record.id, function (win) {
140 win.on('saved', function () {
149 var delete_action = this.delete_action = new Ext.Action({
152 iconCls: 'icon-minus',
153 handler: function () {
154 var records = grid.getSelectionModel().getSelections();
156 Ext.each(records, function (record, index) {
159 model.api.data_destroy_consequences(pks, function (consequences) {
160 var convert_consequences_array = function (consequences) {
161 var last_parent = consequences[0];
162 Ext.each(consequences, function (consequence, index) {
164 if (!Ext.isArray(consequence)) {
165 last_parent = consequence;
167 last_parent['children'] = convert_consequences_array(consequence);
168 delete consequences[index];
172 new_consequences = [];
173 Ext.each(consequences, function (consequence) {
175 var new_consequence = {};
176 if (!consequence['children']) {
177 new_consequence['leaf'] = true;
179 new_consequence['leaf'] = false;
180 new_consequence['children'] = consequence['children'];
182 var app_label = consequence['app_label'];
183 var name = consequence['name'];
184 var model = Gilbert.get_model(app_label, name);
186 new_consequence['text'] = consequence['__unicode__'];
187 new_consequence['iconCls'] = model.iconCls;
189 new_consequence['text'] = '(' + consequence['name'] + ') ' + consequence['__unicode__'];
190 new_consequence['iconCls'] = 'icon-block';
192 new_consequence['disabled'] = true;
193 new_consequences.push(new_consequence);
196 return new_consequences;
199 var tree = this.tree = new Ext.tree.TreePanel({
200 loader: new Ext.tree.TreeLoader(),
203 trackMouseOver: false,
207 'text': 'To be deleted',
208 'iconCls': 'icon-minus',
210 'children': convert_consequences_array(consequences),
217 var consequences_win = application.create_window({
222 title: 'Delete ' + model.verbose_name_plural,
223 iconCls: 'icon-minus',
228 html: 'Are you sure you want to delete these ' + model.verbose_name_plural + '?',
229 bodyStyle: 'padding: 15px;',
237 handler: function () {
238 consequences_win.close();
245 handler: function () {
246 consequences_win.close();
247 store.remove(records);
255 consequences_win.show();
260 grid.on('cellcontextmenu', function (grid, rowIndex, cellIndex, e) {
262 selmodel = grid.getSelectionModel();
263 if (!selmodel.isSelected(rowIndex)) {
264 selmodel.selectRow(rowIndex, false);
266 var contextmenu = new Ext.menu.Menu({
272 contextmenu.showAt(e.xy);
275 grid.on('rowdblclick', function(grid, rowIndex, e) {
276 var record = grid.getStore().getAt(rowIndex)
277 plugin.create_instance_window(model, record.id, function (win) {
278 win.on('saved', function () {
285 grid.getSelectionModel().on('selectionchange', function (selmodel) {
286 if (selmodel.hasSelection()) {
287 edit_action.setDisabled(false);
288 delete_action.setDisabled(false);
290 edit_action.setDisabled(true);
291 delete_action.setDisabled(true);
295 Gilbert.lib.plugins.models.ui.ModelPanel.superclass.constructor.call(this, Ext.applyIf(config||{}, {
297 tbar: new Ext.Toolbar({
300 { xtype: 'tbseparator' },
306 iconCls: 'icon-gear',
318 Gilbert.lib.plugins.models.Plugin = Ext.extend(Gilbert.lib.plugins.Plugin, {
320 init: function (application) {
321 Gilbert.lib.plugins.models.Plugin.superclass.init.call(this, application);
323 var new_menu = this.new_menu = new Ext.menu.Menu();
324 var manage_menu = this.manage_menu = new Ext.menu.Menu();
326 application.mainmenu.insert(2, {
328 iconCls: 'icon-plus',
333 application.mainmenu.insert(3, {
335 iconCls: 'icon-databases',
340 application.do_layout();
342 Ext.iterate(application.models, function (app_label, models) {
343 Ext.iterate(models, function (name, model) {
344 this.handle_new_model(model);
348 application.on('model_registered', function (model) {
349 this.handle_new_model(model);
352 this.instance_windows = {}
355 handle_new_model: function (model) {
357 model.api.has_add_permission(function (has_add_permission) {
358 if (has_add_permission) {
359 outer.add_to_new_menu(model);
362 model.api.has_read_permission(function (has_read_permission) {
363 if (has_read_permission) {
364 outer.add_to_manage_menu(model);
369 add_to_new_menu: function (model) {
372 text: model.verbose_name.capfirst(),
373 iconCls: model.iconCls,
375 handler: function (button, event) {
376 outer.create_instance_window(this.model, undefined, function (win) {
383 add_to_manage_menu: function (model) {
385 this.manage_menu.add({
386 text: model.verbose_name_plural.capfirst(),
387 iconCls: model.iconCls,
389 handler: function (button, event) {
390 var win = outer.create_model_management_window(this.model);
396 create_model_management_window: function (model, config, cls) {
398 var panel = new Gilbert.lib.plugins.models.ui.ModelPanel(model, this);
399 var win = this.application.create_window(Ext.applyIf(config||{},{
401 title: model.verbose_name_plural.capfirst(),
402 iconCls: model.iconCls,
411 create_instance_window: function (model, pk, callback, config, cls) {
412 var win = this.instance_windows[[model.app_label, model.name, pk]];
413 if (win != undefined){
418 var callback = callback;
419 var application = this.application;
422 var form_callback = function (form) {
424 var win = outer.instance_windows[[model.app_label, model.name, pk]] = application.create_window({
427 iconCls: form.iconCls,
428 bodyStyle: 'padding: 5px; background: solid;',
437 text: 'Save and Close',
438 iconCls: 'icon-database-import',
439 handler: function (button) {
440 var loading_mask = new Ext.LoadMask(win.body, {
445 win.items.items[0].getForm().submit({
446 success: function (form, action) {
448 win.fireEvent('saved');
451 failure: function (form, action) {
460 iconCls: 'icon-database-import',
461 handler: function (button) {
462 var loading_mask = new Ext.LoadMask(win.body, {
467 win.items.items[0].getForm().submit({
468 success: function (form, action) {
469 win.fireEvent('saved');
470 var pk = action.result.pk;
471 model.create_edit_form(function (newform) {
475 win.setTitle(newform.title);
476 win.setIconClass(newform.iconCls);
480 failure: function (form, action) {
488 win.on('close', function(){
489 delete outer.instance_windows[[model.app_label, model.name, pk]];
498 model.create_edit_form(form_callback, pk, {
499 bodyStyle: 'padding: 10px;',
502 model.create_new_form(form_callback, {
503 bodyStyle: 'padding: 10px;',
511 Gilbert.on('ready', function (application) {
512 application.register_plugin('models', new Gilbert.lib.plugins.models.Plugin());