Skip to content

Commit

Permalink
added "activate!" and "deactivate!" messages for sticky iframe cells …
Browse files Browse the repository at this point in the history
…(via postmessenger)

motionbank#15
  • Loading branch information
martinleopold committed Nov 1, 2013
1 parent a56d5e2 commit 55c4d9d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
8 changes: 8 additions & 0 deletions app-base/models/cell-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = BaseModel.extend({
connection_id : null
},

// check if this cell has the sticky flag set
// TODO: using fields is not necessary. just use this.attributes.sticky with RegEx.test
isSticky : function() {
return _.any(this.attributes.fields, function(field) {
return ( field.name === 'sticky' && (field.value === 'true' || field.value === '1') );
});
},

set : function ( opts ) {
// override some attributes per set<->cell connection
if ( opts && typeof opts === 'object' && 'fields' in opts ) {
Expand Down
48 changes: 43 additions & 5 deletions app-base/views/cell-types/cell-iframe-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = CellDefaultView.extend({

// keep this in sync with cell-vimeo!
render : function () {

CellDefaultView.prototype.render.apply(this,arguments);

if ( this.model.attributes['autoload'] &&
Expand Down Expand Up @@ -91,12 +92,49 @@ module.exports = CellDefaultView.extend({
this.activate();
this.renderContent();
} else {
var isSticky = _.any(model.attributes.fields, function(field) {
return ( field.name === 'sticky' && (field.value === 'true' || field.value === '1') );
});
if (!isSticky) this.deactivate();
if (!model.isSticky()) this.deactivate();
}
}
}
},

activate : function () {
if ( !this.active ) {
console.log("activate iframe: " + this.cid);

this.active = true; // needs to be before render(). why? don't know...

if ( !this.model.isSticky() ) {
this.render();
} else {
if (!this.rendered) {
this.render();
var iframe = $('iframe', this.$el)[0];
var that = this;
$(iframe).one('load', function(){
pm.send(that.active ? 'activate!' : 'deactivate!', {}, this.contentWindow );
that.rendered = true;
});
} else {
var iframe = $('iframe', this.$el)[0];
pm.send( 'activate!', {}, iframe.contentWindow );
}}

}
},

deactivate : function () {
if ( this.active ) {
if (!this.model.isSticky()) {
console.log("deactivate iframe: " + this.cid);
this.$el.empty();
this.rendered = false;
} else {
var iframe = $('iframe', this.$el)[0];
if (iframe) {
pm.send('deactivate!', {}, iframe.contentWindow );
}
}
this.active = false;
}
}
});
1 change: 1 addition & 0 deletions app-base/views/cell-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = BaseView.extend({
BaseView.prototype.render.apply(this,arguments);

this.$el.addClass( 'type-' + this.model.get('type') );
this.$el.addClass( this.cid ); // add view id as class (viewXX) to make debugging easier

// use cell fields that start with "css-" as css attributes on the $el
var cssOpts = {
Expand Down

0 comments on commit 55c4d9d

Please sign in to comment.