wejsv2old-plugin-rating
Version:
We.js v2 Old - Rating Plugin
240 lines (210 loc) • 7.37 kB
JavaScript
/*
* WeRatingComponent.js - Plugin to rate contents
*
* How to use:
*
* @param {string} modelName - Model's name
* @param {string} modelId - Model record's id that is being evaluated
*
* {{we-rating modelName="relato" modelId="1313" modal="we-relato-rating-modal"}}
*
* Add the outlet below on the route template where the ratign will be donde
* {{outlet 'ratingModal'}}
*
* Add the action on the route's router to render rating modal into the previous outlet
*
* renderRatingModal: function (controller, template) {
* this.render(template, { // the template to render
* into: 'relato', // the template to render into
* outlet: 'ratingModal', // the name of the outlet in that template
* controller: controller // the controller to use for the template
* });
* }
*
*/
App.WeRatingComponent = Ember.Component.extend({
target: Em.computed.alias('targetObject'),
saving: false,
rated: false,
rating: 0,
action: 'renderRatingModal',
ratingDone: 'ratingDone',
tagName: 'button',
classNameBindings: ['hide', 'rated', 'allowRate::hide'],
text: function () {
return this.get('rated') ? Ember.I18n.translations['rating.open.button.rated'] : Ember.I18n.translations['rating.open.button'];
}.property('rated'),
isRated: Ember.computed.alias('rated'),
hide: function () {
return !this.get('manager.active');
}.property('manager.active'),
isValid: function () {
return this.get('rating') > 0 && this.get('selectedTerms.length') > 0;
}.property('rating', 'selectedTerms.[]'),
allowRate: function (){
if (App.currentUser.id == this.get('model.creator.id')) {
this.set('allowRateAlias', false);
return false;
}
// this only applies for relatos
if (this.get('model.autores') && this.get('model.autores.length')){
var arr = this.get('model.autores').map(function (a){
return a.id;
});
if (arr.indexOf(App.currentUser.id) > -1){
return false;
}
}
this.set('allowRateAlias', true);
return true;
}.property('model.creator', 'model.autores'),
checkPermission: function () {
var self = this;
Ember.assert('\'modelName\' must be provided', self.get('modelName'));
Ember.assert('\'modelId\' must be provided', self.get('modelId'));
Ember.assert('\'modal\' must be provided', self.get('modal'));
var queryManager = {};
if (!self.get('ratingManager')) {
queryManager.modelName = self.get('modelName');
} else {
queryManager = self.get('ratingManager');
}
var rM = self.store.find('ratingmanager', queryManager);
rM
.then(function (aRatingManager) { // Get the associated cached content
var ratingmanager = aRatingManager;
if (Ember.isArray(ratingmanager)) ratingmanager = ratingmanager.get('firstObject');
self.set('manager', ratingmanager);
return self.store
.findById(self.get('modelName'), self.get('modelId'));
})
.then(function (model){ // Get the terms associated with the vocabulary
self.set('model', model);
return self.store.find('term', {
vocabulary: self.get('manager.vocabulary.id')
});
})
.then(function (terms) { // If the user is logged in, check if it has voted already
self.set('terms', terms);
if (!App.currentUser.id) return;
self.store.find('ratingvote', {
modelId: Number(self.get('modelId')),
creator: Number(App.currentUser.id),
manager: Number(self.get('manager.id'))
}).then(function (rate) {
if (rate.get('length') > 0) {
self.set('rated', true);
}
});
});
// Bind handler to event to allow multiple instances of this component to be shared
we.events.on('we-rating:relato:' + self.get('modelId') + ':rated', function (rate){
self.setProperties({
rated: true,
rating: rate.get('rating')
});
});
}.on('init'),
onModalIdChange: function (){
var self = this;
// Bind handler to event to allow multiple instances of this component to be shared
we.events.on('we-rating:relato:' + self.get('modelId') + ':rated', function (rate){
self.setProperties({
rated: true,
rating: rate.get('rating')
})
});
self.set('rated', false);
if (!App.currentUser.id) return;
self.store.findById(self.get('modelName'), self.get('modelId'))
.then(function (model){
self.set('model', model);
return self.store.find('ratingvote', {
modelId: Number(self.get('modelId')),
creator: Number(App.currentUser.id),
manager: Number(self.get('manager.id'))
})
})
.then(function (rate) {
if (rate.get('length') > 0) {
self.set('rated', true);
}
});
}.observes('modelId'),
bindDelegator: function () {
if (this.get('delegate')) {
this.get('delegate').set(this.get('property') || 'WeRatingComponent', this);
}
}.on('init'),
modalId: function () {
return this.get('modal') + this.get('modelId');
}.property('modal'),
onManagerActive: function () {
if (this.get('manager.active')) this.sendAction('action', this, 'we-relato-rating-modal');
return;
}.observes('manager.active'),
proxiedTerms: Ember.computed.map('terms', function (t) {
return Ember.ObjectProxy.create({
content: t,
checked: false
});
}),
proxiedSelectedTerms: Ember.computed.filterBy('proxiedTerms', 'checked', true),
selectedTerms: Ember.computed.mapBy('proxiedSelectedTerms', 'content'),
click: function () {
if (!App.currentUser.id) {
$('#we-auth-modal-need-login').modal('show');
return;
}
$('#' + this.get('modalId'))
.modal('show')
.on('hidden.bs.modal', function () {
this.sendAction('close');
}.bind(this));
},
_yield: function (context, options) {
var get = Ember.get,
view = options.data.view,
parentView = this._parentView,
template = get(this, 'template');
if (template) {
Ember.assert('A Component must have a parent view in order to yield.', parentView);
view.appendChild(Ember.View, {
isVirtual: true,
tagName: '',
_contextView: parentView,
template: template,
context: get(view, 'context'), // the default is get(parentView, 'context'),
controller: get(parentView, 'controller'),
templateData: {
keywords: parentView.cloneKeywords()
}
});
}
},
actions: {
rate: function () {
var self = this;
var newRating = {
modelId: self.get('modelId'),
manager: self.get('manager'),
rate: self.get('rating'),
creator: App.currentUser
};
self.set('saving', true);
var rating = self.store.createRecord('ratingvote', newRating);
rating.get('terms').pushObjects(self.get('selectedTerms'))
rating
.save()
.then(function (rate) {
self.setProperties({
saving: false,
rated: true
});
// Trigger we.event to notify all components
we.events.trigger('we-rating:relato:' + self.get('modelId') + ':rated', rate);
self.sendAction('ratingDone');
});
}
}
});