"use strict"; (function () { 'use strict'; angular.module('changeModule') .directive('documentsTab', ['events', 'tabIds', 'ticketModel', function (events, tabIds, ticketModel) { return { restrict: 'E', replace: true, templateUrl: 'views/change/documents-tab.html', scope: { isNoteRequired: '=', context: '=', docTypes: '=', editMode: '=', metadata: '=?' }, link: function (scope) { //var docTypes = scope.docTypes; scope.$watch(function () { return scope.documents; }, function (newVal) { if (newVal) { scope.$parent.$parent.documents = scope.documents; } }); scope.attachmentLimit = 3; scope.state = { documentsSelected: false, allDocumentsSelected: false }; scope.defaultDocumentTypes = _.cloneDeep(scope.docTypes) || []; scope.docTypesPicked = 0; if (scope.editMode) { scope.selectedDocumentTypes = scope.context.documents; scope.docTypesPicked = scope.context.documents.length || 0; scope.documentsChunked = chunkArray(scope.selectedDocumentTypes, 2); if (scope.selectedDocumentTypes && scope.selectedDocumentTypes.length > 0) { scope.state.documentsSelected = true; } } else { scope.context.documents = scope.selectedDocumentTypes; } scope.documentsTab = { currentNote: { id: 'note', attachments: [] }, allUserNotes: [] }; scope.addNote = { inputText: '' }; scope.handleDocumentsControlButtonClick = function (documentSelected) { if (!_.isUndefined(documentSelected) && documentSelected === 'docSelect' && scope.editMode) { scope.markSelectedDocTypeInvisible(); } scope.state.documentsSelected = !scope.state.documentsSelected; scope.context.documents = scope.selectedDocumentTypes; if (scope.editMode) { scope.$emit(events.CHANGE_REQUEST_PLANS_EDIT_STATUS, { hideButtons: !scope.state.documentsSelected }); } }; scope.markSelectedDocTypeInvisible = function () { _.each(scope.selectedDocumentTypes, function (selectedDoc) { _.each(scope.defaultDocumentTypes, function (defaultDocumentType) { if (defaultDocumentType.name === selectedDoc.name) { defaultDocumentType.invisible = true; } }); }); }; scope.toggleDocumentBlock = function (docType, isRemove) { docType.active = !docType.active; docType.active ? scope.docTypesPicked++ : scope.docTypesPicked-- && (scope.state.allDocumentsSelected = false); if (!docType.active) { scope.selectedDocumentTypes = _.without(scope.selectedDocumentTypes, docType); } if (scope.selectedDocumentTypes === 0) { scope.showSave = true; } if (scope.docTypesPicked === 0) { _.forEach(scope.defaultDocumentTypes, function (defaultDocType) { defaultDocType.active = false; defaultDocType.plans = []; }); scope.selectedDocumentTypes = []; scope.documentsChunked = []; scope.state.documentsSelected = false; scope.$emit(events.CHANGE_REQUEST_PLANS_EDIT_STATUS, { hideButtons: true, showSave: true }); } else { if (!isRemove) { scope.selectedDocumentTypes = _.filter(scope.defaultDocumentTypes, { active: true }); scope.documentsChunked = chunkArray(scope.selectedDocumentTypes, 2); } } }; scope.handleKeydown = function ($event, item) { if ($event.keyCode === 32) { // space key scope.toggleDocumentBlock(item); $event.stopPropagation(); } }; scope.selectAllDocuments = function () { scope.state.allDocumentsSelected = !scope.state.allDocumentsSelected; scope.docTypesPicked = scope.state.allDocumentsSelected ? scope.defaultDocumentTypes.length : 0; _.each(scope.defaultDocumentTypes, function (docType) { docType.active = scope.state.allDocumentsSelected; }); scope.selectedDocumentTypes = _.filter(scope.defaultDocumentTypes, { active: true }); scope.documentsChunked = chunkArray(scope.selectedDocumentTypes, 2); return; }; scope.addSameTypePlan = function (docType) { docType.activePlanIndex = parseInt(docType.plans.push({ attachments: [], desc: '' }) - 1); }; scope.removeActivePlanTab = function (docType) { var activePlanIndex = docType.activePlanIndex; if (!(docType.plans[docType.activePlanIndex].workNote && docType.plans[docType.activePlanIndex].workNote.locked)) { docType.plans.splice(activePlanIndex, 1); docType.activePlanIndex = 0; if (docType.plans.length === 0) { scope.toggleDocumentBlock(docType, true); scope.documentsChunked = chunkArray(scope.selectedDocumentTypes, 2); } docType.invisible = false; } var replacementIndex = _.findIndex(scope.defaultDocumentTypes, function (doc) { return doc.name === docType.name; }); if (!_.isUndefined(replacementIndex) && replacementIndex !== -1) { scope.defaultDocumentTypes[replacementIndex] = docType; } }; scope.changeDocumentTab = function (docType, plan) { var idx = docType.plans.indexOf(plan); docType.activePlanIndex = idx; docType.isLocked = plan.workNote && plan.workNote.locked; }; function chunkArray(arr, chunkSize) { if (Object.prototype.toString.call(arr).slice(8, -1) !== 'Array') { return arr; } return [].concat.apply([], arr.map(function (elem, i) { if (!elem.plans || elem.plans.length === 0) { elem.plans = [{ attachments: [], desc: '' }]; } elem.activePlanIndex = 0; return i % chunkSize ? [] : [arr.slice(i, i + chunkSize)]; })); } scope.$on(events.DISCARD_DOCUMENTS, function () { scope.context.documents = []; scope.state.documentsSelected = false; scope.state.allDocumentsSelected = false; _.each(scope.defaultDocumentTypes, function (docType) { docType.active = scope.state.allDocumentsSelected; docType.plans = []; docType.plans.push({ attachments: [], desc: '' }); docType.activePlanIndex = 0; }); }); scope.$watch(tabIds.wizard.documents + '.$invalid', function (invalid) { if (typeof invalid !== 'undefined') { scope.$emit(events.CHANGE_WIZARD_FORM_STATE, { name: tabIds.wizard.documents, invalid: invalid }); scope.$emit(events.RELEASE_WIZARD_FORM_STATE, { name: tabIds.wizard.documents, invalid: invalid }); } }); } }; } ]); })();