Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy Redactor instance when scope is destroyed #75

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions angular-redactor-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
});
}
};

scope.$on('$destroy', function() {
element.redactor('core.destroy');
});
}
};
}]);
Expand Down
111 changes: 111 additions & 0 deletions angular-redactor-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
(function() {
'use strict';

/**
* usage: <textarea ng-model="content" redactor></textarea>
*
* additional options:
* redactor: hash (pass in a redactor options hash)
*
*/

var redactorOptions = {};

angular
.module('angular-redactor', [])
.constant('redactorOptions', redactorOptions)
.directive('redactor', [
'$timeout',
function($timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
function _get(obj, path, defaultValue, justExistence) {
if (!path) return undefined;
var tree = path.split(/(?:\[|\]|\]?\.)/i).filter(i => i !== "");
let fullDepth = obj || {};
while (tree.length) {
const currentDepth = tree.shift();
if (fullDepth[currentDepth]) {
fullDepth = fullDepth[currentDepth];
} else {
tree.splice(0);
return defaultValue;
}
}
if (justExistence) {
return typeof fullDepth !== "undefined" && fullDepth !== null;
}
return fullDepth;
}

function _hasIn(obj, path) {
return _get(obj, path, undefined, true);
}

var randomId =
'redactor-' +
Math.floor((1 + Math.random()) * 2e13)
.toString(16)
.substr(1) +
+new Date(),
debounce;

element.attr('id', randomId);

// Expose scope var with loaded state of Redactor
scope.redactorLoaded = false;

var updateModel = function updateModel(value) {
if (debounce) {
$timeout.cancel(debounce);
}
// $timeout to avoid $digest collision
debounce = $timeout(function() {
if (_hasIn(ngModel, '$setViewValue')) {
try {
ngModel.$setViewValue(value);
} catch(e) {}
}
debounce = undefined;
}, 250);
};

var options = {
spellcheck: false,
callbacks: {
changed: updateModel
}
},
additionalOptions = attrs.redactor
? scope.$eval(attrs.redactor)
: {},
editor;

angular.extend(options, redactorOptions, additionalOptions);

// put in timeout to avoid $digest collision. call render()
// to set the initial value.
$timeout(function() {
editor = $R('#' + randomId, options);
ngModel.$render();
});

ngModel.$render = function() {
if (angular.isDefined(editor)) {
$timeout(function() {
editor.source.setCode(ngModel.$viewValue || '');
scope.redactorLoaded = true;
});
}
};

scope.$on('$destroy', function() {
$R('#' + randomId, 'destroy');
});
}
};
}
]);
})();
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-redactor",
"main": "angular-redactor.js",
"version": "1.1.5",
"main": "angular-redactor-3.js",
"version": "1.1.7",
"homepage": "https://github.com/TylerGarlick/angular-redactor",
"authors": [
"Tyler Garlick <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-redactor",
"version": "1.1.7",
"version": "1.1.10.1",
"description": "Directive for redactor WYSIWYG editor",
"main": "angular-redactor.js",
"scripts": {
Expand Down