Created
October 20, 2018 02:21
Team Change Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
extendsFrom: 'RecordView', | |
initialize: function (options) { | |
this._super('initialize', [options]); | |
}, | |
_render: function () { | |
this._super('_render'); | |
this.model.on('change:team_name', this.onChangeTeamName, this); | |
}, | |
onChangeTeamName: function () { | |
var teamField = this.getField('team_name'); | |
var teams = teamField.value; | |
var teamIDs = []; | |
var teamLock = sessionStorage.getItem('s2s_teamLock'); | |
if (teamLock == 1) { | |
return null; | |
} | |
//Only run this on an edit view | |
if (this.action != 'edit') { | |
sessionStorage.removeItem('s2s_teamLock'); | |
return null; | |
} | |
for (i = 0; i < teams.length; i++) { | |
if (!_.isEmpty(teams[i]['id']) && !_.isUndefined(teams[i]['id'])) { | |
teamIDs[i] = teams[i]['id']; | |
} else { | |
//If we get here then the user is adding a team | |
sessionStorage.setItem('s2s_teamArray', JSON.stringify(teamIDs)); | |
return null; | |
} | |
} | |
//if there are no teams then just return | |
if (teamIDs.length == 0) { | |
return null; | |
} | |
var prevTeams = sessionStorage.getItem('s2s_teamArray'); | |
if (_.isEmpty(prevTeams) || _.isUndefined(prevTeams)) { | |
sessionStorage.setItem('s2s_teamArray', JSON.stringify(teamIDs)); | |
prevTeams = teamIDs; | |
} else { | |
prevTeams = JSON.parse(prevTeams); | |
} | |
var arrayDiff = this.arrayDiff(teamIDs, prevTeams); | |
if (arrayDiff.length > 0) { | |
var teamIndex = teamIDs.indexOf(arrayDiff[0]); | |
if (!_.isUndefined(teams[teamIndex])) { | |
var teamDisplayName = teams[teamIndex]['name']; | |
var message = app.lang.get('MSG_PRIMARY_TEAM001', this.module, { | |
teamName: teamDisplayName | |
}); | |
app.alert.show('message-id', { | |
level: 'confirmation', | |
messages: message, | |
autoClose: false, | |
confirm: { | |
label: 'Yes' | |
}, | |
cancel: { | |
label: 'No' | |
}, | |
onConfirm: function () { | |
sessionStorage.setItem('s2s_teamLock', '1'); | |
teamField.setPrimary(teamIndex); | |
sessionStorage.removeItem('s2s_teamLock'); | |
sessionStorage.removeItem('s2s_teamArray'); | |
} | |
, | |
onCancel: function () { | |
sessionStorage.removeItem('s2s_teamLock'); | |
sessionStorage.removeItem('s2s_teamArray'); | |
} | |
}); | |
} | |
} | |
return true; | |
}, | |
arrayDiff: function (a1, a2) { | |
var a = [], diff = []; | |
for (var i = 0; i < a1.length; i++) { | |
a[a1[i]] = true; | |
} | |
for (var i = 0; i < a2.length; i++) { | |
if (a[a2[i]]) { | |
delete a[a2[i]]; | |
} else { | |
a[a2[i]] = true; | |
} | |
} | |
for (var k in a) { | |
diff.push(k); | |
} | |
return diff; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment