Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved handling of competency domains #1945

Merged
merged 6 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 78 additions & 60 deletions app/components/objective-manage-competency.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,97 @@
import Ember from 'ember';
import DS from 'ember-data';

const { Component, computed } = Ember;
const { notEmpty, oneWay } = computed;
const { Component, RSVP, computed } = Ember;
const { Promise, all, filter} = RSVP;

export default Component.extend({
objective: null,
programYear: oneWay('objective.programYear'),
showCompetencyList: notEmpty('programYear.competencies'),
classNames: ['objective-manager', 'objective-manage-competency'],
competencies: computed('programYear.competencies.[]', 'objective.competency', function(){
if(!this.get('programYear')){
return [];
}

return DS.PromiseArray.create({
promise: this.get('programYear.competencies')
schoolCompetencies: computed('programYear.program.school.competencies.[]', function(){
return new Promise(resolve => {
this.get('programYear').then(programYear => {
programYear.get('program').then(program => {
program.get('school').then(school => {
school.get('competencies').then(competencies => {
resolve(competencies);
});
});
});
});
});
}),
domains: computed('[email protected]', 'objective.competency', function(){
var defer = Ember.RSVP.defer();
var domainContainer = {};
var domainIds = [];
var promises = [];
let domainProxy = Ember.ObjectProxy.extend({
selectedCompetency: null,
subCompetencies: [],
selected: computed('subCompetencies.[]', 'selectedCompetency', function(){
let selectedSubCompetencies = this.get('subCompetencies').filter(competencyProxy => {
return competencyProxy.get('id') === this.get('selectedCompetency.id');
});
return selectedSubCompetencies.length > 0;
}),

programYear: computed('objective.programYears.[]', function(){
return new Promise(resolve => {
const objective = this.get('objective');
objective.get('programYears').then(programYears => {
if (programYears.length) {
let programYear = programYears.get('firstObject');
resolve(programYear);
} else {
resolve(null);
}
});
});
let competencyProxy = Ember.ObjectProxy.extend({
selectedCompetency: null,
selected: computed('content', 'selectedCompetency', function(){
return this.get('content.id') === this.get('selectedCompetency.id');
}),
}),

competencies: computed('programYear.competencies.[]', function(){
return new Promise(resolve => {
this.get('programYear').then(programYear => {
programYear.get('competencies').then(competencies => {
resolve(competencies);
});
});
});
this.get('competencies').forEach((competency) =>{
promises.pushObject(competency.get('domain').then(
domain => {
if(!domainContainer.hasOwnProperty(domain.get('id'))){
domainIds.pushObject(domain.get('id'));
domainContainer[domain.get('id')] = domainProxy.create({
content: domain,
selectedCompetency: this.get('objective.competency'),
subCompetencies: [],
}),

competenciesWithSelectedChildren: computed('schoolCompetencies.[]', 'objective.competency', function(){
return new Promise(resolve => {
const objective = this.get('objective');
objective.get('competency').then(selectedCompetency => {
if (selectedCompetency) {
this.get('schoolCompetencies').then(competencies => {
filter(competencies.toArray(), (competency => {
return new Promise(resolve => {
competency.get('treeChildren').then(children => {
let selectedChildren = children.filter(competency => selectedCompetency.get('id') === competency.get('id'));
resolve(selectedChildren.length > 0);
});
});
})).then(competenciesWithSelectedChildren => {
resolve(competenciesWithSelectedChildren);
});
}
if(competency.get('id') !== domain.get('id')){
var subCompetencies = domainContainer[domain.get('id')].get('subCompetencies');
if(!subCompetencies.contains(competency)){
subCompetencies.pushObject(competencyProxy.create({
content: competency,
selectedCompetency: this.get('objective.competency')
}));
subCompetencies.sortBy('title');
}
}
});
} else {
resolve([]);
}
));
});
});
Ember.RSVP.all(promises).then(function(){
var domains = domainIds.map(function(id){
return domainContainer[id];
}).filter(
domain => domain.get('subCompetencies').length > 0
).sortBy('title');
defer.resolve(domains);
}),

domains: computed('competencies.[]', function(){
return new Promise(resolve => {
this.get('competencies').then(competencies => {
all(competencies.mapBy('domain')).then(domains => {
resolve(domains.uniq());
});
});
});
}),

return DS.PromiseArray.create({
promise: defer.promise
domainsWithNoChildren: computed('domains.[]', function(){
return new Promise(resolve => {
this.get('domains').then(domains => {
filter(domains.toArray(), (competency => {
return new Promise(resolve => {
competency.get('children').then(children => {
resolve(children.length === 0);
});
});
})).then(domainsWithNoChildren => {
resolve(domainsWithNoChildren);
});
});
});
}),
actions: {
Expand Down
122 changes: 86 additions & 36 deletions app/components/programyear-competencies.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,115 @@
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';

const { Component, computed } = Ember;
const { Component, RSVP, computed } = Ember;
const { Promise, all, filter } = RSVP;

export default Component.extend({
init(){
this._super(...arguments);
this.get('loadSelectedCompetencies').perform();
},
didUpdateAttrs(){
this._super(...arguments);
this.get('loadSelectedCompetencies').perform();
},
programYear: null,
isManaging: null,
classNames: ['programyear-competencies'],
isManaging: false,
isSaving: false,
bufferCompetencies: [],
selectedCompetencies: [],

showCollapsible: computed('isManaging', 'programYear.competencies.[]', function () {
const isManaging = this.get('isManaging');
const competencies = this.get('programYear.competencies');
return !isManaging && competencies.get('length');
}),
loadSelectedCompetencies: task(function * (){
const programYear = this.get('programYear');
if (programYear){
let selectedCompetencies = yield programYear.get('competencies');
this.set('selectedCompetencies', selectedCompetencies.toArray());
} else {
yield timeout(1000);
}
}).restartable(),

actions: {
manage: function(){
var self = this;
this.get('programYear.competencies').then(function(competencies){
self.set('bufferCompetencies', competencies.toArray());
self.set('isManaging', true);
save: task(function * () {
yield timeout(10);
let programYear = this.get('programYear');
let selectedCompetencies = this.get('selectedCompetencies');
programYear.set('competencies', selectedCompetencies);
try {
yield programYear.save();
} finally {
this.get('flashMessages').success('general.savedSuccessfully');
this.get('setIsManaging')(false);
this.get('expand')();
}

}).drop(),

competencies: computed('programYear.program.school.competencies.[]', function(){
return new Promise(resolve => {
const programYear = this.get('programYear')
programYear.get('program').then(program => {
program.get('school').then(school => {
school.get('competencies').then(competencies => {
resolve(competencies);
});
});
});
},
save: function(){
this.set('isSaving', true);
let programYear = this.get('programYear');
programYear.get('competencies').then(competencies => {
competencies.clear();
this.get('bufferCompetencies').forEach(competency => {
competency.get('programYears').addObject(programYear);
competencies.addObject(competency);
});
}),

domains: computed('competencies.[]', function(){
return new Promise(resolve => {
this.get('competencies').then(competencies => {
all(competencies.mapBy('domain')).then(domains => {
resolve(domains.uniq());
});
programYear.save().then(()=> {
this.set('isSaving', false);
this.set('isManaging', false);
});
});
}),

competenciesWithSelectedChildren: computed('competencies.[]', 'selectedCompetencies.[]', function(){
const selectedCompetencies = this.get('selectedCompetencies');
return new Promise(resolve => {
this.get('competencies').then(competencies => {
filter(competencies.toArray(), (competency => {
return new Promise(resolve => {
competency.get('treeChildren').then(children => {
let selectedChildren = children.filter(competency => selectedCompetencies.contains(competency));
resolve(selectedChildren.length > 0);
});
});
})).then(competenciesWithSelectedChildren => {
resolve(competenciesWithSelectedChildren);
});
});
},
});
}),

actions: {
cancel: function(){
this.set('bufferCompetencies', []);
this.set('isManaging', false);
this.get('loadSelectedCompetencies').perform();
this.get('setIsManaging')(false);
},
addCompetencyToBuffer: function(competency){
this.get('bufferCompetencies').addObject(competency);
let selectedCompetencies = this.get('selectedCompetencies').toArray();
selectedCompetencies.addObject(competency);
competency.get('children').then(children => {
this.get('bufferCompetencies').addObjects(children);
selectedCompetencies.addObjects(children.toArray());
});
this.set('selectedCompetencies', selectedCompetencies);
},
removeCompetencyFromBuffer: function(competency){
this.get('bufferCompetencies').removeObject(competency);
let selectedCompetencies = this.get('selectedCompetencies').toArray();
selectedCompetencies.removeObject(competency);
competency.get('children').then(children => {
children.forEach(child => {
this.get('bufferCompetencies').removeObject(child);
});
selectedCompetencies.removeObjects(children.toArray());
});
this.set('selectedCompetencies', selectedCompetencies);
},
collapse(){
this.get('programYear.competencies').then(competencies => {
if (competencies.get('length')) {
this.attrs.collapse();
this.get('collapse')();
}
});
},
Expand Down
Loading