Commit 2ac9175e authored by alexandre's avatar alexandre

WIP: experiment with radio messages

parent 1b0a1b72
......@@ -6,12 +6,22 @@ window.W = window.W || {};
(function(undefined) {
'use strict';
function is_external_link(el) {
// data-external is used for attachments
return (el.host !== window.location.host) || ('external' in el.dataset);
}
W.ScoreApp = Marionette.Application.extend({
channelName: 'user',
radioRequests: {
'user:loggedIn': 'isLoggedIn',
'user:username': 'username'
},
username: function() {
return this.model.get("username");
},
isLoggedIn: function() {
return this.model.get("pk");
},
W.ScoreApp = new Marionette.Application({
region: 'body',
triggers: {
......@@ -19,10 +29,10 @@ window.W = window.W || {};
},
onStart: function(options) {
var userAuthModel = new W.UserAuthModel();
var baseView = new W.BaseView({model: userAuthModel});
this.model = new W.UserAuthModel();
var baseView = new W.BaseView({model: this.model});
this.showView(baseView);
userAuthModel.fetch();
baseView.model.fetch();
// hide modals when typing escape
$(document).on("keydown", function(event) {
......@@ -35,7 +45,7 @@ window.W = window.W || {};
$(document).on("click", "a", function(e) {
// Do not interfere with Backbone Paginator.
if (! e.isDefaultPrevented() && !is_external_link(e.target)) {
if (! e.isDefaultPrevented() && ! W.utils.is_external_link(e.target)) {
e.preventDefault(); // This is important
var href = $(e.currentTarget).attr('href');
......
......@@ -32,5 +32,6 @@ window.W = window.W || {};
W.config.lang = W.utils.getUserLanguage();
var scoreApp = W.ScoreApp.start();
var scoreApp = new W.ScoreApp();
scoreApp.start();
})();
......@@ -206,6 +206,24 @@ window.W = window.W || {};
return data;
},
hasPerm: function(perm) {
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:loggedIn');
var username = channel.request('user:username');
var permissions = this.get("permissions");
var userPerm = permissions.findWhere({username: username}) || permissions.findWhere({username: "AnonymousUser"});
var hasPerm = false;
if (userPerm) {
hasPerm = _.contains(userPerm.get("permissions"), perm);
} else {
hasPerm = false;
}
return hasPerm;
}
});
......
......@@ -6,6 +6,11 @@ window.W.utils = window.W.utils || {};
(function (undefined) {
'use strict';
W.utils.is_external_link = function (el) {
// data-external is used for attachments
return (el.host !== window.location.host) || ('external' in el.dataset);
};
W.utils.animatedScroll = function (el, distance, duration) {
var start = Date.now(),
......
......@@ -1776,7 +1776,8 @@ window.W = window.W || {};
depth: this.model.getDepth(),
rangeType: rangeType,
formatTimestamp: formatTimestamp,
metaExpanded: this.metaExpanded
metaExpanded: this.metaExpanded,
canChangeScore: this.model.hasPerm('change_score')
};
},
......@@ -2612,13 +2613,23 @@ window.W = window.W || {};
W.AboutView = Marionette.View.extend({
template: '#about-template',
attributes: { class: 'body-text body-text--about content' }
attributes: { class: 'body-text body-text--about content' },
});
W.HomeView = Marionette.View.extend({
template: '#home-template',
templateContext: function () {
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:isLoggedIn');
return {
isLoggedIn: isLoggedIn
};
},
regions: {
latest_updated: '#latest_updated',
user_scores: '#user_scores'
......@@ -2656,29 +2667,36 @@ window.W = window.W || {};
}
});
var scoreCollection2 = new W.ScoreCollection([], {
// All the `state` and `queryParams` key value pairs are merged with
// the defaults too.
state: {
pageSize: 5,
firstPage: 1,
currentPage: 1,
sortKey: "updated_at",
order: -1
}
});
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:loggedIn');
console.log("****************");
console.log(isLoggedIn);
if (isLoggedIn) {
var scoreCollection2 = new W.ScoreCollection([], {
// All the `state` and `queryParams` key value pairs are merged with
// the defaults too.
state: {
pageSize: 5,
firstPage: 1,
currentPage: 1,
sortKey: "updated_at",
order: -1
}
});
scoreCollection2.queryParams.shared_with=true;
scoreCollection2.queryParams.shared_with=true;
var myListView2 = new W.ScoreListFooView({
collection: scoreCollection2
});
var myListView2 = new W.ScoreListFooView({
collection: scoreCollection2
});
myListView2.collection.fetch({
success: function () {
that.showChildView('user_scores', myListView2);
}
});
myListView2.collection.fetch({
success: function () {
that.showChildView('user_scores', myListView2);
}
});
};
}
});
......
......@@ -15,10 +15,12 @@
<br>
<div id="latest_updated"></div>
<% if (isLoggedIn) { %>
<br>
<br>
<h1><%- t('Mes partitions') %></h1>
<br>
<div id="user_scores"></div>
<% } %>
</section>
......@@ -3,10 +3,12 @@
<h1 id="title"><%- title %></h1>
<div class="score-actions">
<% if (canChangeScore) { %>
<button class="save" data-label-progress="<%- t('Enregistrer…') %>" data-label="<%- t('Enregistrer') %>" <% if (!hasChanges) { %>disabled<% } %>><%- t('Enregistrer') %></button>
<button class="duplicate"><%- t('Dupliquer') %></button>
<button class="delete"><%- t('Supprimer') %></button>
<!--<button data-name="export"><%- t('Export') %></button>-->
<% } %>
</div>
</header>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment