Created
March 7, 2016 16:43
-
-
Save Sarapulov/bd42ccc0e58c3b0084b7 to your computer and use it in GitHub Desktop.
Assignment Control App | app.js code
This file contains hidden or 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
(function() { | |
return { | |
current_user: null, | |
requests: { | |
fetchCurrentUser: function(){ | |
return { | |
url: '/api/v2/users/' + this.currentUser().id() + '.json?include=groups,organizations', | |
method: 'GET', | |
proxy_v2: true | |
}; | |
} | |
}, | |
events: { | |
'app.created': function(){ | |
this.ajax('fetchCurrentUser'); | |
}, | |
'fetchCurrentUser.done': 'initialize' | |
}, | |
initialize: function(data) { | |
this.current_user = _.extend(data, data.user); | |
this.current_user_group_ids = _.map(data.groups, function(g) { return g['id']; }); | |
if (this.currentUserIsTarget()) | |
return this.hideAssigneeOptions(); | |
}, | |
currentUserIsTarget: function(){ | |
var rules = [ | |
[ 'targeted_user_ids', String(this.current_user.id) ], | |
[ 'targeted_user_tags', this.current_user.tags ], | |
[ 'targeted_organization_ids', _.map(this.current_user.organizations, function(org) { return String(org.id); })], | |
[ 'targeted_group_ids', _.map(this.current_user.groups, function(group) { return String(group.id); })] | |
]; | |
return _.any(_.map( | |
rules, | |
function(rule){ | |
return this._contains(this._settings(rule[0]), rule[1]); | |
}, | |
this | |
)); | |
}, | |
hideAssigneeOptions: function(){ | |
var group_ids = this._settings('hidden_group_ids'); | |
// var user_ids = this._settings('hidden_user_ids'); // ORIGINAL CODE | |
var user_ids = this.__settings(); | |
var removed_groups = []; | |
if (this.setting('assign_to_self')) { | |
user_ids.splice(user_ids.indexOf(this.currentUser().id().toString())); | |
_.each(this.current_user_group_ids, function(id) { | |
// id = id.toString(); | |
removed_groups.push(id); | |
group_ids.splice(group_ids.indexOf(id)); | |
}); | |
} | |
var currentuser = this.currentUser().id(); | |
_.each(this.ticketFields('assignee').options(), function(option){ | |
var group_and_user = option.value().split(':'), | |
group_and_user_label = option.label().split('::'), | |
group_id = group_and_user[0], | |
user_id = group_and_user[1] || "", | |
assignable_group = group_and_user_label[0] == group_and_user_label[1]; | |
// hide if option from assignee field as app setting suggests, but exclude any record for current user | |
if ( (_.contains(group_ids, group_id) || _.contains(user_ids, user_id*1)) && ( currentuser*1 != user_id*1) ) { | |
console.log("hiding", group_and_user_label); | |
option.hide(); | |
} | |
// disable assignable group record for a group which current user belongs to | |
if ( assignable_group && _.contains(removed_groups, group_id*1) ) { | |
console.log("disabling", group_and_user_label); | |
option.disable(); | |
} | |
}.bind(this)); | |
}, | |
_settings: function(label){ | |
return _.compact((this.setting(label) || "").split(',')); | |
}, | |
__settings: function(){ // remove when go live | |
var hidden_user_ids = [663546192,1329999031,1342526072,1337514501,1346422452,1342093231,1361217221,1361307101,1372540652,1383505532,1386188902,1384256951,1630693351,1630693441,1630693551,1630693631,1630693691,1630693751,1632920182,1630693861,1630694011,1630694081,1630694221,1632920582,1632920652,1632920692,1632920772,1630694791,1630694851,1632920922,1630695081,1632921132,1630695221,1630695281,1630695331,1632921292,1630695411,1630695491,1632921552,1632921722,1630695821,1632921842,1632921902,1632921982,1632922032,1632922052,1632922102,1632922142,1630696341,1632922312,1630696501,1630696601,1630696671,1632922662,1630696901,1630697181,1630697281,1632923042,1630697391,1632923192,1630697511,1630697571,1632923372,1632923422,1630697771,1630697861,1632923622,1630697991,1630698081,1632923782,1632923842,1630698351,1632924042,1632924102,1630698591,1632924222,1632924292,1632924422,1632924462,1632924552,1630699041,1630699131,1630699191,1632924862,1630699471,1632925002,1632925052,1630699671,1632925172,1630699801,1632925292,1632925372,1630700041,1630700111,1636975032,1636982402,1634791531,1636984962,1634794981,1634796601,1634799191,1636992862,1634801671,1634802771,1636996232,1636997572,1635819811,1635820961,1637971912,1637972672,1548114679,1561719225,1548140669,1561756725,2026551785,2098090539, 1329994391,1342363102,1634505151,1552218529,2347039395]; | |
// return _.compact((this.setting(label) || "").split(',')); | |
return hidden_user_ids; | |
}, | |
_contains: function(list, values){ | |
if (typeof values !== "object") | |
return _.contains(list, values); | |
var flattened_contains = _.inject(values, function(memo, value){ | |
memo.push(_.contains(list, value)); | |
return memo; | |
}, []); | |
return _.any(flattened_contains); | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment