Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions lib/client/templates.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
Template.afSummernote.created = function () {
this.value = new ReactiveVar(this.data.value);
};

Template.afSummernote.rendered = function() {
Template.afSummernote.onRendered(function() {
var self = this;
var options = this.data.atts.settings || {};
var options = {};
if (this.data && this.data.atts) {
options = this.data.atts.settings || {};
} else {
options = this.data;
}
var $editor = $(this.firstNode);

var onblur = options.onblur;
options.onblur = function(e) {
$editor.change();
if (typeof onblur === 'function') {
onblur.apply(this, arguments);
}
};
this.$editor = $editor
this.onblur = options.onblur;

$editor.summernote(options);


var value = null;
this.autorun(function () {
$editor.summernote('code', self.value.get());
var dataContext = Template.currentData();
if (value!=dataContext.value) {
$editor.summernote('code', dataContext.value);
value = dataContext.value;
}
});

$editor.closest('form').on('reset', function() {
$editor.summernote('code', '');
});
};
});


Template.afSummernote.events({
'summernote.blur': function(event, template) {
// Unfortunatly there seems to be an issue where value is not updated in the
// summernote code in some cases. We are just garenteeing that this is updated before
// triggering the autform change detection
template.$(event.target)[0].value = template.$(event.target).summernote('code');
template.$(event.target).change();

var onblur = template.onblur;
if (typeof onblur === 'function') {
onblur.apply(this, arguments);
}
}
});

Template.afSummernote.onDestroyed(function() {
if (this.$editor){
this.$editor.summernote('destroy');
}
});

Template.afSummernote.helpers({
atts: function () {
var self = this;

/**
* This is bit hacky but created and rendered callback sometimes
* (or always?) get empty value. This helper gets called multiple
* times so we intercept and save the value once it is not empty.
*/
Tracker.nonreactive(function () {
var t = Template.instance();
if (t.value.get() !== self.value) {
t.value.set(self.value);
}
});

return _.omit(this.atts, 'settings');
}
});
});