Skip to content

Commit

Permalink
add unknown/invisible status if selected
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman authored and rijkvanzanten committed Oct 31, 2017
1 parent a59a28a commit b34b45e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
66 changes: 49 additions & 17 deletions app/core/widgets/StatusWidget.js
@@ -1,9 +1,11 @@
define([
'app',
'underscore',
'backbone'
'backbone',
'utils',
'core/t'
],
function(app, _, Backbone) {
function(app, _, Backbone, Utils, __t) {

'use strict';

Expand Down Expand Up @@ -44,35 +46,65 @@ function(app, _, Backbone) {
return table.getStatusColumnName();
},

serialize: function () {
parseStatusItem: function (status, currentStatus) {
var item = status.toJSON();

// NOTE: do not strictly compare as status can (will) be string
item.selected = status.get('id') == currentStatus;
item.model = status;
item.color = item.background_color || item.color;

return item;
},

getStatusList: function () {
var statuses = [];
var model = this.model;
var foundMatch = false;
var structure = model.structure;
var attr = this.getStatusColumnName();
var currentStatus = this.model.get(attr);
var statusColumnName = this.getStatusColumnName();
var currentStatus = this.model.get(statusColumnName);

if (!currentStatus && structure.get(attr)) {
currentStatus = structure.get(attr).get('default_value');
if (Utils.isNothing(currentStatus) && structure.get(statusColumnName)) {
currentStatus = structure.get(statusColumnName).get('default_value');
}

_.each(model.getStatusVisible(), function (status) {
var item = status.toJSON();
// Go through all the statuses and add to the list all visible or a selected one
model.getTableStatusesMapping().each(function (status) {
var item = this.parseStatusItem(status, currentStatus);

// NOTE: do not strictly compare as status can (will) be string
item.selected = status.get('id') == currentStatus;
item.model = status;
item.color = item.background_color || item.color;
statuses.push(item);
});
if (item.selected) {
foundMatch = true;
}

if (item.selected || model.isStatusVisible(status)) {
statuses.push(item);
}
}, this);

// if there's not a match, we add the status as selected and name it "unknown"
if (!foundMatch) {
statuses.push({
id: currentStatus,
name: __t('unknown'),
selected: true
})
}

statuses = _.sortBy(statuses, function(item) {
return item.sort;
});

return statuses;
},

serialize: function () {
return {
model: this.model,
readonly: typeof this.model.canEdit === 'function' ? this.model.canEdit(attr) : true,
statuses: statuses
readonly: typeof this.model.canEdit === 'function'
? this.model.canEdit(this.getStatusColumnName())
: true,
statuses: this.getStatusList()
};
},

Expand Down
16 changes: 10 additions & 6 deletions app/helpers/status.js
Expand Up @@ -19,19 +19,23 @@ define(['app', 'underscore'], function (app, _) {
return mapping.get(statusValue);
},

isStatusVisible: function (table, status) {
var tableStatuses = this.getTableStatuses(table);
var deleteValue = status ? tableStatuses.get('delete_value') : undefined;
var isDelete = deleteValue == status.get('id');

return status.get('hidden_globally') !== true && !isDelete;
},

getStatusVisible: function (tableName) {
var mapping = this.getTableStatusesMapping(tableName);
var status = this.getTableStatuses(tableName);
var deleteValue = status ? status.get('delete_value') : undefined;
var statuses = [];

mapping.each(function (status) {
var isDelete = deleteValue == status.get('id');

if (status.get('hidden_globally') !== true && !isDelete) {
if (this.isStatusVisible(tableName, status)) {
statuses.push(status);
}
});
}, this);

return statuses;
},
Expand Down
8 changes: 8 additions & 0 deletions app/mixins/status.js
Expand Up @@ -58,10 +58,18 @@ define(['underscore', 'helpers/status'], function (_, StatusHelper) {
return this.getStatus().get('name');
},

getTableStatusesMapping: function () {
return StatusHelper.getTableStatusesMapping(this._getTableName());
},

getStatusVisible: function () {
return StatusHelper.getStatusVisible(this._getTableName());
},

isStatusVisible: function (status) {
return StatusHelper.isStatusVisible(this._getTableName(), status);
},

getStatusVisibleValues: function () {
return StatusHelper.getStatusVisibleValues(this._getTableName());
},
Expand Down

0 comments on commit b34b45e

Please sign in to comment.