/
opentok-editor.min.js
1 lines (1 loc) · 29.4 KB
/
opentok-editor.min.js
1
if("undefined"==typeof ot)var ot={};if(ot.TextOperation=function(){"use strict";function t(){return this&&this.constructor===t?(this.ops=[],this.baseLength=0,this.targetLength=0,void 0):new t}function e(e){var n=e.ops,o=t.isRetain;switch(n.length){case 1:return n[0];case 2:return o(n[0])?n[1]:o(n[1])?n[0]:null;case 3:if(o(n[0])&&o(n[2]))return n[1]}return null}function n(t){return o(t.ops[0])?t.ops[0]:0}t.prototype.equals=function(t){if(this.baseLength!==t.baseLength)return!1;if(this.targetLength!==t.targetLength)return!1;if(this.ops.length!==t.ops.length)return!1;for(var e=0;e<this.ops.length;e++)if(this.ops[e]!==t.ops[e])return!1;return!0};var o=t.isRetain=function(t){return"number"==typeof t&&t>0},r=t.isInsert=function(t){return"string"==typeof t},i=t.isDelete=function(t){return"number"==typeof t&&0>t};return t.prototype.retain=function(t){if("number"!=typeof t)throw new Error("retain expects an integer");return 0===t?this:(this.baseLength+=t,this.targetLength+=t,o(this.ops[this.ops.length-1])?this.ops[this.ops.length-1]+=t:this.ops.push(t),this)},t.prototype.insert=function(t){if("string"!=typeof t)throw new Error("insert expects a string");if(""===t)return this;this.targetLength+=t.length;var e=this.ops;return r(e[e.length-1])?e[e.length-1]+=t:i(e[e.length-1])?r(e[e.length-2])?e[e.length-2]+=t:(e[e.length]=e[e.length-1],e[e.length-2]=t):e.push(t),this},t.prototype["delete"]=function(t){if("string"==typeof t&&(t=t.length),"number"!=typeof t)throw new Error("delete expects an integer or a string");return 0===t?this:(t>0&&(t=-t),this.baseLength-=t,i(this.ops[this.ops.length-1])?this.ops[this.ops.length-1]+=t:this.ops.push(t),this)},t.prototype.isNoop=function(){return 0===this.ops.length||1===this.ops.length&&o(this.ops[0])},t.prototype.toString=function(){var t=Array.prototype.map||function(t){for(var e=this,n=[],o=0,r=e.length;r>o;o++)n[o]=t(e[o]);return n};return t.call(this.ops,function(t){return o(t)?"retain "+t:r(t)?"insert '"+t+"'":"delete "+-t}).join(", ")},t.prototype.toJSON=function(){return this.ops},t.fromJSON=function(e){for(var n=new t,s=0,a=e.length;a>s;s++){var c=e[s];if(o(c))n.retain(c);else if(r(c))n.insert(c);else{if(!i(c))throw new Error("unknown operation: "+JSON.stringify(c));n["delete"](c)}}return n},t.prototype.apply=function(t){var e=this;if(t.length!==e.baseLength)throw new Error("The operation's base length must be equal to the string's length.");for(var n=[],i=0,s=0,a=this.ops,c=0,p=a.length;p>c;c++){var h=a[c];if(o(h)){if(s+h>t.length)throw new Error("Operation can't retain more characters than are left in the string.");n[i++]=t.slice(s,s+h),s+=h}else r(h)?n[i++]=h:s-=h}if(s!==t.length)throw new Error("The operation didn't operate on the whole string.");return n.join("")},t.prototype.invert=function(e){for(var n=0,i=new t,s=this.ops,a=0,c=s.length;c>a;a++){var p=s[a];o(p)?(i.retain(p),n+=p):r(p)?i["delete"](p.length):(i.insert(e.slice(n,n-p)),n-=p)}return i},t.prototype.compose=function(e){var n=this;if(n.targetLength!==e.baseLength)throw new Error("The base length of the second operation has to be the target length of the first operation");for(var s=new t,a=n.ops,c=e.ops,p=0,h=0,u=a[p++],l=c[h++];;){if("undefined"==typeof u&&"undefined"==typeof l)break;if(i(u))s["delete"](u),u=a[p++];else if(r(l))s.insert(l),l=c[h++];else{if("undefined"==typeof u)throw new Error("Cannot compose operations: first operation is too short.");if("undefined"==typeof l)throw new Error("Cannot compose operations: first operation is too long.");if(o(u)&&o(l))u>l?(s.retain(l),u-=l,l=c[h++]):u===l?(s.retain(u),u=a[p++],l=c[h++]):(s.retain(u),l-=u,u=a[p++]);else if(r(u)&&i(l))u.length>-l?(u=u.slice(-l),l=c[h++]):u.length===-l?(u=a[p++],l=c[h++]):(l+=u.length,u=a[p++]);else if(r(u)&&o(l))u.length>l?(s.insert(u.slice(0,l)),u=u.slice(l),l=c[h++]):u.length===l?(s.insert(u),u=a[p++],l=c[h++]):(s.insert(u),l-=u.length,u=a[p++]);else{if(!o(u)||!i(l))throw new Error("This shouldn't happen: op1: "+JSON.stringify(u)+", op2: "+JSON.stringify(l));u>-l?(s["delete"](l),u+=l,l=c[h++]):u===-l?(s["delete"](l),u=a[p++],l=c[h++]):(s["delete"](u),l+=u,u=a[p++])}}}return s},t.prototype.shouldBeComposedWith=function(t){if(this.isNoop()||t.isNoop())return!0;var o=n(this),s=n(t),a=e(this),c=e(t);return a&&c?r(a)&&r(c)?o+a.length===s:i(a)&&i(c)?s-c===o||o===s:!1:!1},t.prototype.shouldBeComposedWithInverted=function(t){if(this.isNoop()||t.isNoop())return!0;var o=n(this),s=n(t),a=e(this),c=e(t);return a&&c?r(a)&&r(c)?o+a.length===s||o===s:i(a)&&i(c)?s-c===o:!1:!1},t.transform=function(e,n){if(e.baseLength!==n.baseLength)throw new Error("Both operations have to have the same base length");for(var s=new t,a=new t,c=e.ops,p=n.ops,h=0,u=0,l=c[h++],f=p[u++];;){if("undefined"==typeof l&&"undefined"==typeof f)break;if(r(l))s.insert(l),a.retain(l.length),l=c[h++];else if(r(f))s.retain(f.length),a.insert(f),f=p[u++];else{if("undefined"==typeof l)throw new Error("Cannot compose operations: first operation is too short.");if("undefined"==typeof f)throw new Error("Cannot compose operations: first operation is too long.");var d;if(o(l)&&o(f))l>f?(d=f,l-=f,f=p[u++]):l===f?(d=f,l=c[h++],f=p[u++]):(d=l,f-=l,l=c[h++]),s.retain(d),a.retain(d);else if(i(l)&&i(f))-l>-f?(l-=f,f=p[u++]):l===f?(l=c[h++],f=p[u++]):(f-=l,l=c[h++]);else if(i(l)&&o(f))-l>f?(d=f,l+=f,f=p[u++]):-l===f?(d=f,l=c[h++],f=p[u++]):(d=-l,f+=l,l=c[h++]),s["delete"](d);else{if(!o(l)||!i(f))throw new Error("The two operations aren't compatible");l>-f?(d=-f,l+=f,f=p[u++]):l===-f?(d=l,l=c[h++],f=p[u++]):(d=l,f+=l,l=c[h++]),a["delete"](d)}}}return[s,a]},t}(),"object"==typeof module&&(module.exports=ot.TextOperation),"undefined"==typeof ot)var ot={};if(ot.Selection=function(t){"use strict";function e(t,e){this.anchor=t,this.head=e}function n(t){this.ranges=t||[]}var o=t.ot?t.ot.TextOperation:require("./text-operation");return e.fromJSON=function(t){return new e(t.anchor,t.head)},e.prototype.equals=function(t){return this.anchor===t.anchor&&this.head===t.head},e.prototype.isEmpty=function(){return this.anchor===this.head},e.prototype.transform=function(t){function n(e){for(var n=e,r=t.ops,i=0,s=t.ops.length;s>i&&(o.isRetain(r[i])?e-=r[i]:o.isInsert(r[i])?n+=r[i].length:(n-=Math.min(e,-r[i]),e+=r[i]),!(0>e));i++);return n}var r=n(this.anchor);return this.anchor===this.head?new e(r,r):new e(r,n(this.head))},n.Range=e,n.createCursor=function(t){return new n([new e(t,t)])},n.fromJSON=function(t){for(var o=t.ranges||t,r=0,i=[];r<o.length;r++)i[r]=e.fromJSON(o[r]);return new n(i)},n.prototype.equals=function(t){if(this.position!==t.position)return!1;if(this.ranges.length!==t.ranges.length)return!1;for(var e=0;e<this.ranges.length;e++)if(!this.ranges[e].equals(t.ranges[e]))return!1;return!0},n.prototype.somethingSelected=function(){for(var t=0;t<this.ranges.length;t++)if(!this.ranges[t].isEmpty())return!0;return!1},n.prototype.compose=function(t){return t},n.prototype.transform=function(t){for(var e=0,o=[];e<this.ranges.length;e++)o[e]=this.ranges[e].transform(t);return new n(o)},n}(this),"object"==typeof module&&(module.exports=ot.Selection),"undefined"==typeof ot)var ot={};if(ot.WrappedOperation=function(){"use strict";function t(t,e){this.wrapped=t,this.meta=e}function e(t,e){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])}function n(t,n){if(t&&"object"==typeof t){if("function"==typeof t.compose)return t.compose(n);var o={};return e(t,o),e(n,o),o}return n}function o(t,e){return t&&"object"==typeof t&&"function"==typeof t.transform?t.transform(e):t}return t.prototype.apply=function(){return this.wrapped.apply.apply(this.wrapped,arguments)},t.prototype.invert=function(){var e=this.meta;return new t(this.wrapped.invert.apply(this.wrapped,arguments),e&&"object"==typeof e&&"function"==typeof e.invert?e.invert.apply(e,arguments):e)},t.prototype.compose=function(e){return new t(this.wrapped.compose(e.wrapped),n(this.meta,e.meta))},t.transform=function(e,n){var r=e.wrapped.constructor.transform,i=r(e.wrapped,n.wrapped);return[new t(i[0],o(e.meta,n.wrapped)),new t(i[1],o(n.meta,e.wrapped))]},t}(this),"object"==typeof module&&(module.exports=ot.WrappedOperation),"undefined"==typeof ot)var ot={};if(ot.UndoManager=function(){"use strict";function t(t){this.maxItems=t||50,this.state=n,this.dontCompose=!1,this.undoStack=[],this.redoStack=[]}function e(t,e){for(var n=[],o=e.constructor,r=t.length-1;r>=0;r--){var i=o.transform(t[r],e);"function"==typeof i[0].isNoop&&i[0].isNoop()||n.push(i[0]),e=i[1]}return n.reverse()}var n="normal",o="undoing",r="redoing";return t.prototype.add=function(t,e){if(this.state===o)this.redoStack.push(t),this.dontCompose=!0;else if(this.state===r)this.undoStack.push(t),this.dontCompose=!0;else{var n=this.undoStack;!this.dontCompose&&e&&n.length>0?n.push(t.compose(n.pop())):(n.push(t),n.length>this.maxItems&&n.shift()),this.dontCompose=!1,this.redoStack=[]}},t.prototype.transform=function(t){this.undoStack=e(this.undoStack,t),this.redoStack=e(this.redoStack,t)},t.prototype.performUndo=function(t){if(this.state=o,0===this.undoStack.length)throw new Error("undo not possible");t(this.undoStack.pop()),this.state=n},t.prototype.performRedo=function(t){if(this.state=r,0===this.redoStack.length)throw new Error("redo not possible");t(this.redoStack.pop()),this.state=n},t.prototype.canUndo=function(){return 0!==this.undoStack.length},t.prototype.canRedo=function(){return 0!==this.redoStack.length},t.prototype.isUndoing=function(){return this.state===o},t.prototype.isRedoing=function(){return this.state===r},t}(),"object"==typeof module&&(module.exports=ot.UndoManager),"undefined"==typeof ot)var ot={};if(ot.Client=function(){"use strict";function t(t){this.revision=t,this.state=r}function e(){}function n(t){this.outstanding=t}function o(t,e){this.outstanding=t,this.buffer=e}t.prototype.setState=function(t){this.state=t},t.prototype.applyClient=function(t){this.setState(this.state.applyClient(this,t))},t.prototype.applyServer=function(t){this.revision++,this.setState(this.state.applyServer(this,t))},t.prototype.serverAck=function(){this.revision++,this.setState(this.state.serverAck(this))},t.prototype.serverReconnect=function(){"function"==typeof this.state.resend&&this.state.resend(this)},t.prototype.transformSelection=function(t){return this.state.transformSelection(t)},t.prototype.sendOperation=function(){throw new Error("sendOperation must be defined in child class")},t.prototype.applyOperation=function(){throw new Error("applyOperation must be defined in child class")},t.Synchronized=e,e.prototype.applyClient=function(t,e){return t.sendOperation(t.revision,e),new n(e)},e.prototype.applyServer=function(t,e){return t.applyOperation(e),this},e.prototype.serverAck=function(){throw new Error("There is no pending operation.")},e.prototype.transformSelection=function(t){return t};var r=new e;return t.AwaitingConfirm=n,n.prototype.applyClient=function(t,e){return new o(this.outstanding,e)},n.prototype.applyServer=function(t,e){var o=e.constructor.transform(this.outstanding,e);return t.applyOperation(o[1]),new n(o[0])},n.prototype.serverAck=function(){return r},n.prototype.transformSelection=function(t){return t.transform(this.outstanding)},n.prototype.resend=function(t){t.sendOperation(t.revision,this.outstanding)},t.AwaitingWithBuffer=o,o.prototype.applyClient=function(t,e){var n=this.buffer.compose(e);return new o(this.outstanding,n)},o.prototype.applyServer=function(t,e){var n=e.constructor.transform,r=n(this.outstanding,e),i=n(this.buffer,r[1]);return t.applyOperation(i[1]),new o(r[0],i[0])},o.prototype.serverAck=function(t){return t.sendOperation(t.revision,this.buffer),new n(this.buffer)},o.prototype.transformSelection=function(t){return t.transform(this.outstanding).transform(this.buffer)},o.prototype.resend=function(t){t.sendOperation(t.revision,this.outstanding)},t}(this),"object"==typeof module&&(module.exports=ot.Client),ot.CodeMirrorAdapter=function(){"use strict";function t(t){this.cm=t,this.ignoreNextChange=!1,this.changeInProgress=!1,this.selectionChanged=!1,s(this,"onChanges"),s(this,"onChange"),s(this,"onCursorActivity"),s(this,"onFocus"),s(this,"onBlur"),t.on("changes",this.onChanges),t.on("change",this.onChange),t.on("cursorActivity",this.onCursorActivity),t.on("focus",this.onFocus),t.on("blur",this.onBlur)}function e(t,e){return t.line<e.line?-1:t.line>e.line?1:t.ch<e.ch?-1:t.ch>e.ch?1:0}function n(t,n){return e(t,n)<=0}function o(t,e){return n(t,e)?t:e}function r(t,e){return n(t,e)?e:t}function i(t){return t.indexFromPos({line:t.lastLine(),ch:0})+t.getLine(t.lastLine()).length}function s(t,e){var n=t[e];t[e]=function(){n.apply(t,arguments)}}var a=ot.TextOperation,c=ot.Selection;t.prototype.detach=function(){this.cm.off("changes",this.onChanges),this.cm.off("change",this.onChange),this.cm.off("cursorActivity",this.onCursorActivity),this.cm.off("focus",this.onFocus),this.cm.off("blur",this.onBlur)},t.operationFromCodeMirrorChanges=function(t,e){function o(t){return t[t.length-1]}function r(t){if(0===t.length)return 0;for(var e=0,n=0;n<t.length;n++)e+=t[n].length;return e+t.length-1}function s(t,e){return function(i){return n(i,e.from)?t(i):n(e.to,i)?t({line:i.line+e.text.length-1-(e.to.line-e.from.line),ch:e.to.line<i.line?i.ch:e.text.length<=1?i.ch-(e.to.ch-e.from.ch)+r(e.text):i.ch-e.to.ch+o(e.text).length})+r(e.removed)-r(e.text):e.from.line===i.line?t(e.from)+i.ch-e.from.ch:t(e.from)+r(e.removed.slice(0,i.line-e.from.line))+1+i.ch}}for(var c=i(e),p=(new a).retain(c),h=(new a).retain(c),u=function(t){return e.indexFromPos(t)},l=t.length-1;l>=0;l--){var f=t[l];u=s(u,f);var d=u(f.from),g=c-d-r(f.text);p=(new a).retain(d)["delete"](r(f.removed)).insert(f.text.join("\n")).retain(g).compose(p),h=h.compose((new a).retain(d)["delete"](r(f.text)).insert(f.removed.join("\n")).retain(g)),c+=r(f.removed)-r(f.text)}return[p,h]},t.operationFromCodeMirrorChange=t.operationFromCodeMirrorChanges,t.applyOperationToCodeMirror=function(t,e){e.operation(function(){for(var n=t.ops,o=0,r=0,i=n.length;i>r;r++){var s=n[r];if(a.isRetain(s))o+=s;else if(a.isInsert(s))e.replaceRange(s,e.posFromIndex(o)),o+=s.length;else if(a.isDelete(s)){var c=e.posFromIndex(o),p=e.posFromIndex(o-s);e.replaceRange("",c,p)}}})},t.prototype.registerCallbacks=function(t){this.callbacks=t},t.prototype.onChange=function(){this.changeInProgress=!0},t.prototype.onChanges=function(e,n){if(!this.ignoreNextChange){var o=t.operationFromCodeMirrorChanges(n,this.cm);this.trigger("change",o[0],o[1])}this.selectionChanged&&this.trigger("selectionChange"),this.changeInProgress=!1,this.ignoreNextChange=!1},t.prototype.onCursorActivity=t.prototype.onFocus=function(){this.changeInProgress?this.selectionChanged=!0:this.trigger("selectionChange")},t.prototype.onBlur=function(){this.cm.somethingSelected()||this.trigger("blur")},t.prototype.getValue=function(){return this.cm.getValue()},t.prototype.getSelection=function(){for(var t=this.cm,e=t.listSelections(),n=[],o=0;o<e.length;o++)n[o]=new c.Range(t.indexFromPos(e[o].anchor),t.indexFromPos(e[o].head));return new c(n)},t.prototype.setSelection=function(t){for(var e=[],n=0;n<t.ranges.length;n++){var o=t.ranges[n];e[n]={anchor:this.cm.posFromIndex(o.anchor),head:this.cm.posFromIndex(o.head)}}this.cm.setSelections(e)};var p=function(){var t={},e=document.createElement("style");document.documentElement.getElementsByTagName("head")[0].appendChild(e);var n=e.sheet;return function(e){t[e]||(t[e]=!0,n.insertRule(e,(n.cssRules||n.rules).length))}}();return t.prototype.setOtherCursor=function(t,e,n){var o=this.cm.posFromIndex(t),r=this.cm.cursorCoords(o),i=document.createElement("span");return i.className="other-client",i.style.display="inline-block",i.style.padding="0",i.style.marginLeft=i.style.marginRight="-1px",i.style.borderLeftWidth="2px",i.style.borderLeftStyle="solid",i.style.borderLeftColor=e,i.style.height=.9*(r.bottom-r.top)+"px",i.style.zIndex=0,i.setAttribute("data-clientid",n),this.cm.setBookmark(o,{widget:i,insertLeft:!0})},t.prototype.setOtherSelectionRange=function(t,e){var n=/^#([0-9a-fA-F]{6})$/.exec(e);if(!n)throw new Error("only six-digit hex colors are allowed.");var i="selection-"+n[1],s="."+i+" { background: "+e+"; }";p(s);var a=this.cm.posFromIndex(t.anchor),c=this.cm.posFromIndex(t.head);return this.cm.markText(o(a,c),r(a,c),{className:i})},t.prototype.setOtherSelection=function(t,e,n){for(var o=[],r=0;r<t.ranges.length;r++){var i=t.ranges[r];o[r]=i.isEmpty()?this.setOtherCursor(i.head,e,n):this.setOtherSelectionRange(i,e,n)}return{clear:function(){for(var t=0;t<o.length;t++)o[t].clear()}}},t.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),n=this.callbacks&&this.callbacks[t];n&&n.apply(this,e)},t.prototype.applyOperation=function(e){e.isNoop()||(this.ignoreNextChange=!0),t.applyOperationToCodeMirror(e,this.cm)},t.prototype.registerUndo=function(t){this.cm.undo=t},t.prototype.registerRedo=function(t){this.cm.redo=t},t}(this),ot.SocketIOAdapter=function(){"use strict";function t(t){this.socket=t;var e=this;t.on("client_left",function(t){e.trigger("client_left",t)}).on("set_name",function(t,n){e.trigger("set_name",t,n)}).on("ack",function(){e.trigger("ack")}).on("operation",function(t,n,o){e.trigger("operation",n),e.trigger("selection",t,o)}).on("selection",function(t,n){e.trigger("selection",t,n)}).on("reconnect",function(){e.trigger("reconnect")})}return t.prototype.sendOperation=function(t,e,n){this.socket.emit("operation",t,e,n)},t.prototype.sendSelection=function(t){this.socket.emit("selection",t)},t.prototype.registerCallbacks=function(t){this.callbacks=t},t.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),n=this.callbacks&&this.callbacks[t];n&&n.apply(this,e)},t}(),ot.AjaxAdapter=function(){"use strict";function t(t,e,n){"/"!==t[t.length-1]&&(t+="/"),this.path=t,this.ownUserName=e,this.majorRevision=n.major||0,this.minorRevision=n.minor||0,this.poll()}return t.prototype.renderRevisionPath=function(){return"revision/"+this.majorRevision+"-"+this.minorRevision},t.prototype.handleResponse=function(t){var e,n=t.operations;for(e=0;e<n.length;e++)n[e].user===this.ownUserName?this.trigger("ack"):this.trigger("operation",n[e].operation);n.length>0&&(this.majorRevision+=n.length,this.minorRevision=0);var o=t.events;if(o){for(e=0;e<o.length;e++){var r=o[e].user;if(r!==this.ownUserName)switch(o[e].event){case"joined":this.trigger("set_name",r,r);break;case"left":this.trigger("client_left",r);break;case"selection":this.trigger("selection",r,o[e].selection)}}this.minorRevision+=o.length}var i=t.users;i&&(delete i[this.ownUserName],this.trigger("clients",i)),t.revision&&(this.majorRevision=t.revision.major,this.minorRevision=t.revision.minor)},t.prototype.poll=function(){var t=this;$.ajax({url:this.path+this.renderRevisionPath(),type:"GET",dataType:"json",timeout:5e3,success:function(e){t.handleResponse(e),t.poll()},error:function(){setTimeout(function(){t.poll()},500)}})},t.prototype.sendOperation=function(t,e,n){if(t!==this.majorRevision)throw new Error("Revision numbers out of sync");var o=this;$.ajax({url:this.path+this.renderRevisionPath(),type:"POST",data:JSON.stringify({operation:e,selection:n}),contentType:"application/json",processData:!1,success:function(){},error:function(){setTimeout(function(){o.sendOperation(t,e,n)},500)}})},t.prototype.sendSelection=function(t){$.ajax({url:this.path+this.renderRevisionPath()+"/selection",type:"POST",data:JSON.stringify(t),contentType:"application/json",processData:!1,timeout:1e3})},t.prototype.registerCallbacks=function(t){this.callbacks=t},t.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),n=this.callbacks&&this.callbacks[t];n&&n.apply(this,e)},t}(),ot.EditorClient=function(){"use strict";function t(t,e){this.selectionBefore=t,this.selectionAfter=e}function e(t,e){this.clientId=t,this.selection=e}function n(t,e,n,o,r){this.id=t,this.listEl=e,this.editorAdapter=n,this.name=o,this.li=document.createElement("li"),o&&(this.li.textContent=o,this.listEl.appendChild(this.li)),this.setColor(o?s(o):Math.random()),r&&this.updateSelection(r)}function o(t,e,n,o){h.call(this,t),this.serverAdapter=n,this.editorAdapter=o,this.undoManager=new l,this.initializeClientList(),this.initializeClients(e);var r=this;this.editorAdapter.registerCallbacks({change:function(t,e){r.onChange(t,e)},selectionChange:function(){r.onSelectionChange()},blur:function(){r.onBlur()}}),this.editorAdapter.registerUndo(function(){r.undo()}),this.editorAdapter.registerRedo(function(){r.redo()}),this.serverAdapter.registerCallbacks({client_left:function(t){r.onClientLeft(t)},set_name:function(t,e){r.getClientObject(t).setName(e)},ack:function(){r.serverAck()},operation:function(t){r.applyServer(f.fromJSON(t))},selection:function(t,e){e?r.getClientObject(t).updateSelection(r.transformSelection(u.fromJSON(e))):r.getClientObject(t).removeSelection()},clients:function(t){var e;for(e in r.clients)r.clients.hasOwnProperty(e)&&!t.hasOwnProperty(e)&&r.onClientLeft(e);for(e in t)if(t.hasOwnProperty(e)){var n=r.getClientObject(e);t[e].name&&n.setName(t[e].name);var o=t[e].selection;o?r.clients[e].updateSelection(r.transformSelection(u.fromJSON(o))):r.clients[e].removeSelection()}},reconnect:function(){r.serverReconnect()}})}function r(t,e,n){function o(t){var e=Math.round(255*t).toString(16);return 1===e.length?"0"+e:e}return"#"+o(t)+o(e)+o(n)}function i(t,e,n){if(0===e)return r(n,n,n);var o=.5>n?n*(1+e):n+e-e*n,i=2*n-o,s=function(t){return 0>t&&(t+=1),t>1&&(t-=1),1>6*t?i+6*(o-i)*t:1>2*t?o:2>3*t?i+6*(o-i)*(2/3-t):i};return r(s(t+1/3),s(t),s(t-1/3))}function s(t){for(var e=1,n=0;n<t.length;n++)e=17*(e+t.charCodeAt(n))%360;return e/360}function a(t,e){function n(){}n.prototype=e.prototype,t.prototype=new n,t.prototype.constructor=t}function c(t){return t[t.length-1]}function p(t){t.parentNode&&t.parentNode.removeChild(t)}var h=ot.Client,u=ot.Selection,l=ot.UndoManager,f=ot.TextOperation,d=ot.WrappedOperation;return t.prototype.invert=function(){return new t(this.selectionAfter,this.selectionBefore)},t.prototype.compose=function(e){return new t(this.selectionBefore,e.selectionAfter)},t.prototype.transform=function(e){return new t(this.selectionBefore.transform(e),this.selectionAfter.transform(e))},e.fromJSON=function(t){return new e(t.clientId,t.selection&&u.fromJSON(t.selection))},e.prototype.transform=function(t){return new e(this.clientId,this.selection&&this.selection.transform(t))},n.prototype.setColor=function(t){this.hue=t,this.color=i(t,.75,.5),this.lightColor=i(t,.5,.9),this.li&&(this.li.style.color=this.color)},n.prototype.setName=function(t){this.name!==t&&(this.name=t,this.li.textContent=t,this.li.parentNode||this.listEl.appendChild(this.li),this.setColor(s(t)))},n.prototype.updateSelection=function(t){this.removeSelection(),this.selection=t,this.mark=this.editorAdapter.setOtherSelection(t,t.position===t.selectionEnd?this.color:this.lightColor,this.id)},n.prototype.remove=function(){this.li&&p(this.li),this.removeSelection()},n.prototype.removeSelection=function(){this.mark&&(this.mark.clear(),this.mark=null)},a(o,h),o.prototype.addClient=function(t,e){this.clients[t]=new n(t,this.clientListEl,this.editorAdapter,e.name||t,e.selection?u.fromJSON(e.selection):null)},o.prototype.initializeClients=function(t){this.clients={};for(var e in t)t.hasOwnProperty(e)&&this.addClient(e,t[e])},o.prototype.getClientObject=function(t){var e=this.clients[t];return e?e:this.clients[t]=new n(t,this.clientListEl,this.editorAdapter)},o.prototype.onClientLeft=function(t){console.log("User disconnected: "+t);var e=this.clients[t];e&&(e.remove(),delete this.clients[t])},o.prototype.initializeClientList=function(){this.clientListEl=document.createElement("ul")},o.prototype.applyUnredo=function(t){this.undoManager.add(t.invert(this.editorAdapter.getValue())),this.editorAdapter.applyOperation(t.wrapped),this.selection=t.meta.selectionAfter,this.editorAdapter.setSelection(this.selection),this.applyClient(t.wrapped)},o.prototype.undo=function(){var t=this;this.undoManager.canUndo()&&this.undoManager.performUndo(function(e){t.applyUnredo(e)})},o.prototype.redo=function(){var t=this;this.undoManager.canRedo()&&this.undoManager.performRedo(function(e){t.applyUnredo(e)})},o.prototype.onChange=function(e,n){var o=this.selection;this.updateSelection();var r=new t(o,this.selection),i=(new d(e,r),this.undoManager.undoStack.length>0&&n.shouldBeComposedWithInverted(c(this.undoManager.undoStack).wrapped)),s=new t(this.selection,o);this.undoManager.add(new d(n,s),i),this.applyClient(e)},o.prototype.updateSelection=function(){this.selection=this.editorAdapter.getSelection()},o.prototype.onSelectionChange=function(){var t=this.selection;this.updateSelection(),t&&this.selection.equals(t)||this.sendSelection(this.selection)},o.prototype.onBlur=function(){this.selection=null,this.sendSelection(null)},o.prototype.sendSelection=function(t){this.state instanceof h.AwaitingWithBuffer||this.serverAdapter.sendSelection(t)},o.prototype.sendOperation=function(t,e){this.serverAdapter.sendOperation(t,e.toJSON(),this.selection)},o.prototype.applyOperation=function(t){this.editorAdapter.applyOperation(t),this.updateSelection(),this.undoManager.transform(new d(t,null))},o}(),"undefined"==typeof ot)var ot={};ot.Server=function(){"use strict";function t(t,e){this.document=t,this.operations=e||[]}return t.prototype.receiveOperation=function(t,e){if(0>t||this.operations.length<t)throw new Error("operation revision not in history");for(var n=this.operations.slice(t),o=e.constructor.transform,r=0;r<n.length;r++)e=o(e,n[r])[0];return this.document=e.apply(this.document),this.operations.push(e),e},t}(this),"object"==typeof module&&(module.exports=ot.Server);var opTrans;opTrans="undefined"==typeof ot&&"undefined"!=typeof require?require("ot"):ot;var OpenTokAdapter=function(){"use strict";function t(t,e,n,o){if(OT.$.eventing(this),this.registerCallbacks=this.on,this.session=t,o&&e>o.length){var r=[];r[e-o.length-1]=null,this.operations=r.concat(o)}else this.operations=o?o:[];var i=new opTrans.Server(n,this.operations);this.session.on({connectionDestroyed:function(t){this.trigger("client_left",t.connection.connectionId)},connectionCreated:function(t){t.connection.data&&t.connection.data.name&&this.trigger("set_name",t.connection.connectionId,t.connection.data.name)},"signal:opentok-editor-operation":function(e){var n,o=JSON.parse(e.data);n=new opTrans.WrappedOperation(opTrans.TextOperation.fromJSON(o.operation),o.selection&&opTrans.Selection.fromJSON(o.selection));var r=i.receiveOperation(o.revision,n);console.log("new operation: "+n),e.from.connectionId===t.connection.connectionId?this.trigger("ack"):(this.trigger("operation",r.wrapped.toJSON()),this.trigger("selection",e.from.connectionId,r.meta))},"signal:opentok-editor-selection":function(t){var e=JSON.parse(t.data);this.trigger("selection",t.from.connectionId,e)}},this)}return t.prototype.sendOperation=function(t,e,n){this.session.signal({type:"opentok-editor-operation",data:JSON.stringify({revision:t,operation:e,selection:n})},function(t){t&&console.error("Error sending operation",t)})},t.prototype.sendSelection=function(t){this.session.signal({type:"opentok-editor-selection",data:JSON.stringify(t)})},t}();"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=OpenTokAdapter);var ng,opTrans;ng="undefined"==typeof angular&&"undefined"!=typeof require?require("angular"):angular,"undefined"==typeof ot&&"undefined"!=typeof require&&(window.ot=require("ot"),ot.UndoManager=require("ot/lib/undo-manager.js"),ot.WrappedOperation=require("ot/lib/wrapped-operation.js"),require("ot/lib/codemirror-adapter.js"),require("ot/lib/editor-client.js"),window.OpenTokAdapter=require("./opentok-adapter.js")),"undefined"==typeof window.CodeMirror&&(window.CodeMirror=require("codemirror")),function(){var t=function(t){return t.map(function(t){return{operation:t.wrapped.toJSON()}})},e=function(t){return t.map(function(t){return new ot.WrappedOperation(ot.TextOperation.fromJSON(t.operation),t.cursor&&ot.Selection.fromJSON(t.cursor))})};ng.module("opentok-editor",["opentok"]).directive("otEditor",["OTSession","$window",function(n){return{restrict:"E",scope:{modes:"="},template:'<div class="opentok-editor-mode-select" ng-show="!connecting"><select ng-model="selectedMode" name="modes" ng-options="mode.name for mode in modes"></select></div><div ng-if="connecting" class="opentok-editor-connecting">Connecting...</div><div ng-show="!connecting" class="opentok-editor-connected"><div class="opentok-editor"></div></div>',link:function(o,r,i){var s,a,c,p,h=r.context.querySelector("div.opentok-editor"),u=(r.context.querySelector("select"),!1),l=n.session;"undefined"!=typeof require&&o.modes.forEach(function(t){require("codemirror/mode/"+t.value+"/"+t.value+".js")}),o.connecting=!0;var f=o.modes.filter(function(t){return t.value===i.mode});o.selectedMode=f.length>0?f[0]:o.modes[0];var d=function(t,e,n,r){a||(p=new OpenTokAdapter(l,t,n,r),p.registerCallbacks("operation",function(){o.$emit("otEditorUpdate")}),a=new ot.EditorClient(t,e,p,new ot.CodeMirrorAdapter(s)),o.$apply(function(){o.connecting=!1,setTimeout(function(){s.refresh()},1e3)}))},g=function(){s&&!u&&(u=!0,s.getValue()!==c.str&&(s.setValue(c.str),o.$emit("otEditorUpdate")),d(c.revision,c.clients,c.str,e(c.operations)))},m=function(e){var n=p&&p.operations?t(p.operations):[];n.length>50&&(n=n.slice(n.length-50));var o={type:"opentok-editor-doc",data:JSON.stringify({revision:a.revision,clients:[],str:s.getValue(),operations:n})};e&&(o.to=e),l.signal(o)},y=function(){s=CodeMirror(h,i),l.signal({type:"opentok-editor-request-doc"}),setTimeout(function(){u||(u=!0,d(0,[],s.getValue()),m())},1e4)};l.on({sessionConnected:function(){y()},"signal:opentok-editor-request-doc":function(t){a&&t.from.connectionId!==l.connection.connectionId&&m(t.from)},"signal:opentok-editor-doc":function(t){c=JSON.parse(t.data),g()}}),l.isConnected()&&y(),o.$watch("selectedMode",function(){s&&s.setOption("mode",o.selectedMode.value)}),o.$on("otEditorRefresh",function(){s.refresh()})}}}])}();