Skip to content

Commit

Permalink
Rewires user profile view model to support changes in the mobile surv…
Browse files Browse the repository at this point in the history
…ey manager/designer. re #4357
  • Loading branch information
chiatt committed Dec 18, 2018
1 parent fcb737f commit e0d2d5b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
8 changes: 4 additions & 4 deletions arches/app/media/js/viewmodels/mobile-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ define([
}
};

this.getMobileSurveyResources = function(mobilesurvey){
this.getMobileSurveyResources = function(){
var successCallback = function(data){
mobilesurvey.collectedResources(true);
self.mobilesurvey.collectedResources(true);
_.each(data.resources, self.processResources);
_.each(self.resourceList.items(), self.flattenCards);
};
if (!mobilesurvey.collectedResources()) {
if (!this.mobilesurvey.collectedResources()) {
$.ajax({
url: arches.urls.mobile_survey_resources(mobilesurvey.id)
url: arches.urls.mobile_survey_resources(this.mobilesurvey.id)
})
.done(successCallback)
.fail(function(data){console.log('request failed', data);});
Expand Down
37 changes: 32 additions & 5 deletions arches/app/media/js/views/user-profile-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ define([
'knockout-mapping',
'arches',
'viewmodels/mobile-survey',
'models/mobile-survey',
'views/base-manager',
'views/mobile-survey-manager/identity-list',
'profile-manager-data'
], function($, _, ko, koMapping, arches, MobileSurveyViewModel, BaseManagerView, data) {
], function($, _, ko, koMapping, arches, MobileSurveyViewModel, MobileSurveyModel, BaseManagerView, IdentityList, data) {

var UserProfileManager = BaseManagerView.extend({
initialize: function(options) {
Expand All @@ -32,15 +34,40 @@ define([
self.viewModel.toggleEditUserForm = function() {
this.showEditUserForm(!this.showEditUserForm());
};
self.viewModel.mobileSurveyManager = new MobileSurveyViewModel(data);

_.each(self.viewModel.mobileSurveyManager.mobilesurveys(), function(mobilesurvey) {

this.identityList = new IdentityList({
items: ko.observableArray(data.identities)
});

this.viewModel.mobilesurveys =
data.mobilesurveys.map(function(mobilesurvey) {
return new MobileSurveyViewModel({
mobilesurvey: mobilesurvey,
identities: data.identities
});
});

this.viewModel.mobileSurveyFilter = ko.observable('');

this.viewModel.filteredMobileSurveys = ko.computed(function() {
var filter = self.viewModel.mobileSurveyFilter();
var list = self.viewModel.mobilesurveys;
if (filter.length === 0) {
return list;
}
return _.filter(list, function(mobilesurvey) {
return mobilesurvey.mobilesurvey.name().toLowerCase().indexOf(filter.toLowerCase()) >= 0;
});
});

_.each(self.viewModel.mobilesurveys, function(mobilesurvey) {
mobilesurvey.resources = ko.computed(function() {
var resources = [];
var resourceLookup = {};
_.each(self.viewModel.mobileSurveyManager.resourceList.items(), function(resource) {
_.each(mobilesurvey.resourceList.items(), function(resource) {
_.each(resource.cards(), function(card) {
if (_.contains(mobilesurvey.cards(), card.cardid)) {
if (_.contains(mobilesurvey.mobilesurvey.cards(), card.cardid)) {
if (resourceLookup[resource.id]) {
resourceLookup[resource.id].cards.push(card);
} else {
Expand Down
32 changes: 16 additions & 16 deletions arches/app/templates/views/user-profile-manager.htm
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,22 @@ <h4 class="text-main" style="font-weight: 500;">{{ user.userprofile.phone }}</h4
{% trans 'My Surveys' %}
</div>

<!--ko ifnot: mobileSurveyManager.mobilesurveys().length -->
<!--ko ifnot: mobilesurveys.length -->
<div id="default-mobile-projects" class="profile-default-message-panel">
{% trans "Sorry, but you haven't been invited to a survey yet..." %}
</div>
<!--/ko-->

<!--ko if: mobileSurveyManager.mobilesurveys().length -->
<!--ko if: mobilesurveys.length -->
<!-- Survey Invitations List -->
<div id="mobile-projects-list" class="profile-default-message-panel">
<div class="row">

<!-- Find a survey -->
<div class="col-sm-9 col-sm-offset-3 relative">
<div class="project-search-widget">
<input type="text" class="form-control" placeholder="{% trans 'Find a survey...' %}" data-bind="value: mobileSurveyManager.mobileSurveyFilter, valueUpdate: 'keyup'">
<span class="clear-node-search" data-bind="visible: mobileSurveyManager.mobileSurveyFilter().length > 0, click: function() { mobileSurveyManager.mobileSurveyFilter(''); }"><i class="fa fa-times-circle"></i></span>
<input type="text" class="form-control" placeholder="{% trans 'Find a survey...' %}" data-bind="value: mobileSurveyFilter, valueUpdate: 'keyup'">
<span class="clear-node-search" data-bind="visible: mobileSurveyFilter().length > 0, click: function() { mobileSurveyFilter(''); }"><i class="fa fa-times-circle"></i></span>
</div>
</div>
</div>
Expand All @@ -314,30 +314,30 @@ <h4 class="text-main" style="font-weight: 500;">{{ user.userprofile.phone }}</h4
<div id="" class="row">
<div class="col-sm-9 col-sm-offset-3 mar-top">
<div class="">
<!--ko foreach: {data: mobileSurveyManager.filteredMobileSurveys(), as: 'mobilesurvey'} -->
<!--ko foreach: {data: filteredMobileSurveys(), as: 'vm'} -->
<div class="project-panel">

<div class="project-panel-header">
<div class="user-survey">
<div class="project-status" data-bind="text:mobilesurvey.active() ? '{% trans "Active" %}' : '{% trans "Inactive" %}', css: {active: mobilesurvey.active()}"></div>
<div class="project-status" data-bind="text:vm.mobilesurvey.active() ? '{% trans "Active" %}' : '{% trans "Inactive" %}', css: {active: vm.mobilesurvey.active()}"></div>
<ul class="project-metadata">
<li class="project-metadata-item">
<span data-bind="text: mobilesurvey.startdate() || '?'"></span>
<span data-bind="text: vm.mobilesurvey.startdate() || '?'"></span>
{% trans 'to' %}
<span data-bind="text: mobilesurvey.enddate() || '?'"></span>
<span data-bind="text: vm.mobilesurvey.enddate() || '?'"></span>
</li>
</ul>
</div>
<h3 class="project-panel-title" data-bind="text: mobilesurvey.name() === '' ? '{% trans "Unnamed Survey" %}' : mobilesurvey.name()"></h3>
<h3 class="project-panel-title" data-bind="text: vm.mobilesurvey.name() === '' ? '{% trans "Unnamed Survey" %}' : vm.mobilesurvey.name()"></h3>

<div class="">
<div class="project-details">
<a id="view-project-details-btn" class="text-primary text-semibold" data-bind="click: function(val){val.toggleShowDetails(); $parent.mobileSurveyManager.getMobileSurveyResources(val)}">{% trans 'View Details' %}</a>
<a id="view-project-details-btn" class="text-primary text-semibold" data-bind="click: function(val){val.mobilesurvey.toggleShowDetails(); val.getMobileSurveyResources(val)}">{% trans 'View Details' %}</a>
</div>
</div>
</div>

<div class='profile-mpm-panel' data-bind="css: {'show-details': mobilesurvey.showDetails}">
<div class='profile-mpm-panel' data-bind="css: {'show-details': vm.mobilesurvey.showDetails}">
<div class="">
<p class="mpm-item-listing-header" style="margin-bottom: 0px;">
{% trans 'Survey Summary' %}
Expand All @@ -362,16 +362,16 @@ <h3 class="project-panel-title" data-bind="text: mobilesurvey.name() === '' ? '{
<ul class="mpm-node-detail-metadata">
<li class="">

{% trans 'Groups' %} (<span data-bind="text: mobilesurvey.groups().length"></span>)
<ul class="mpm-node-detail-metadata" data-bind="foreach: _.filter(mobilesurvey.identities, function(id){return (id.type === 'group' && _.contains(mobilesurvey.groups(), id.id))})">
{% trans 'Groups' %} (<span data-bind="text: vm.mobilesurvey.groups().length"></span>)
<ul class="mpm-node-detail-metadata" data-bind="foreach: _.filter(vm.mobilesurvey.identities, function(id){return (id.type === 'group' && _.contains(mobilesurvey.groups(), id.id))})">
<li class="" data-bind="text: $data.name"></li>
</ul>
</li>
</ul>
<ul class="mpm-node-detail-metadata">
<li class="">
{% trans 'Users' %} (<span data-bind="text: mobilesurvey.users().length"></span>)
<ul class="mpm-node-detail-metadata" data-bind="foreach: _.filter(mobilesurvey.identities, function(id){return (id.type === 'user' && _.contains(mobilesurvey.users(), id.id))})">
{% trans 'Users' %} (<span data-bind="text: vm.mobilesurvey.users().length"></span>)
<ul class="mpm-node-detail-metadata" data-bind="foreach: _.filter(vm.mobilesurvey.identities, function(id){return (id.type === 'user' && _.contains(vm.mobilesurvey.users(), id.id))})">
<li class="" data-bind="text: $data.name"></li>
</ul>
</li>
Expand All @@ -384,7 +384,7 @@ <h3 class="project-panel-title" data-bind="text: mobilesurvey.name() === '' ? '{
</p>

<ul class="mpm-node-detail-metadata">
<!--ko foreach: {data: mobilesurvey.resources(), as: 'resource'} -->
<!--ko foreach: {data: vm.resources, as: 'resource'} -->
<li class="">
<div data-bind="text: resource.name + ' (' + resource.cards.length + ' data entry cards)'"></div>
<!--ko foreach: {data: resource.cards, as: 'card'} -->
Expand Down

0 comments on commit e0d2d5b

Please sign in to comment.