Created
June 16, 2012 14:56
-
-
Save mandrasch/2941550 to your computer and use it in GitHub Desktop.
PagedownBootstrap for AngularJS (directive)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*usage: <textarea ui:pagedown-bootstrap ng:model="box.content"></textarea> | |
*/ | |
myApp.directive('uiPagedownBootstrap', function() { | |
var nextId = 0; | |
return { | |
require: 'ngModel', | |
replace:true, | |
template:'<div class="pagedown-bootstrap-editor"></div>', | |
link:function (scope, iElement, iAttrs, ngModel) { | |
var editorUniqueId = nextId++; | |
iElement.html($('<div>'+ | |
'<div class="wmd-panel">'+ | |
'<div id="wmd-button-bar-'+editorUniqueId+'"></div>'+ | |
'<textarea class="wmd-input" id="wmd-input-'+editorUniqueId+'">{{modelValue()}}'+ | |
'</textarea>'+ | |
'</div>'+ | |
'<div id="wmd-preview-'+editorUniqueId+'" class="wmd-panel wmd-preview"></div>'+ | |
'</div>')); | |
var converter = new Markdown.Converter(); | |
var help = function () { | |
// 2DO: add nice modal dialog | |
alert("Do you need help?"); | |
} | |
var editor = new Markdown.Editor(converter, "-"+editorUniqueId, { | |
handler: help | |
}); | |
editor.run(); | |
// local -> parent scope change (model) | |
$("#wmd-input-"+editorUniqueId).on('change',function(){ | |
var rawContent = $(this).val(); | |
ngModel.$setViewValue(rawContent); | |
scope.$apply(); | |
}); | |
// parent scope -> local change | |
scope.modelValue = function() { | |
console.log('modelvalue - ',ngModel.$viewValue); | |
return ngModel.$viewValue; | |
}; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment