SwellRT integration for angular.js:
Use collaborative javascript models composed by:
Use collaborative text edition in your angular projects thanks to the ready to use rich text collaborative editor from SwellRT.
Angular-SwellRT provides an intuitive way to use the powerful SwellRT technology on your angular projects.
The communication and storage of your Angular models has never been so easy: first, you get your object from a SwellRT data model, then, any changes you do in the object propagates in real time to any other client using that data model and is automatically persisted in the SwellRT federated infrastructure.
Angular-SwellRT is available at bower, to use it just run
bower install angular-swellrt
. You can also directly download this repository
Include angular-swellrt.js source to your angular project:
./bower_components/angular-swellrt/dist/angular-swellrt.js'
as a javascript source file.angular
.module('App', [
...,
'SwellRTService'
]).
You have several options for the SwellRT server:
https://demo-swellrt.p2pvalue.eu
Create a SwellRT user: SwellRT provides access control and user management. To start, you can just share a user among all the collaborating clients.
SwellRT.registerUser("http://swellrt-server.net","user@swellrt-server.net","password", successCallback, errorCallback);
Start a SwellRT session:
window.onSwellRTReady = function () {
window.SwellRT.startSession(
"http://swellrt-server.net","user@swellrt-server.net","password", successCallback, errorCallback)
}
Create a SwellRT model: the data is shared and stored through SwellRT models. To create a model, run:
var modelId = window.SwellRT.createModel();
the return value modelId
is the identifier of the data model, used to access it as explained next.
Create the collaborative proxy object: create a proxy object that changes when SwellRT model changes and propagate the changes you do on it:
angular.module('App').controller('TextEditor', ['swellRT', function(swellRT) {
window.SwellRT.openModel(modelId, function(model){
$scope.proxy = swellRT.proxy(model);
);
}
Once you get a proxy object of the model, any compatible change will be propagated, some examples are given bellow:
// add a string to the 'newKey' key of the model
proxy.newKey = "my new collaborative string";
// add a list with a simple object and a string to the 'listKey' key of the model
proxy.listKey = [{foo = "foo"}, "bar"];
// remove the object at key 'newKey' of the model
delete proxy.newKey;
Angular-SwellRT collaborative objects have a special attribute _participants
to manage the participants of the SwellRT model.
// add a participant to the model
proxy._participants.push('foo@local.net');
// make a model public:
proxy._participants.push('@local.net');
In general, avoid assigning undefined
and function
values to proxy paths that should propagate their changes to SwellRT model.
SwellRT does not have any undefined
or function
object types, therefore there is not a correct way of propagating those changes. The bhaviour when asigning an undefined
value to a property in the proxy object is:
undefined
will hold an undefined
value in the proxy and a SwellRT.createString(undefined)
value in the SwellRT model.undefined
will clear their previous value holding a cleared FileType object at the SwellRT model and an undefined
value in the proxy.undefined
will stop propagating changes from their path (i.e. if an Array or a Map is assigned to undefined
, the proxy object will have an undefined value in that path but the SwellRT model will keep the previous values. Further changes in that path will not be propatagted.undefined
will not change the SwellRT model.To change directly the type of an already observed path will result in unexpected behaviour. Please, first delete
and then add the new object of a ifferent type if you need to change the type of an attribute.
Create the rich-text object: In a proxy of a SwellRT model (see instructions above), add a TextObject:
$scope.proxy.myText = new swellRT.TextObject()
Add the editor to the view and attach it to the text model:
<div class="swellrt-editor" ng-model="proxy.myText" placeholder="This is a SwellRT collaborative editor! write here in collaboration with others" block-edit="false" swellrt-editor-on-ready="callback()"></div>
ng-model
takes the value of the TextObjectplaceholder
is an optional parameter to show a placeholder when the TextObject is emptyblock-edit
is an optional parameters block/allow editions of the editor's content.swellrt-editor-on-ready
allows to execute an scope function after the editor is builtnote: TextObjects are not observed, since their changes are already propagated by SwellRT. To change the value that the model has at the path where a TextObject is attached, first delete that object.
Create the attachement object: In a proxy of a SwellRT model (see instructions above), add a FileObject:
// Get a HTML5 File object from an input element.
var inputFile = document.getElementById("inputFileElementId").files[0];
$scope.proxy.myAttachement = new swellRT.FileObject(inputFile);
$scope.proxy.myAttachement.url
. Also, a promise is return by the getUrl
method, which will be resolved when the file is succesfully uploaded: $scope.proxy.myAttachement.getUrl(function(url){console.log('the attachement url:', url)})
a collection of generated avatars can be obtained with the directive swellrtAvatars
which receives a list of user names and returns avatars built with their initials. This feature is based in SwellRT avatars
<div class="swellrt-avatars" ng-model="[foo@bar.net, foobar@baz.com]" swellrt-avatar-options = "{numberOfAvatars : 3}"></div>
See SwellRT avatars to learn what can be used in swellrt-avat-options parameter (size, number of avatars, etc).
Clone this repository using git:
git clone P2Pvalue/angular-swellrt.git
cd angular-swellrt
Angular-swellRT uses npm and bower to manage dependencies, run the following code to get them:
npm install
bower install
Angular-swellRT uses Babel to compile its ECMAScript 6 code to a previous javascript version. Following gulp commands does this job, resulting in the file dist/angular-swellrt.js:
To run it once:
gulp dist
To run it for every source code change:
gulp live:dist