keeweb/app/scripts/mixins/view.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const Tip = require('../util/tip');
2015-10-17 23:49:24 +02:00
_.extend(Backbone.View.prototype, {
hide: function() {
2016-02-28 11:36:56 +01:00
Tip.hideTips(this.$el);
2015-10-17 23:49:24 +02:00
return this.toggle(false);
},
show: function() {
return this.toggle(true);
},
toggle: function(visible) {
2015-10-24 11:15:54 +02:00
if (visible === undefined) {
visible = this._hidden;
}
this.$el.toggleClass('show', !!visible);
2015-10-17 23:49:24 +02:00
this.$el.toggleClass('hide', !visible);
this._hidden = !visible;
this.trigger(visible ? 'show' : 'hide');
2016-02-28 11:36:56 +01:00
if (!visible) {
Tip.hideTips(this.$el);
}
2015-10-17 23:49:24 +02:00
return this;
},
2015-10-31 20:09:32 +01:00
isHidden: function() {
return this._hidden;
},
2016-08-21 23:12:49 +02:00
isVisible: function() {
return !this._hidden;
},
2015-10-17 23:49:24 +02:00
afterPaint: function(callback) {
this.requestAnimationFrame(function() {
this.requestAnimationFrame(callback);
});
},
2016-01-16 13:35:34 +01:00
setTimeout: function(callback) {
setTimeout(callback.bind(this), 0);
},
2015-10-17 23:49:24 +02:00
requestAnimationFrame: function(callback) {
requestAnimationFrame(callback.bind(this));
},
renderTemplate: function(model, replace) {
2016-02-28 11:36:56 +01:00
Tip.hideTips(this.$el);
if (replace && replace.plain) {
this.$el.html(this.template(model));
2015-10-17 23:49:24 +02:00
} else {
if (replace) {
this.$el.html('');
}
2017-01-31 07:50:28 +01:00
const el = $(this.template(model));
if (!this._elAppended || replace) {
this.$el.append(el);
this._elAppended = true;
} else {
this.$el.replaceWith(el);
}
this.setElement(el);
2015-10-17 23:49:24 +02:00
}
Tip.createTips(this.$el);
2015-10-17 23:49:24 +02:00
},
_parentRemove: Backbone.View.prototype.remove,
remove: function() {
this.trigger('remove');
2016-04-23 16:50:40 +02:00
this.removeInnerViews();
if (this.scroll) {
2016-07-17 13:30:38 +02:00
try {
this.scroll.dispose();
2019-08-16 23:05:39 +02:00
} catch (e) {}
2016-04-23 16:50:40 +02:00
}
Tip.hideTips(this.$el);
this._parentRemove(arguments);
},
removeInnerViews: function() {
2015-10-17 23:49:24 +02:00
if (this.views) {
2016-07-17 13:30:38 +02:00
_.each(this.views, view => {
2015-10-17 23:49:24 +02:00
if (view) {
if (view instanceof Backbone.View) {
view.remove();
} else if (view.length) {
2016-07-17 13:30:38 +02:00
view.forEach(v => v.remove());
2015-10-17 23:49:24 +02:00
}
}
});
2016-04-23 16:50:40 +02:00
this.views = {};
2015-10-17 23:49:24 +02:00
}
},
deferRender: function() {
_.defer(this.render.bind(this));
2015-10-17 23:49:24 +02:00
}
});
module.exports = Backbone.View;