Skip to content

Commit

Permalink
Merge pull request #1497 from adaptlearning/issue/1496
Browse files Browse the repository at this point in the history
Adds support for _classes to NotifyPushView
  • Loading branch information
moloko authored Mar 31, 2017
2 parents 9042fe3 + a146759 commit fca05d5
Showing 1 changed file with 101 additions and 103 deletions.
204 changes: 101 additions & 103 deletions src/core/js/views/notifyPushView.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,119 @@
define(function(require) {
define(function (require) {

var Backbone = require('backbone');
var Adapt = require('coreJS/adapt');
var Backbone = require('backbone');
var Adapt = require('coreJS/adapt');

var NotifyPushView = Backbone.View.extend({
var NotifyPushView = Backbone.View.extend({

className: 'notify-push',
className: function () {
return 'notify-push ' + this.model.get('_classes');
},

initialize: function() {
this.listenTo(Adapt, 'notify:pushShown notify:pushRemoved', this.updateIndexPosition);
this.listenTo(this.model.collection, 'remove', this.updateIndexPosition);
this.listenTo(this.model.collection, 'change:_index', this.updatePushPosition);
//include accessibility globals in notify model
this.model.set('_globals', Adapt.course.get('_globals'));
this.listenTo(Adapt, 'remove', this.remove);
this.preRender();
this.render();
},
initialize: function () {
this.listenTo(Adapt, 'notify:pushShown notify:pushRemoved', this.updateIndexPosition);
this.listenTo(this.model.collection, 'remove', this.updateIndexPosition);
this.listenTo(this.model.collection, 'change:_index', this.updatePushPosition);
this.listenTo(Adapt, 'remove', this.remove);

events: {
'click .notify-push-close': 'closePush',
'click .notify-push-inner': 'triggerEvent'
},
// Include accessibility globals in notify model.
this.model.set('_globals', Adapt.course.get('_globals'));

preRender: function() {
this.hasBeenRemoved = false;
},
this.preRender();
this.render();
},

render: function() {
events: {
'click .notify-push-close': 'closePush',
'click .notify-push-inner': 'triggerEvent'
},

preRender: function () {
this.hasBeenRemoved = false;
},

render: function () {
var data = this.model.toJSON();
var template = Handlebars.templates['notifyPush'];
this.$el.html(template(data)).appendTo('#wrapper');

_.defer(_.bind(function() {
_.defer(_.bind(function () {
this.postRender();
}, this));

return this;
},

postRender: function() {

this.$el.addClass('show');

_.delay(_.bind(function() {
this.closePush();
}, this), this.model.get('_timeout'));

Adapt.trigger('notify:pushShown');

},

closePush: function(event) {

if (event) {
event.preventDefault();
}

// Check whether this view has been removed as the delay can cause it to be fired twice
if (this.hasBeenRemoved === false) {

this.hasBeenRemoved = true;

this.$el.removeClass('show');

_.delay(_.bind(function() {
this.model.collection.remove(this.model);
Adapt.trigger('notify:pushRemoved', this);
this.remove();
}, this), 600);

}

},

triggerEvent: function(event) {

Adapt.trigger(this.model.get('_callbackEvent'));
this.closePush();

},

updateIndexPosition: function() {
if (!this.hasBeenRemoved) {
var models = this.model.collection.models;
for (var i = 0 , len = models.length; i < len; i++) {
var index = i;
var model = models[i];
if (model.get('_isActive') === true) {
model.set('_index', index);
this.updatePushPosition();
}
}
}
},

updatePushPosition: function() {
if (this.hasBeenRemoved) {
return;
}
if (this.model.get('_index') != undefined) {
var elementHeight = this.$el.height();
var offset = 20;
var navigationHeight = $('.navigation').height();
var currentIndex = this.model.get('_index');
var flippedIndex = (currentIndex == 0) ? 1 : 0;
if (this.model.collection.where({_isActive:true}).length === 1) {
flippedIndex = 0;
}
var positionLowerPush = (elementHeight + offset) * flippedIndex + navigationHeight + offset;
this.$el.css('top', positionLowerPush);
}
}

});

return NotifyPushView;
},

postRender: function () {
this.$el.addClass('show');

_.delay(_.bind(function () {
this.closePush();
}, this), this.model.get('_timeout'));

Adapt.trigger('notify:pushShown');
},

closePush: function (event) {
if (event) {
event.preventDefault();
}

// Check whether this view has been removed as the delay can cause it to be fired twice
if (this.hasBeenRemoved === false) {

this.hasBeenRemoved = true;

this.$el.removeClass('show');

_.delay(_.bind(function () {
this.model.collection.remove(this.model);
Adapt.trigger('notify:pushRemoved', this);
this.remove();
}, this), 600);
}
},

triggerEvent: function (event) {
Adapt.trigger(this.model.get('_callbackEvent'));
this.closePush();
},

updateIndexPosition: function () {
if (!this.hasBeenRemoved) {
var models = this.model.collection.models;
for (var i = 0, len = models.length; i < len; i++) {
var index = i;
var model = models[i];
if (model.get('_isActive') === true) {
model.set('_index', index);
this.updatePushPosition();
}
}
}
},

updatePushPosition: function () {
if (this.hasBeenRemoved) {
return;
}

if (this.model.get('_index') != undefined) {
var elementHeight = this.$el.height();
var offset = 20;
var navigationHeight = $('.navigation').height();
var currentIndex = this.model.get('_index');
var flippedIndex = (currentIndex == 0) ? 1 : 0;

if (this.model.collection.where({ _isActive: true }).length === 1) {
flippedIndex = 0;
}

var positionLowerPush = (elementHeight + offset) * flippedIndex + navigationHeight + offset;
this.$el.css('top', positionLowerPush);
}
}
});

return NotifyPushView;

});

0 comments on commit fca05d5

Please sign in to comment.