"use strict";var forestPadDebug=!0,app=angular.module("forestPad",["ngRoute","ngAnimate","ngTouch","forestPad.filters","forestPad.userServices","forestPad.documentServices","forestPad.forestServices","forestPad.nodeActionMenuServices","forestPad.directives","forestPad.controllers","ui.bootstrap","ui.ace","http-auth-interceptor","scope.safeApply","timer","uploader"]);app.config(["$routeProvider",function(n){n.when("/",{templateUrl:"content/views/home.html",controller:"HomeController"}).when("/login",{templateUrl:"content/views/login.html",controller:"LoginController"}).when("/register",{templateUrl:"content/views/register.html",controller:"RegisterController"}).when("/document/search/:q",{templateUrl:"content/views/document-search.html",controller:"DocumentSearchController"}).when("/document/create",{templateUrl:"content/views/document-create.html",controller:"DocumentCreateController"}).when("/document/delete/:id",{templateUrl:"content/views/document-delete.html",controller:"DocumentDeleteController"}).when("/editor/document/:id",{templateUrl:"content/views/editor.html",controller:"EditorController"}).when("/editor",{templateUrl:"content/views/editor-empty.html",controller:"EditorEmptyController"}).when("/import",{templateUrl:"content/views/document-import.html",controller:"ImportController"}).otherwise({redirectTo:"/"})}]),app.run(["$rootScope","$log",function(n,t){n.$log=t}]),app.config(["$provide",function(n){n.decorator("$log",["$delegate","$sniffer",function(n){var i=n.log;return n.info=function(n){forestPadDebug&&toastr.info(n,"Log")},n.warn=function(n){forestPadDebug&&toastr.warn(n,"Warn")},n.error=function(n){forestPadDebug&&toastr.error(n,"Error")},n.userInfo=function(n){toastr.info(n,"User info message")},n.userError=function(n){toastr.error(n,"User error message")},n}])}]),toastr.options={debug:!1,positionClass:"toast-bottom-right",newestOnTop:!1,fadeIn:300,fadeOut:1e3,timeOut:5e3,extendedTimeOut:8e3},toastr.subscribe(function(){}),angular.module("forestPad.controllers",[]).controller("ForestPadController",["$rootScope","$scope","$log","$location","$routeParams","userService","documentService","forestService",function(n,t,i,r,u,f,e,o){t.$watch("location.path()",new function(){}),t.user=f.getUser(),t.redirectToEditor=function(){f.redirectToEditor()},t.signOut=function(){e.unloadDocuments(),o.unloadForest(),f.signOut()},t.search=function(){var n=encodeURIComponent($("#searchInput").val());$("#searchInput").val(""),r.path("/document/search/"+n)},t.focusOnSearch=function(){i.info("focus on search"),$("#searchInput").focus().select()},t.$on("event:load-forest",function(){}),t.$on("event:timer-keep-alive",function(){i.info("Stayin' alive...")}),t.$on("event:timer-save",function(){if(t.user.loggedIn){var i=o.getForest().Name;i!=undefined&&n.$broadcast("event:timer-save-indicator")}}),t.$on("event:timer-housekeeping",function(){t.currentDocument=o.getForest().Name})}]).controller("LoginController",["$scope","$http","$log","$location","$timeout","authService","userService",function(n,t,i,r,u,f,e){n.shouldBeOpen=!0,n.opts={backdropFade:!0,dialogFade:!0,keyboard:!1,backdropClick:!1},n.usernameFocusLost=function(){i("username focus lost")},n.submit=function(){n.showSpinner=!0,e.signIn(n.username,n.password).then(function(){i.info("sign in succeeded"),n.showSpinner=!1,n.user=e.getUser(),r.path("/editor")},function(){i.info("sign in failed"),n.showSpinner=!1,n.user=e.getUser(),n.user.username===undefined&&(n.error="Login failed",u(function(){n.error=""},3500),n.password="")})}}]).controller("RegisterController",["$scope","$log",function(){}]).controller("DocumentSearchController",["$scope","$log","$location","$routeParams","documentService",function(n,t,i,r){n.shouldBeOpen=!0,n.q=decodeURIComponent(r.q),n.opts={backdropFade:!0,dialogFade:!0,keyboard:!1,backdropClick:!1},n.cancelSearchDocument=function(){history.back()},n.submit=function(){}}]).controller("DocumentCreateController",["$scope","$log","$location","documentService",function(n,t,i,r){n.shouldBeOpen=!0,n.opts={backdropFade:!0,dialogFade:!0,keyboard:!1,backdropClick:!1},n.cancelCreateDocument=function(){history.back()},n.submit=function(){n.createDocumentForm.$valid&&(n.showSpinner=!0,r.createDocument(n.name,n.description).then(function(){var t=r.getCurrentDocument();i.path("/editor/document/"+t.ForestPadDocumentId),n.showSpinner=!1},function(){n.showSpinner=!1}))}}]).controller("DocumentDeleteController",["$scope","$log","$routeParams","$location","documentService",function(n,t,i,r,u){n.shouldBeOpen=!0,n.opts={backdropFade:!0,dialogFade:!0,keyboard:!1,backdropClick:!1},n.cancelDeleteDocument=function(){history.back()},n.submit=function(){n.deleteDocumentForm.$valid&&(n.showSpinner=!0,u.deleteDocument(i.id).then(function(){n.showSpinner=!1,r.path("/editor")},function(){n.showSpinner=!1}))}}]).controller("ImportController",["$scope","$log","documentService",function(n){n.shouldBeOpen=!0,n.opts={backdropFade:!0,dialogFade:!0,keyboard:!1,backdropClick:!1},n.cancelImportDocument=function(){history.back()}}]).controller("HomeController",["$scope","$log",function(){}]).controller("EditorController",["$scope","$rootScope","$log","$routeParams","$location","documentService","forestService","nodeActionMenuService",function(n,t,i,r,u,f,e,o){t.$broadcast("loadDocuments"),n.currentDocumentId=r.id,n.saveCurrentDocument=function(){},n.toggleNodeActionMenu=function(t,i){n.nodeActionMenuCss=o.init(t,i),n.nodeActionMenuVisible=!0},n.newForest=function(){e.newNode(r.id,null,"new forest - "+ +new Date,"new forest",0)}}]).controller("EditorEmptyController",["$scope","$rootScope","$log",function(n,t){t.$broadcast("loadDocuments")}]).controller("DocumentListController",["$scope","$rootScope","$http","$log","documentService",function(n,t,i,r,u){n.$on("loadDocuments",function(){u.loadDocuments().then(function(){n.documents=u.getDocuments()})})}]).controller("ForestController",["$scope","$rootScope","$http","$log","$routeParams","$timeout","forestService","nodeActionMenuService",function(n,t,i,r,u,f,e){e.loadForest(u.id).then(function(){n.forest=e.getForest(),t.$broadcast("event:load-forest")}),n.nodeExpand=function(n){e.expandNode(n)},n.newNode=function(n){e.newNode(u.id,n,"new leaf - "+ +new Date,"new leaf",0)},n.nodeGetText=function(n,i){i.stopPropagation(),t.$broadcast("loadText",n)}}]).controller("TextController",["$scope","$http","$log",function(n){n.$on("loadText",function(t,i){n.node=i}),n.nameChange=function(){},n.textChange=function(){},n.nameChangeOnBlur=function(){},n.textChangeOnBlur=function(){}}]),angular.module("forestPad.directives",[]).directive("auth",["$log","$location",function(n,t){return{restrict:"A",link:function(n,i){i.removeClass("waiting-for-angular-auth");var f=i.find("#loginDialog"),e=i.find("#forestPadSection"),u="";f.hide(),n.$on("event:auth-loginRequired",function(){u=t.path(),t.path("/login")}),n.$on("event:auth-loginConfirmed",function(){u==""?t.path("/editor"):t.path(u)})}}}]).directive("appVersion",["version",function(n){return function(t,i){i.text(n)}}]).directive("captureEnterKey",[function(){return function(n,t,i){t.bind("keydown keypress",function(t){t.which===13&&(n.$apply(function(){n.$eval(i.captureEnterKey)}),t.preventDefault())})}}]).directive("captureCtrlKKey",[function(){return function(n,t,i){t.bind("keydown keypress",function(t){t.ctrlKey&&t.which===75&&(n.$apply(function(){n.$eval(i.captureCtrlKKey)}),t.preventDefault())})}}]).directive("nodeEnter",[function(){return function(n,t){t.bind("mouseenter",function(){var i={};t.css(i)}),t.bind("mouseleave",function(){var i={};t.css(i)})}}]).directive("demandFocus",["$timeout",function(n){return{link:function(t,i,r){t.$watch(r.demandFocus,function(t){angular.isDefined(t)&&t&&n(function(){i[0].focus()})},!0)}}}]).directive("forestViewDetailChevron",[function(){return function(n,t){t.bind("mouseenter",function(){var i={"-webkit-box-shadow":"0px 0px 3px 5px #f2e1f2","-moz-box-shadow":"0px 0px 3px 5px #f2e1f2","box-shadow":"0px 0px 3px 5px #f2e1f2"};t.css(i)}),t.bind("mouseleave",function(){var i={"-webkit-box-shadow":"none","-moz-box-shadow":"none","box-shadow":"none"};t.css(i)})}}]).directive("nodeActionMenu",["$log","nodeActionMenuService",function(n,t){return{restrict:"A",template:'<div class="nodeActionMenu"><\/div>',controller:["$scope","$log",function(n){n.onClick=function(n){t.triggerAction(n.item.data.action)}}],link:function(n,i){var u=100,f=0,s=.81,e=1,h=d3.scale.category20c(),o=t.getMenuItems(),c=function(t){var r=d3.select(t[0]).data([o]).append("svg:svg").attr("width",200).attr("height",200).attr("class","nodeActionMenuD3").append("g").attr("transform","translate("+u+","+u+")").on("mouseover",function(){d3.select(this).transition().duration(250).style("opacity",e)}).on("mouseout",function(){d3.select(this).transition().duration(800).style("opacity",f).each("end",function(){n.nodeActionMenuVisible=!1,n.$apply()})}),i=d3.svg.arc().outerRadius(u-10),l=d3.svg.arc().outerRadius(u),a=d3.layout.pie().value(function(n){return n.pieslice}),c=r.selectAll("g.slice").data(a).enter().append("svg:g").attr("class","slice").on("mouseover",function(){d3.select(this).select("path").transition().duration(200).attr("d",l).style("opacity",e)}).on("mouseout",function(){d3.select(this).select("path").transition().duration(200).attr("d",i).transition().style("opacity",s)}).on("click",function(t){r.transition().duration(800).style("opacity",f).each("end",function(){n.nodeActionMenuVisible=!1,n.$apply()});n.onClick({item:t})});c.append("svg:path").attr("fill",function(n,t){return h(t)}).attr("d",i),c.append("svg:text").attr("transform",function(n){return n.innerRadius=0,n.outerRadius=u,"translate("+i.centroid(n)+")"}).attr("text-anchor","middle").text(function(n,t){return o[t].action})};c(i)}}}]).directive("saveLed",["$log","$timeout",function(n,t){return{restrict:"A",link:function(n,i){i.addClass("saveLed"),n.$on("event:timer-save-indicator",function(){i.addClass("active"),i.removeClass("inactive"),t(function(){i.removeClass("active"),i.addClass("inactive")},800)})}}}]),angular.module("forestPad.filters",[]).filter("makeIdUnbreakable",[function(){return function(n){return String(n).replace(/\-/g,"")}}]).filter("interpolate",["version",function(n){return function(t){return String(t).replace(/\%VERSION\%/mg,n)}}]).filter("reverse",[function(){return function(n){return n.split("").reverse().join("")}}]).filter("truncate",[function(){return function(n,t,i){if(n!==undefined)return isNaN(t)&&(t=20),i===undefined&&(i="..."),n.length<=t||n.length-i.length<=t?n:String(n).substring(0,t-i.length)+i}}]),angular.module("forestPad.userServices",[]).value("version","0.1").factory("userService",["$http","$log","$location","authService",function(n,t,i,r){var u=[],f;return u.username=undefined,u.loggedIn=!1,u.firstname=undefined,u.lastname=undefined,f=function(e){n({method:"GET",url:"account/sessionactive",ignoreAuthModule:!0}).success(function(n){n.LoggedIn&&(t.info(n.Username+" is logged in"),u.username=n.Username,u.loggedIn=n.LoggedIn,u.firstname=n.FirstName,u.lastname=n.LastName,r.loginConfirmed())}).error(function(){e&&i.path("/login")})},f(!1),{keepAlive:function(){f(!0)},redirectToEditor:function(){f(!0),i.path("/editor")},signIn:function(t,i){return n({method:"POST",url:"account/login",data:"username="+t+"&password="+i,headers:{"Content-Type":"application/x-www-form-urlencoded"}}).success(function(n){u.username=n.Username,u.loggedIn=!0,u.firstname=n.FirstName,u.lastname=n.LastName,r.loginConfirmed()}).error(function(){u.username=undefined,u.loggedIn=!1,u.firstname=undefined,u.lastname=undefined})},signOut:function(){n({method:"POST",url:"account/logout"}).success(function(){u.username=undefined,u.loggedIn=!1,u.firstname=undefined,u.lastname=undefined,i.path("/")})},getUser:function(){return u}}}]),angular.module("forestPad.documentServices",[]).value("version","0.1").factory("documentService",["$http","$log","$location",function(n){var r=[],u=[];return{getCurrentDocument:function(){return u},loadDocuments:function(){return NProgress.start(),n({method:"GET",url:"documents"}).success(function(n){r=n,NProgress.done()})},unloadDocuments:function(){r=[]},getDocuments:function(){return r},createDocument:function(t,i){return NProgress.start(),n({method:"POST",url:"document/create",data:"name="+t+"&description="+i,headers:{"Content-Type":"application/x-www-form-urlencoded"}}).success(function(n){u=n,NProgress.done()})},deleteDocument:function(t){return NProgress.start(),n({method:"POST",url:"document/"+t+"/delete"}).success(function(){NProgress.done()})}}}]),angular.module("forestPad.forestServices",[]).value("version","0.1").factory("forestService",["$http","$log","$location",function(n,t){var r=[],f=function(t){t.NodeCount>0?(NProgress.start(),n({method:"GET",url:"document/"+t.ForestPadDocumentId+"/node/"+t.ForestPadNodeId}).success(function(n){t.Nodes=n.Nodes,NProgress.done()})):t.Nodes=[]},e=function(t,i){NProgress.start(),n({method:"GET",url:"document/"+i.ForestPadDocumentId+"/node/"+t.ForestPadNodeId}).success(function(n){t.Nodes=n.Nodes,NProgress.done()})},u=function(n,i,r){for(var f=0;f<=r.length-1;f++){if(n==r[f].ForestPadNodeId){t.info("found "+n+" of parent "+i.ForestPadNodeId);break}r[f].Nodes!=null&&r[f].Nodes.length>0&&u(n,r[f],r[f].Nodes)}return i};return{loadForest:function(t){return NProgress.start(),n({method:"GET",url:"document/"+t}).success(function(n){r=n,NProgress.done()})},unloadForest:function(){r=[]},getForest:function(){return r},expandNode:function(n){f(n)},newNode:function(t,i,u,f,o){var s;s=i==null?"create":i.ForestPadNodeId+"/create",NProgress.start(),n({method:"POST",url:"document/"+t+"/node/"+s,data:"name="+u+"&text="+f+"&rank="+o,headers:{"Content-Type":"application/x-www-form-urlencoded"}}).success(function(n){i==null?r.Nodes.unshift(n):(i.Nodes==null?e(i,n):i.Nodes.unshift(n),i.NodeCount++),NProgress.done()})},deleteNode:function(i,f){NProgress.start(),n({method:"POST",url:"document/"+i+"/node/"+f.ForestPadNodeId+"/delete",headers:{"Content-Type":"application/x-www-form-urlencoded"}}).success(function(){var h,s;f.ForestPadNodeParentId==null?(s=r.Nodes.indexOf(f),t.info("parent is null - index of node to delete "+s),r.Nodes.splice(s,1)):(h=u(f.ForestPadNodeId,r,r.Nodes),s=h.indexOf(f),t.info("parent is "+h.ForestPadNodeId+" - index of node to delete "+s),h.Nodes.splice(s,1)),NProgress.done()}).error(function(n,i){t.error("delete node failure "+i)})}}}]),angular.module("forestPad.nodeActionMenuServices",[]).value("version","0.1").factory("nodeActionMenuService",["$http","$log","$location","$routeParams","forestService",function(n,t,i,r,u){var f=null,e=[{action:"New",pieslice:30},{action:"Move",pieslice:20},{action:"Delete",pieslice:20},{action:"Settings",pieslice:30}];return{init:function(n,t){f=n;var i=200,r=200;return{left:t.pageX-i/2,top:t.pageY-r/2,width:i+"px",height:r+"px"}},triggerAction:function(n){if(f!=null){t.info("trigger action [ "+n+" ] on node "+angular.toJson(f));switch(n){case"New":u.newNode(r.id,f,"new leaf - "+ +new Date,"new leaf",0);break;case"Move":t.info("Moving");break;case"Delete":t.info("Deleting"),u.deleteNode(r.id,f);break;case"Settings":t.info("Settingsing")}}},getMenuItems:function(){return e},setMenuItems:function(n){this.menuItems=n}}}])