Skip to content

Instantly share code, notes, and snippets.

@MihailoJoksimovic
Created May 7, 2013 15:42
Show Gist options
  • Save MihailoJoksimovic/5533630 to your computer and use it in GitHub Desktop.
Save MihailoJoksimovic/5533630 to your computer and use it in GitHub Desktop.
Ext.define('DIG.libs.DocumentEngine', {
/*
* The parent component that has all the grids inside it.
*/
parentComponent: null,
/*
* The header panel where all the document headers will be grouped.
*/
headerPanel: null,
/*
* The footer panel where all the document headers that belong
* to the footer will be displayed.
*/
footerPanel: null,
/*
* The instance of the positions grid that will show the document
* positions.
*/
positionsGrid: null,
extApplication: null,
documentData: null,
/*
* Initialize the required stuff.
*/
constructor: function(config) {
var me = this;
if (!Ext.isObject(config))
Ext.Error.raise('typeof config is not object');
me.parentComponent = config.parentComponent;
me.headerPanel = config.headerPanel;
me.footerPanel = config.footerPanel;
me.positionsGrid = config.positionsGrid;
me.extApplication = config.extApplication;
},
displayDocument: function(documentData, potentialAnswers, isNew) {
this.documentData = documentData;
for (var i = 0; i < documentData.headers.length; i++) {
var header = documentData.headers[i];
if (typeof Translation[header.name] !== 'undefined') header.name = Translation[header.name];
var panelHead = Ext.create('Ext.panel.Panel', {
xtype: 'panel',
height: 169,
margin: 10,
padding: '',
width: 234,
title: header.name,
overflowY: 'auto'
});
this.headerPanel.add(panelHead);
for (var j = 0; j < header.fields.length; j++) {
var field = header.fields[j];
if (typeof Translation[field.id] !== 'undefined') field.name = Translation[field.id];
var extField = this.getField(field);
panelHead.add(extField);
}
}
for (var k = 0; k < documentData.footers.length; k++) {
var footer = documentData.footers[k];
if (typeof Translation[footer.name] !== 'undefined') footer.name = Translation[footer.name];
var panelFoot = Ext.create('Ext.panel.Panel', {
xtype: 'panel',
height: 169,
margin: 10,
padding: '',
width: 234,
title: footer.name,
overflowY: 'auto'
});
this.footerPanel.add(panelFoot);
for (var l = 0; l < footer.fields.length; l++) {
var fieldFoot = footer.fields[l];
if (typeof Translation[fieldFoot.id] !== 'undefined') fieldFoot.name = Translation[fieldFoot.id];
var extFieldFoot = this.getField(fieldFoot);
panelFoot.add(extFieldFoot);
}
}
var grid = this.generateGrid(documentData, isNew);
this.parentComponent.insert(1, grid);
var buttons = this.generateDockedToolbar(documentData, isNew);
if (buttons.length > 0) {
var toolbar = Ext.create('Ext.toolbar.Toolbar', {
dock: 'top',
items: buttons
});
this.parentComponent.addDocked(toolbar);
}
},
generateDockedToolbar: function(documentData, isNew) {
var me = this;
var buttons = new Array();
if (isNew) {
var saveButton = Ext.create('Ext.button.Button', {
text: Translation.Save,
itemId: 'saveButton'
});
buttons.push(saveButton);
var sendButton = Ext.create('Ext.button.Button', {
text: Translation.Send,
itemId: 'sendButton'
});
buttons.push(sendButton);
} else {
var pdfButton = Ext.create('Ext.button.Button', {
text: Translation.DownloadPdf,
itemId: 'pdfButton'
});
buttons.push(pdfButton);
// If the doctype is not 1
/*if (documentData.doctype != 1) {
var buttonParent = Ext.create('Ext.button.Button', {
text: 'Parent document',
itemId: 'parentButton'
});
buttons.push(buttonParent);
}*/
if (documentData.archived == 0 && documentData.potentialAnswers.length > 0) {
var menuItems = new Array();
Ext.each(documentData.potentialAnswers, function(elem) {
var menuItem = Ext.create('Ext.menu.Item', {
itemId: elem[0],
text: elem[1]
});
menuItems.push(menuItem);
});
var buttonMenu = Ext.create('Ext.menu.Menu', {
width: 180,
itemId: 'generateDocumentMenu',
items: menuItems
});
var menuButton = Ext.create('Ext.button.Button', {
text: Translation.DocumentEngineGenerateDockedToolbarGenerateAnswer,
itemId: 'parentButton',
menu: buttonMenu
});
buttons.push(menuButton);
}
if (documentData.archived == 0) {
var archiveButton = Ext.create('Ext.button.Button', {
text: Translation.DocumentEngineGenerateDockedToolbarArchieving,
itemId: 'archiveButton'
});
buttons.push(archiveButton);
}
}
if (documentData.documentStatus == '4') {
var changesButton = Ext.create('Ext.button.Button', {
text: 'Show changes',
itemId: 'changesButton',
handler : function() {
me.highlightChanges();
}
});
buttons.push(changesButton);
}
var documentAttachmentsButton = Ext.create('Ext.button.Button', {
text: Translation.DocumentEngineGenerateDockedToolbarAttachments + ((documentData.total_files > 0) ? ' (' + documentData.total_files + ')' : ''),
itemId: 'documentAttachments',
disabled: (documentData.total_files == 0)
});
buttons.push(documentAttachmentsButton);
return buttons;
},
generateGrid: function(documentData, isNew) {
var gridColumns = new Array();
var plugins = new Array();
if (isNew) {
var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1
});
plugins.push(cellEditing);
}
var arraystoreColumns = new Array();
for (var i = 0; i < documentData.positionHeaders.length; i++) {
var docdef = Ext.getStore('Docdefs').getById(documentData.positionHeaders[i].docdef_id)
var column = this.generateGridColumn(documentData.positionHeaders[i], docdef, isNew, documentData.allManufacturersArray);
var arraystoreColumn = {
name: docdef.get('name'),
docdef: docdef//,
//type: this.determineType(documentData.positionHeaders[i])
};
gridColumns.push(column);
arraystoreColumns.push(arraystoreColumn);
}
if (isNew) {
var actionColumn = Ext.create('Ext.grid.column.Action', {
items: [
{
altText: Translation.DocumentEngineActionSplitAltText,
icon: 'img/plus_split_ico.png',
tooltip: Translation.DocumentEngineActionSplitTooltip,
handler: function(grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
this.fireEvent('splitPosition', grid, rec, rowIndex, colIndex);
}
}
]
});
actionColumn.on('splitPosition', function(grid, record, rowIndex, colIndex) {
this.extApplication.fireEvent('PositionSplit', grid, record, rowIndex, colIndex);
}, this);
gridColumns.push(actionColumn);
}
// create the data store
var store = new Ext.data.ArrayStore({
fields: arraystoreColumns,
groupField: Constant.Docdef.ByName.POSITION_NUMBER
});
store.loadData(documentData.positions);
store.markSplitPositions = function() {
var posnums = [];
this.each(function(record) {
var posnum = record.get(Constant.Docdef.ByName.POSITION_NUMBER);
if (posnums.indexOf(posnum) >= 0) {
record.set('disallow_split', true);
} else {
posnums.push(posnum);
}
});
}
store.markSplitPositions();
var groupingFeature = Ext.create('Ext.grid.feature.Grouping');
var grid = Ext.create('Webedi.grid.DocposGrid', {
alias: 'docposGridPanel',
height: 250,
title: Translation.DocumentEngineGridPanel,
itemId: 'documentGridPanel',
store: store,
columns: gridColumns,
plugins: plugins,
features: [groupingFeature],
margin: 10,
documentData: documentData
});
return grid;
},
generateGridColumn: function(headerData, docdef, isNew, manufacturersArray) {
var me = this;
var column;
var dd = Ext.create('Ext.form.field.Date');
var dateFormat = dd.format;
var columnTitle = headerData.name;
if (Translation[docdef.get('name')] !== 'undefined') columnTitle = Translation[docdef.get('name')];
if (isNew && (headerData.status == 'editable'
|| headerData.status == 'new')) {
switch (headerData.type) {
case 'date' :
column = Ext.create('Ext.grid.column.Column', {
text: columnTitle,
dataIndex: docdef.get('name'),
xtype: 'datecolumn',
format: dateFormat,
editor: {
xtype: 'datefield',
format: dateFormat,
allowBlank: headerData.mandatory == 'N'
}
});
break;
case 'number' :
column = Ext.create('Ext.grid.column.Column', {
text: columnTitle,
dataIndex: docdef.get('name'),
editor: {
xtype: 'numberfield',
allowBlank: headerData.mandatory == 'N'
}
});
break;
case 'multiselect' :
var manufacturersStore = Ext.create('Ext.data.Store', {
// store configs
storeId: 'myStore',
// reader configs
data: manufacturersArray,
fields: ['id','name']
});
column = Ext.create('Ext.grid.column.Column', {
text: columnTitle,
dataIndex: docdef.get('name'),
width: 250,
editor: {
xtype: 'combobox',
width: 250,
allowBlank: headerData.mandatory == 'N',
multiSelect: true,
store: manufacturersStore,
displayField: 'name',
valueField: 'name',
delimiter: '|'
}
});
break;
default:
column = Ext.create('Ext.grid.column.Column', {
text: columnTitle,
dataIndex: docdef.get('name'),
renderer_id: headerData.renderer_id,
editor: {
allowBlank: headerData.mandatory == 'N'
}
});
}
column.docdef = docdef;
column.renderer = function(value) {
headerData.renderer_id = '1';
if (headerData.renderer_id) {
console.log("Applying renderer!");
var fieldRenderer = Ext.create('DIG.libs.Renderer.FieldRenderer');
return fieldRenderer.renderField(headerData.renderer_id, value)
} else {
return value;
}
}
} else {
column = Ext.create('Ext.grid.column.Column', {
text: columnTitle,
dataIndex: docdef.get('name')
});
}
return column;
},
highlightChanges: function() {
for (var i=0; i < this.documentData.documentChanges.length; i++) {
var docdef = Ext.getStore('Docdefs').getById(this.documentData.documentChanges[i].we_docdef_id);
var field = Ext.getCmp(docdef.get('name').replace(/\s/g, ''));
if (field) {
field.inputEl.addCls('changesHighlight');
}
}
},
getField: function(field) {
var extField = null;
switch(field.type) {
case 'date' :
extField = Ext.create('Ext.form.field.Date', {
id: field.id,
padding: 5,
fieldLabel: field.name,
value: field.value,
readOnly: field.edit_status == 'readonly'
});
break;
case 'number' :
extField = Ext.create('Ext.form.field.Number', {
id: field.id,
padding: 5,
fieldLabel: field.name,
value: field.value,
readOnly: field.edit_status == 'readonly'
});
break;
case 'hidden' :
extField = Ext.create('Ext.form.field.Hidden', {
id: field.id,
name: field.id,
value: field.value
});
break;
default:
extField = Ext.create('Ext.form.field.Text', {
id: field.id,
padding: 5,
fieldLabel: field.name,
value: field.value,
readOnly: field.edit_status == 'readonly'
});
}
return extField;
},
determineType: function(viewColumn) {
var returnValue = 'string';
switch(viewColumn.type) {
case 'date':
returnValue = 'date';
break;
case 'number':
returnValue = 'int';
break;
case 'float':
returnValue = 'float';
break;
case 'bool':
returnValue = 'bool';
break;
default:
returnValue = 'string';
}
return returnValue;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment