diff --git a/dist/angular-form-builder.js b/dist/angular-form-builder.js index be7fbd4..7a3adf1 100644 --- a/dist/angular-form-builder.js +++ b/dist/angular-form-builder.js @@ -169,6 +169,8 @@ }).call(this); (function() { + var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + angular.module('builder.directive', ['builder.provider', 'builder.controller', 'builder.drag', 'validator']).directive('fbBuilder', [ '$injector', function($injector) { var $builder, $drag; @@ -485,6 +487,7 @@ restrict: 'A', controller: 'fbFormObjectController', link: function(scope, element, attrs) { + var index, _i, _ref; scope.formObject = $parse(attrs.fbFormObject)(scope); scope.$component = $builder.components[scope.formObject.component]; scope.$on($builder.broadcastChannel.updateInput, function() { @@ -503,7 +506,7 @@ checked.push((_ref = scope.options[index]) != null ? _ref : scope.inputArray[index]); } } - return scope.inputText = checked.join(', '); + return scope.inputText = checked; }, true); } scope.$watch('inputText', function() { @@ -528,14 +531,27 @@ if (!scope.$component.arrayToText && scope.formObject.options.length > 0) { scope.inputText = scope.formObject.options[0]; } - return scope.$watch("default['" + scope.formObject.id + "']", function(value) { + for (index = _i = 0, _ref = scope["default"].length; _i < _ref; index = _i += 1) { + if (scope["default"][index].id === scope.formObject.id) { + break; + } + } + return scope.$watch("default[" + index + "]", function(value) { + var i, _j, _ref1, _ref2; if (!value) { return; } if (scope.$component.arrayToText) { - return scope.inputArray = value; + for (i = _j = 0, _ref1 = scope.formObject.options.length; _j < _ref1; i = _j += 1) { + if (_ref2 = scope.formObject.options[i], __indexOf.call(value.value, _ref2) >= 0) { + scope.inputArray.push(scope.formObject.options[i]); + } else { + scope.inputArray.push(false); + } + } + return scope.inputArray; } else { - return scope.inputText = value; + return scope.inputText = value.value; } }); } diff --git a/dist/angular-form-builder.min.js b/dist/angular-form-builder.min.js index c9dbad7..2993792 100644 --- a/dist/angular-form-builder.min.js +++ b/dist/angular-form-builder.min.js @@ -1 +1 @@ -(function(){var a;a=function(a,b){var c,d;for(c in a)d=a[c],"$$hashKey"!==c&&(b[c]=d)},angular.module("builder.controller",["builder.provider"]).controller("fbFormObjectEditableController",["$scope","$injector",function(b,c){var d;return d=c.get("$builder"),b.setupScope=function(c){var e;return a(c,b),b.optionsText=c.options.join("\n"),b.$watch("[label, description, placeholder, required, options, validation]",function(){return c.label=b.label,c.description=b.description,c.placeholder=b.placeholder,c.required=b.required,c.options=b.options,c.validation=b.validation},!0),b.$watch("optionsText",function(a){var c;return b.options=function(){var b,d,e,f;for(e=a.split("\n"),f=[],b=0,d=e.length;d>b;b++)c=e[b],c.length>0&&f.push(c);return f}(),b.inputText=b.options[0]}),e=d.components[c.component],b.validationOptions=e.validationOptions},b.data={model:null,backup:function(){return this.model={label:b.label,description:b.description,placeholder:b.placeholder,required:b.required,optionsText:b.optionsText,validation:b.validation}},rollback:function(){return this.model?(b.label=this.model.label,b.description=this.model.description,b.placeholder=this.model.placeholder,b.required=this.model.required,b.optionsText=this.model.optionsText,b.validation=this.model.validation):void 0}}}]).controller("fbComponentsController",["$scope","$injector",function(a,b){var c;return c=b.get("$builder"),a.selectGroup=function(b,d){var e,f,g,h;null!=b&&b.preventDefault(),a.activeGroup=d,a.components=[],g=c.components,h=[];for(f in g)e=g[f],e.group===d&&h.push(a.components.push(e));return h},a.groups=c.groups,a.activeGroup=a.groups[0],a.allComponents=c.components,a.$watch("allComponents",function(){return a.selectGroup(null,a.activeGroup)})}]).controller("fbComponentController",["$scope",function(b){return b.copyObjectToScope=function(c){return a(c,b)}}]).controller("fbFormController",["$scope","$injector",function(a,b){var c,d;return c=b.get("$builder"),d=b.get("$timeout"),null==a.input&&(a.input=[]),a.$watch("form",function(){return a.input.length>a.form.length&&a.input.splice(a.form.length),d(function(){return a.$broadcast(c.broadcastChannel.updateInput)})},!0)}]).controller("fbFormObjectController",["$scope","$injector",function(b,c){var d;return d=c.get("$builder"),b.copyObjectToScope=function(c){return a(c,b)},b.updateInput=function(a){var c;return c={id:b.formObject.id,label:b.formObject.label,value:null!=a?a:""},b.$parent.input.splice(b.$index,1,c)}}])}).call(this),function(){angular.module("builder.directive",["builder.provider","builder.controller","builder.drag","validator"]).directive("fbBuilder",["$injector",function(a){var b,c;return b=a.get("$builder"),c=a.get("$drag"),{restrict:"A",scope:{fbBuilder:"="},template:"
\n
\n
",link:function(a,d,e){var f,g,h;return a.formName=e.fbBuilder,null==(g=b.forms)[h=a.formName]&&(g[h]=[]),a.formObjects=b.forms[a.formName],f=!0,$(d).addClass("fb-builder"),c.droppable($(d),{move:function(a){var b,c,e,g,h,i,j,k,l,m,n;if(f&&($("div.fb-form-object-editable").popover("hide"),f=!1),e=$(d).find(".fb-form-object-editable:not(.empty,.dragging)"),0===e.length)return void(0===$(d).find(".fb-form-object-editable.empty").length&&$(d).find(">div:first").append($("
")));for(j=[],j.push(-1e3),h=k=0,m=e.length;m>k;h=k+=1)c=$(e[h]),i=c.offset(),g=c.height(),j.push(i.top+g/2);for(j.push(j[j.length-1]+1e3),h=l=1,n=j.length;n>l;h=l+=1)if(a.pageY>j[h-1]&&a.pageY<=j[h]){$(d).find(".empty").remove(),b=$("
"),h-1l&&k--,b.updateFormObjectIndex(a.formName,l,k))):(j=i.object.formObject,j.editable&&b.removeFormObject(e.fbBuilder,j.index)),$(d).find(".empty").remove()):void $(d).find(".empty").remove()}})}}}]).directive("fbFormObjectEditable",["$injector",function(a){var b,c,d,e;return b=a.get("$builder"),d=a.get("$drag"),c=a.get("$compile"),e=a.get("$validator"),{restrict:"A",controller:"fbFormObjectEditableController",scope:{formObject:"=fbFormObjectEditable"},link:function(a,f){var g;return a.inputArray=[],a.$component=b.components[a.formObject.component],a.setupScope(a.formObject),a.$watch("$component.template",function(b){var d;if(b)return d=c(b)(a),$(f).html(d)}),$(f).on("click",function(){return!1}),d.draggable($(f),{object:{formObject:a.formObject}}),a.formObject.editable?(g={},a.$watch("$component.popoverTemplate",function(d){return d?($(f).removeClass(g.id),g={id:"fb-"+Math.random().toString().substr(2),isClickedSave:!1,view:null,html:d},g.html=$(g.html).addClass(g.id),g.view=c(g.html)(a),$(f).addClass(g.id),$(f).popover({html:!0,title:a.$component.label,content:g.view,container:"body",placement:b.config.popoverPlacement})):void 0}),a.popover={save:function(b){b.preventDefault(),e.validate(a).success(function(){return g.isClickedSave=!0,$(f).popover("hide")})},remove:function(c){c.preventDefault(),b.removeFormObject(a.$parent.formName,a.$parent.$index),$(f).popover("hide")},shown:function(){return a.data.backup(),g.isClickedSave=!1},cancel:function(b){a.data.rollback(),b&&(b.preventDefault(),$(f).popover("hide"))}},$(f).on("show.bs.popover",function(){var a,b,c;return d.isMouseMoved()?!1:($("div.fb-form-object-editable:not(."+g.id+")").popover("hide"),a=$("form."+g.id).closest(".popover"),a.length>0?(b=$(f).offset().top+$(f).height()/2,c=b-a.height()/2,a.css({position:"absolute",top:c}),a.show(),setTimeout(function(){return a.addClass("in"),$(f).triggerHandler("shown.bs.popover")},0),!1):void 0)}),$(f).on("shown.bs.popover",function(){$(".popover ."+g.id+" input:first").select(),a.$apply(function(){return a.popover.shown()})}),$(f).on("hide.bs.popover",function(){var b;return b=$("form."+g.id).closest(".popover"),g.isClickedSave||(a.$$phase||a.$root.$$phase?a.popover.cancel():a.$apply(function(){return a.popover.cancel()})),b.removeClass("in"),setTimeout(function(){return b.hide()},300),!1})):void 0}}}]).directive("fbComponents",function(){return{restrict:"A",template:'\n
\n
\n
',controller:"fbComponentsController"}}).directive("fbComponent",["$injector",function(a){var b,c,d;return b=a.get("$builder"),d=a.get("$drag"),c=a.get("$compile"),{restrict:"A",scope:{component:"=fbComponent"},controller:"fbComponentController",link:function(a,b){return a.copyObjectToScope(a.component),d.draggable($(b),{mode:"mirror",defer:!1,object:{componentName:a.component.name}}),a.$watch("component.template",function(d){var e;if(d)return e=c(d)(a),$(b).html(e)})}}}]).directive("fbForm",["$injector",function(a){return{restrict:"A",require:"ngModel",scope:{formName:"@fbForm",input:"=ngModel","default":"=fbDefault"},template:'
',controller:"fbFormController",link:function(b){var c,d,e;return c=a.get("$builder"),null==(d=c.forms)[e=b.formName]&&(d[e]=[]),b.form=c.forms[b.formName]}}}]).directive("fbFormObject",["$injector",function(a){var b,c,d;return b=a.get("$builder"),c=a.get("$compile"),d=a.get("$parse"),{restrict:"A",controller:"fbFormObjectController",link:function(a,e,f){return a.formObject=d(f.fbFormObject)(a),a.$component=b.components[a.formObject.component],a.$on(b.broadcastChannel.updateInput,function(){return a.updateInput(a.inputText)}),a.$component.arrayToText&&(a.inputArray=[],a.$watch("inputArray",function(b,c){var d,e,f;if(b!==c){d=[];for(e in a.inputArray)a.inputArray[e]&&d.push(null!=(f=a.options[e])?f:a.inputArray[e]);return a.inputText=d.join(", ")}},!0)),a.$watch("inputText",function(){return a.updateInput(a.inputText)}),a.$watch(f.fbFormObject,function(){return a.copyObjectToScope(a.formObject)},!0),a.$watch("$component.template",function(b){var d,f,g;if(b)return f=$(b),d=f.find("[ng-model='inputText']"),d.attr({validator:"{{validation}}"}),g=c(f)(a),$(e).html(g)}),!a.$component.arrayToText&&a.formObject.options.length>0&&(a.inputText=a.formObject.options[0]),a.$watch("default['"+a.formObject.id+"']",function(b){return b?a.$component.arrayToText?a.inputArray=b:a.inputText=b:void 0})}}}])}.call(this),function(){angular.module("builder.drag",[]).provider("$drag",function(){var a,b,c;a=null,b=null,this.data={draggables:{},droppables:{}},this.mouseMoved=!1,this.isMouseMoved=function(a){return function(){return a.mouseMoved}}(this),this.hooks={down:{},move:{},up:{}},this.eventMouseMove=function(){},this.eventMouseUp=function(){},$(function(a){return function(){return $(document).on("mousedown",function(b){var c,d,e;a.mouseMoved=!1,e=a.hooks.down;for(d in e)(c=e[d])(b)}),$(document).on("mousemove",function(b){var c,d,e;a.mouseMoved=!0,e=a.hooks.move;for(d in e)(c=e[d])(b)}),$(document).on("mouseup",function(b){var c,d,e;e=a.hooks.up;for(d in e)(c=e[d])(b)})}}(this)),this.currentId=0,this.getNewId=function(a){return function(){return""+a.currentId++}}(this),this.setupEasing=function(){return jQuery.extend(jQuery.easing,{easeOutQuad:function(a,b,c,d,e){return-d*(b/=e)*(b-2)+c}})},this.setupProviders=function(c){return a=c,b=a.get("$rootScope")},this.isHover=function(){return function(a,b){var c,d,e,f,g;return d=a.offset(),e=b.offset(),f={width:a.width(),height:a.height()},g={width:b.width(),height:b.height()},c={x:!1,y:!1},c.x=d.left>e.left&&d.lefte.left&&d.left+f.widthe.top&&d.tope.top&&d.top+f.height$(window).innerHeight()-50))return a.autoScroll.up=!1,a.autoScroll.down=!1;if(a.autoScroll.up=!1,a.autoScroll.down=!0,!a.autoScroll.scrolling)return a.autoScroll.scroll()}}}(this),stop:function(a){return function(){return a.autoScroll.up=!1,a.autoScroll.down=!1}}(this)},this.dragMirrorMode=function(a){return function(b,c,d){var e;return null==c&&(c=!0),e={id:a.getNewId(),mode:"mirror",maternal:b[0],element:null,object:d},b.on("mousedown",function(d){var f;return d.preventDefault(),f=b.clone(),e.element=f[0],f.addClass("fb-draggable form-horizontal prepare-dragging"),a.hooks.move.drag=function(c,d){var g,h,i,j;if(!f.hasClass("prepare-dragging")||(f.css({width:b.width(),height:b.height()}),f.removeClass("prepare-dragging"),f.addClass("dragging"),!d)){f.offset({left:c.pageX-f.width()/2,top:c.pageY-f.height()/2}),a.autoScroll.start(c),i=a.data.droppables,j=[];for(h in i)g=i[h],j.push(a.isHover(f,$(g.element))?g.move(c,e):g.out(c,e));return j}},a.hooks.up.drag=function(b){var c,d,g,h;h=a.data.droppables;for(d in h)c=h[d],g=a.isHover(f,$(c.element)),c.up(b,g,e);return delete a.hooks.move.drag,delete a.hooks.up.drag,e.element=null,f.remove(),a.autoScroll.stop()},$("body").append(f),c?void 0:a.hooks.move.drag(d,c)}),e}}(this),this.dragDragMode=function(a){return function(b,c,d){var e;return null==c&&(c=!0),e={id:a.getNewId(),mode:"drag",maternal:null,element:b[0],object:d},b.addClass("fb-draggable"),b.on("mousedown",function(d){return d.preventDefault(),b.hasClass("dragging")?void 0:(b.addClass("prepare-dragging"),a.hooks.move.drag=function(c,d){var f,g,h;if(!b.hasClass("prepare-dragging")||(b.css({width:b.width(),height:b.height()}),b.removeClass("prepare-dragging"),b.addClass("dragging"),!d)){b.offset({left:c.pageX-b.width()/2,top:c.pageY-b.height()/2}),a.autoScroll.start(c),h=a.data.droppables;for(g in h)f=h[g],a.isHover(b,$(f.element))?f.move(c,e):f.out(c,e)}},a.hooks.up.drag=function(c){var d,f,g,h;h=a.data.droppables;for(f in h)d=h[f],g=a.isHover(b,$(d.element)),d.up(c,g,e);return delete a.hooks.move.drag,delete a.hooks.up.drag,b.css({width:"",height:"",left:"",top:""}),b.removeClass("dragging defer-dragging"),a.autoScroll.stop()},c?void 0:a.hooks.move.drag(d,c))}),e}}(this),this.dropMode=function(a){return function(c,d){var e;return e={id:a.getNewId(),element:c[0],move:function(a,c){return b.$apply(function(){return"function"==typeof d.move?d.move(a,c):void 0})},up:function(a,c,e){return b.$apply(function(){return"function"==typeof d.up?d.up(a,c,e):void 0})},out:function(a,c){return b.$apply(function(){return"function"==typeof d.out?d.out(a,c):void 0})}}}}(this),this.draggable=function(a){return function(b,c){var d,e,f,g,h,i,j;if(null==c&&(c={}),f=[],"mirror"===c.mode)for(g=0,i=b.length;i>g;g++)e=b[g],d=a.dragMirrorMode($(e),c.defer,c.object),f.push(d.id),a.data.draggables[d.id]=d;else for(h=0,j=b.length;j>h;h++)e=b[h],d=a.dragDragMode($(e),c.defer,c.object),f.push(d.id),a.data.draggables[d.id]=d;return f}}(this),this.droppable=function(a){return function(b,c){var d,e,f,g,h;for(null==c&&(c={}),f=[],g=0,h=b.length;h>g;g++)e=b[g],d=a.dropMode($(e),c),f.push(d),a.data.droppables[d.id]=d;return f}}(this),this.get=function(a){return this.setupEasing(),this.setupProviders(a),{isMouseMoved:this.isMouseMoved,data:this.data,draggable:this.draggable,droppable:this.droppable}},this.get.$inject=["$injector"],this.$get=this.get})}.call(this),function(){angular.module("builder",["builder.directive"])}.call(this),function(){var a=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};angular.module("builder.provider",[]).provider("$builder",function(){var b,c,d;c=null,b=null,d=null,this.config={popoverPlacement:"right"},this.components={},this.groups=[],this.broadcastChannel={updateInput:"$updateInput"},this.forms={"default":[]},this.convertComponent=function(a,b){var c,d,e,f,g,h,i,j,k,l,m;return c={name:a,group:null!=(d=b.group)?d:"Default",label:null!=(e=b.label)?e:"",description:null!=(f=b.description)?f:"",placeholder:null!=(g=b.placeholder)?g:"",editable:null!=(h=b.editable)?h:!0,required:null!=(i=b.required)?i:!1,validation:null!=(j=b.validation)?j:"/.*/",validationOptions:null!=(k=b.validationOptions)?k:[],options:null!=(l=b.options)?l:[],arrayToText:null!=(m=b.arrayToText)?m:!1,template:b.template,templateUrl:b.templateUrl,popoverTemplate:b.popoverTemplate,popoverTemplateUrl:b.popoverTemplateUrl},c.template||c.templateUrl||console.error("The template is empty."),c.popoverTemplate||c.popoverTemplateUrl||console.error("The popoverTemplate is empty."),c},this.convertFormObject=function(a,b){var c,d,e,f,g,h,i,j,k,l;if(null==b&&(b={}),c=this.components[b.component],null==c)throw"The component "+b.component+" was not registered.";return d={id:b.id,component:b.component,editable:null!=(e=b.editable)?e:c.editable,index:null!=(f=b.index)?f:0,label:null!=(g=b.label)?g:c.label,description:null!=(h=b.description)?h:c.description,placeholder:null!=(i=b.placeholder)?i:c.placeholder,options:null!=(j=b.options)?j:c.options,required:null!=(k=b.required)?k:c.required,validation:null!=(l=b.validation)?l:c.validation}},this.reindexFormObject=function(a){return function(b){var c,d,e,f;for(c=a.forms[b],d=e=0,f=c.length;f>e;d=e+=1)c[d].index=d}}(this),this.setupProviders=function(){return function(a){return c=a,b=c.get("$http"),d=c.get("$templateCache")}}(this),this.loadTemplate=function(a){return null==a.template&&b.get(a.templateUrl,{cache:d}).success(function(b){return a.template=b}),null==a.popoverTemplate?b.get(a.popoverTemplateUrl,{cache:d}).success(function(b){return a.popoverTemplate=b}):void 0},this.registerComponent=function(b){return function(d,e){var f,g;null==e&&(e={}),null==b.components[d]?(f=b.convertComponent(d,e),b.components[d]=f,null!=c&&b.loadTemplate(f),g=f.group,a.call(b.groups,g)<0&&b.groups.push(f.group)):console.error("The component "+d+" was registered.")}}(this),this.addFormObject=function(a){return function(b,c){var d;return null==c&&(c={}),null==(d=a.forms)[b]&&(d[b]=[]),a.insertFormObject(b,a.forms[b].length,c)}}(this),this.insertFormObject=function(a){return function(b,c,d){var e;return null==d&&(d={}),null==(e=a.forms)[b]&&(e[b]=[]),c>a.forms[b].length?c=a.forms[b].length:0>c&&(c=0),a.forms[b].splice(c,0,a.convertFormObject(b,d)),a.reindexFormObject(b),a.forms[b][c]}}(this),this.removeFormObject=function(a){return function(b,c){var d;return d=a.forms[b],d.splice(c,1),a.reindexFormObject(b)}}(this),this.updateFormObjectIndex=function(a){return function(b,c,d){var e,f;if(c!==d)return f=a.forms[b],e=f.splice(c,1)[0],f.splice(d,0,e),a.reindexFormObject(b)}}(this),this.$get=["$injector",function(a){return function(b){var c,d,e;a.setupProviders(b),e=a.components;for(d in e)c=e[d],a.loadTemplate(c);return{config:a.config,components:a.components,groups:a.groups,forms:a.forms,broadcastChannel:a.broadcastChannel,registerComponent:a.registerComponent,addFormObject:a.addFormObject,insertFormObject:a.insertFormObject,removeFormObject:a.removeFormObject,updateFormObjectIndex:a.updateFormObjectIndex}}}(this)]})}.call(this); \ No newline at end of file +(function(){var a;a=function(a,b){var c,d;for(c in a)d=a[c],"$$hashKey"!==c&&(b[c]=d)},angular.module("builder.controller",["builder.provider"]).controller("fbFormObjectEditableController",["$scope","$injector",function(b,c){var d;return d=c.get("$builder"),b.setupScope=function(c){var e;return a(c,b),b.optionsText=c.options.join("\n"),b.$watch("[label, description, placeholder, required, options, validation]",function(){return c.label=b.label,c.description=b.description,c.placeholder=b.placeholder,c.required=b.required,c.options=b.options,c.validation=b.validation},!0),b.$watch("optionsText",function(a){var c;return b.options=function(){var b,d,e,f;for(e=a.split("\n"),f=[],b=0,d=e.length;d>b;b++)c=e[b],c.length>0&&f.push(c);return f}(),b.inputText=b.options[0]}),e=d.components[c.component],b.validationOptions=e.validationOptions},b.data={model:null,backup:function(){return this.model={label:b.label,description:b.description,placeholder:b.placeholder,required:b.required,optionsText:b.optionsText,validation:b.validation}},rollback:function(){return this.model?(b.label=this.model.label,b.description=this.model.description,b.placeholder=this.model.placeholder,b.required=this.model.required,b.optionsText=this.model.optionsText,b.validation=this.model.validation):void 0}}}]).controller("fbComponentsController",["$scope","$injector",function(a,b){var c;return c=b.get("$builder"),a.selectGroup=function(b,d){var e,f,g,h;null!=b&&b.preventDefault(),a.activeGroup=d,a.components=[],g=c.components,h=[];for(f in g)e=g[f],e.group===d&&h.push(a.components.push(e));return h},a.groups=c.groups,a.activeGroup=a.groups[0],a.allComponents=c.components,a.$watch("allComponents",function(){return a.selectGroup(null,a.activeGroup)})}]).controller("fbComponentController",["$scope",function(b){return b.copyObjectToScope=function(c){return a(c,b)}}]).controller("fbFormController",["$scope","$injector",function(a,b){var c,d;return c=b.get("$builder"),d=b.get("$timeout"),null==a.input&&(a.input=[]),a.$watch("form",function(){return a.input.length>a.form.length&&a.input.splice(a.form.length),d(function(){return a.$broadcast(c.broadcastChannel.updateInput)})},!0)}]).controller("fbFormObjectController",["$scope","$injector",function(b,c){var d;return d=c.get("$builder"),b.copyObjectToScope=function(c){return a(c,b)},b.updateInput=function(a){var c;return c={id:b.formObject.id,label:b.formObject.label,value:null!=a?a:""},b.$parent.input.splice(b.$index,1,c)}}])}).call(this),function(){var a=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};angular.module("builder.directive",["builder.provider","builder.controller","builder.drag","validator"]).directive("fbBuilder",["$injector",function(a){var b,c;return b=a.get("$builder"),c=a.get("$drag"),{restrict:"A",scope:{fbBuilder:"="},template:"
\n
\n
",link:function(a,d,e){var f,g,h;return a.formName=e.fbBuilder,null==(g=b.forms)[h=a.formName]&&(g[h]=[]),a.formObjects=b.forms[a.formName],f=!0,$(d).addClass("fb-builder"),c.droppable($(d),{move:function(a){var b,c,e,g,h,i,j,k,l,m,n;if(f&&($("div.fb-form-object-editable").popover("hide"),f=!1),e=$(d).find(".fb-form-object-editable:not(.empty,.dragging)"),0===e.length)return void(0===$(d).find(".fb-form-object-editable.empty").length&&$(d).find(">div:first").append($("
")));for(j=[],j.push(-1e3),h=k=0,m=e.length;m>k;h=k+=1)c=$(e[h]),i=c.offset(),g=c.height(),j.push(i.top+g/2);for(j.push(j[j.length-1]+1e3),h=l=1,n=j.length;n>l;h=l+=1)if(a.pageY>j[h-1]&&a.pageY<=j[h]){$(d).find(".empty").remove(),b=$("
"),h-1l&&k--,b.updateFormObjectIndex(a.formName,l,k))):(j=i.object.formObject,j.editable&&b.removeFormObject(e.fbBuilder,j.index)),$(d).find(".empty").remove()):void $(d).find(".empty").remove()}})}}}]).directive("fbFormObjectEditable",["$injector",function(a){var b,c,d,e;return b=a.get("$builder"),d=a.get("$drag"),c=a.get("$compile"),e=a.get("$validator"),{restrict:"A",controller:"fbFormObjectEditableController",scope:{formObject:"=fbFormObjectEditable"},link:function(a,f){var g;return a.inputArray=[],a.$component=b.components[a.formObject.component],a.setupScope(a.formObject),a.$watch("$component.template",function(b){var d;if(b)return d=c(b)(a),$(f).html(d)}),$(f).on("click",function(){return!1}),d.draggable($(f),{object:{formObject:a.formObject}}),a.formObject.editable?(g={},a.$watch("$component.popoverTemplate",function(d){return d?($(f).removeClass(g.id),g={id:"fb-"+Math.random().toString().substr(2),isClickedSave:!1,view:null,html:d},g.html=$(g.html).addClass(g.id),g.view=c(g.html)(a),$(f).addClass(g.id),$(f).popover({html:!0,title:a.$component.label,content:g.view,container:"body",placement:b.config.popoverPlacement})):void 0}),a.popover={save:function(b){b.preventDefault(),e.validate(a).success(function(){return g.isClickedSave=!0,$(f).popover("hide")})},remove:function(c){c.preventDefault(),b.removeFormObject(a.$parent.formName,a.$parent.$index),$(f).popover("hide")},shown:function(){return a.data.backup(),g.isClickedSave=!1},cancel:function(b){a.data.rollback(),b&&(b.preventDefault(),$(f).popover("hide"))}},$(f).on("show.bs.popover",function(){var a,b,c;return d.isMouseMoved()?!1:($("div.fb-form-object-editable:not(."+g.id+")").popover("hide"),a=$("form."+g.id).closest(".popover"),a.length>0?(b=$(f).offset().top+$(f).height()/2,c=b-a.height()/2,a.css({position:"absolute",top:c}),a.show(),setTimeout(function(){return a.addClass("in"),$(f).triggerHandler("shown.bs.popover")},0),!1):void 0)}),$(f).on("shown.bs.popover",function(){$(".popover ."+g.id+" input:first").select(),a.$apply(function(){return a.popover.shown()})}),$(f).on("hide.bs.popover",function(){var b;return b=$("form."+g.id).closest(".popover"),g.isClickedSave||(a.$$phase||a.$root.$$phase?a.popover.cancel():a.$apply(function(){return a.popover.cancel()})),b.removeClass("in"),setTimeout(function(){return b.hide()},300),!1})):void 0}}}]).directive("fbComponents",function(){return{restrict:"A",template:'\n
\n
\n
',controller:"fbComponentsController"}}).directive("fbComponent",["$injector",function(a){var b,c,d;return b=a.get("$builder"),d=a.get("$drag"),c=a.get("$compile"),{restrict:"A",scope:{component:"=fbComponent"},controller:"fbComponentController",link:function(a,b){return a.copyObjectToScope(a.component),d.draggable($(b),{mode:"mirror",defer:!1,object:{componentName:a.component.name}}),a.$watch("component.template",function(d){var e;if(d)return e=c(d)(a),$(b).html(e)})}}}]).directive("fbForm",["$injector",function(a){return{restrict:"A",require:"ngModel",scope:{formName:"@fbForm",input:"=ngModel","default":"=fbDefault"},template:'
',controller:"fbFormController",link:function(b,c,d){var e,f,g;return e=a.get("$builder"),null==(f=e.forms)[g=b.formName]&&(f[g]=[]),b.form=e.forms[b.formName]}}}]).directive("fbFormObject",["$injector",function(b){var c,d,e;return c=b.get("$builder"),d=b.get("$compile"),e=b.get("$parse"),{restrict:"A",controller:"fbFormObjectController",link:function(b,f,g){var h,i,j;for(b.formObject=e(g.fbFormObject)(b),b.$component=c.components[b.formObject.component],b.$on(c.broadcastChannel.updateInput,function(){return b.updateInput(b.inputText)}),b.$component.arrayToText&&(b.inputArray=[],b.$watch("inputArray",function(a,c){var d,e,f;if(a!==c){d=[];for(e in b.inputArray)b.inputArray[e]&&d.push(null!=(f=b.options[e])?f:b.inputArray[e]);return b.inputText=d}},!0)),b.$watch("inputText",function(){return b.updateInput(b.inputText)}),b.$watch(g.fbFormObject,function(){return b.copyObjectToScope(b.formObject)},!0),b.$watch("$component.template",function(a){var c,e,g;if(a)return e=$(a),c=e.find("[ng-model='inputText']"),c.attr({validator:"{{validation}}"}),g=d(e)(b),$(f).html(g)}),!b.$component.arrayToText&&b.formObject.options.length>0&&(b.inputText=b.formObject.options[0]),h=i=0,j=b["default"].length;j>i&&b["default"][h].id!==b.formObject.id;h=i+=1);return b.$watch("default["+h+"]",function(c){var d,e,f,g;if(c){if(b.$component.arrayToText){for(d=e=0,f=b.formObject.options.length;f>e;d=e+=1)g=b.formObject.options[d],a.call(c.value,g)>=0?b.inputArray.push(b.formObject.options[d]):b.inputArray.push(!1);return b.inputArray}return b.inputText=c.value}})}}}])}.call(this),function(){angular.module("builder.drag",[]).provider("$drag",function(){var a,b,c;a=null,b=null,this.data={draggables:{},droppables:{}},this.mouseMoved=!1,this.isMouseMoved=function(a){return function(){return a.mouseMoved}}(this),this.hooks={down:{},move:{},up:{}},this.eventMouseMove=function(){},this.eventMouseUp=function(){},$(function(a){return function(){return $(document).on("mousedown",function(b){var c,d,e;a.mouseMoved=!1,e=a.hooks.down;for(d in e)(c=e[d])(b)}),$(document).on("mousemove",function(b){var c,d,e;a.mouseMoved=!0,e=a.hooks.move;for(d in e)(c=e[d])(b)}),$(document).on("mouseup",function(b){var c,d,e;e=a.hooks.up;for(d in e)(c=e[d])(b)})}}(this)),this.currentId=0,this.getNewId=function(a){return function(){return""+a.currentId++}}(this),this.setupEasing=function(){return jQuery.extend(jQuery.easing,{easeOutQuad:function(a,b,c,d,e){return-d*(b/=e)*(b-2)+c}})},this.setupProviders=function(c){return a=c,b=a.get("$rootScope")},this.isHover=function(a){return function(a,b){var c,d,e,f,g;return d=a.offset(),e=b.offset(),f={width:a.width(),height:a.height()},g={width:b.width(),height:b.height()},c={x:!1,y:!1},c.x=d.left>e.left&&d.lefte.left&&d.left+f.widthe.top&&d.tope.top&&d.top+f.height$(window).innerHeight()-50))return a.autoScroll.up=!1,a.autoScroll.down=!1;if(a.autoScroll.up=!1,a.autoScroll.down=!0,!a.autoScroll.scrolling)return a.autoScroll.scroll()}}}(this),stop:function(a){return function(){return a.autoScroll.up=!1,a.autoScroll.down=!1}}(this)},this.dragMirrorMode=function(a){return function(b,c,d){var e;return null==c&&(c=!0),e={id:a.getNewId(),mode:"mirror",maternal:b[0],element:null,object:d},b.on("mousedown",function(d){var f;return d.preventDefault(),f=b.clone(),e.element=f[0],f.addClass("fb-draggable form-horizontal prepare-dragging"),a.hooks.move.drag=function(c,d){var g,h,i,j;if(!f.hasClass("prepare-dragging")||(f.css({width:b.width(),height:b.height()}),f.removeClass("prepare-dragging"),f.addClass("dragging"),!d)){f.offset({left:c.pageX-f.width()/2,top:c.pageY-f.height()/2}),a.autoScroll.start(c),i=a.data.droppables,j=[];for(h in i)g=i[h],a.isHover(f,$(g.element))?j.push(g.move(c,e)):j.push(g.out(c,e));return j}},a.hooks.up.drag=function(b){var c,d,g,h;h=a.data.droppables;for(d in h)c=h[d],g=a.isHover(f,$(c.element)),c.up(b,g,e);return delete a.hooks.move.drag,delete a.hooks.up.drag,e.element=null,f.remove(),a.autoScroll.stop()},$("body").append(f),c?void 0:a.hooks.move.drag(d,c)}),e}}(this),this.dragDragMode=function(a){return function(b,c,d){var e;return null==c&&(c=!0),e={id:a.getNewId(),mode:"drag",maternal:null,element:b[0],object:d},b.addClass("fb-draggable"),b.on("mousedown",function(d){return d.preventDefault(),b.hasClass("dragging")?void 0:(b.addClass("prepare-dragging"),a.hooks.move.drag=function(c,d){var f,g,h;if(!b.hasClass("prepare-dragging")||(b.css({width:b.width(),height:b.height()}),b.removeClass("prepare-dragging"),b.addClass("dragging"),!d)){b.offset({left:c.pageX-b.width()/2,top:c.pageY-b.height()/2}),a.autoScroll.start(c),h=a.data.droppables;for(g in h)f=h[g],a.isHover(b,$(f.element))?f.move(c,e):f.out(c,e)}},a.hooks.up.drag=function(c){var d,f,g,h;h=a.data.droppables;for(f in h)d=h[f],g=a.isHover(b,$(d.element)),d.up(c,g,e);return delete a.hooks.move.drag,delete a.hooks.up.drag,b.css({width:"",height:"",left:"",top:""}),b.removeClass("dragging defer-dragging"),a.autoScroll.stop()},c?void 0:a.hooks.move.drag(d,c))}),e}}(this),this.dropMode=function(a){return function(c,d){var e;return e={id:a.getNewId(),element:c[0],move:function(a,c){return b.$apply(function(){return"function"==typeof d.move?d.move(a,c):void 0})},up:function(a,c,e){return b.$apply(function(){return"function"==typeof d.up?d.up(a,c,e):void 0})},out:function(a,c){return b.$apply(function(){return"function"==typeof d.out?d.out(a,c):void 0})}}}}(this),this.draggable=function(a){return function(b,c){var d,e,f,g,h,i,j;if(null==c&&(c={}),f=[],"mirror"===c.mode)for(g=0,i=b.length;i>g;g++)e=b[g],d=a.dragMirrorMode($(e),c.defer,c.object),f.push(d.id),a.data.draggables[d.id]=d;else for(h=0,j=b.length;j>h;h++)e=b[h],d=a.dragDragMode($(e),c.defer,c.object),f.push(d.id),a.data.draggables[d.id]=d;return f}}(this),this.droppable=function(a){return function(b,c){var d,e,f,g,h;for(null==c&&(c={}),f=[],g=0,h=b.length;h>g;g++)e=b[g],d=a.dropMode($(e),c),f.push(d),a.data.droppables[d.id]=d;return f}}(this),this.get=function(a){return this.setupEasing(),this.setupProviders(a),{isMouseMoved:this.isMouseMoved,data:this.data,draggable:this.draggable,droppable:this.droppable}},this.get.$inject=["$injector"],this.$get=this.get})}.call(this),function(){angular.module("builder",["builder.directive"])}.call(this),function(){var a=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};angular.module("builder.provider",[]).provider("$builder",function(){var b,c,d;c=null,b=null,d=null,this.config={popoverPlacement:"right"},this.components={},this.groups=[],this.broadcastChannel={updateInput:"$updateInput"},this.forms={"default":[]},this.convertComponent=function(a,b){var c,d,e,f,g,h,i,j,k,l,m;return c={name:a,group:null!=(d=b.group)?d:"Default",label:null!=(e=b.label)?e:"",description:null!=(f=b.description)?f:"",placeholder:null!=(g=b.placeholder)?g:"",editable:null!=(h=b.editable)?h:!0,required:null!=(i=b.required)?i:!1,validation:null!=(j=b.validation)?j:"/.*/",validationOptions:null!=(k=b.validationOptions)?k:[],options:null!=(l=b.options)?l:[],arrayToText:null!=(m=b.arrayToText)?m:!1,template:b.template,templateUrl:b.templateUrl,popoverTemplate:b.popoverTemplate,popoverTemplateUrl:b.popoverTemplateUrl},c.template||c.templateUrl||console.error("The template is empty."),c.popoverTemplate||c.popoverTemplateUrl||console.error("The popoverTemplate is empty."),c},this.convertFormObject=function(a,b){var c,d,e,f,g,h,i,j,k,l;if(null==b&&(b={}),c=this.components[b.component],null==c)throw"The component "+b.component+" was not registered.";return d={id:b.id,component:b.component,editable:null!=(e=b.editable)?e:c.editable,index:null!=(f=b.index)?f:0,label:null!=(g=b.label)?g:c.label,description:null!=(h=b.description)?h:c.description,placeholder:null!=(i=b.placeholder)?i:c.placeholder,options:null!=(j=b.options)?j:c.options,required:null!=(k=b.required)?k:c.required,validation:null!=(l=b.validation)?l:c.validation}},this.reindexFormObject=function(a){return function(b){var c,d,e,f;for(c=a.forms[b],d=e=0,f=c.length;f>e;d=e+=1)c[d].index=d}}(this),this.setupProviders=function(a){return function(a){return c=a,b=c.get("$http"),d=c.get("$templateCache")}}(this),this.loadTemplate=function(a){return null==a.template&&b.get(a.templateUrl,{cache:d}).success(function(b){return a.template=b}),null==a.popoverTemplate?b.get(a.popoverTemplateUrl,{cache:d}).success(function(b){return a.popoverTemplate=b}):void 0},this.registerComponent=function(b){return function(d,e){var f,g;null==e&&(e={}),null==b.components[d]?(f=b.convertComponent(d,e),b.components[d]=f,null!=c&&b.loadTemplate(f),g=f.group,a.call(b.groups,g)<0&&b.groups.push(f.group)):console.error("The component "+d+" was registered.")}}(this),this.addFormObject=function(a){return function(b,c){var d;return null==c&&(c={}),null==(d=a.forms)[b]&&(d[b]=[]),a.insertFormObject(b,a.forms[b].length,c)}}(this),this.insertFormObject=function(a){return function(b,c,d){var e;return null==d&&(d={}),null==(e=a.forms)[b]&&(e[b]=[]),c>a.forms[b].length?c=a.forms[b].length:0>c&&(c=0),a.forms[b].splice(c,0,a.convertFormObject(b,d)),a.reindexFormObject(b),a.forms[b][c]}}(this),this.removeFormObject=function(a){return function(b,c){var d;return d=a.forms[b],d.splice(c,1),a.reindexFormObject(b)}}(this),this.updateFormObjectIndex=function(a){return function(b,c,d){var e,f;if(c!==d)return f=a.forms[b],e=f.splice(c,1)[0],f.splice(d,0,e),a.reindexFormObject(b)}}(this),this.$get=["$injector",function(a){return function(b){var c,d,e;a.setupProviders(b),e=a.components;for(d in e)c=e[d],a.loadTemplate(c);return{config:a.config,components:a.components,groups:a.groups,forms:a.forms,broadcastChannel:a.broadcastChannel,registerComponent:a.registerComponent,addFormObject:a.addFormObject,insertFormObject:a.insertFormObject,removeFormObject:a.removeFormObject,updateFormObjectIndex:a.updateFormObjectIndex}}}(this)]})}.call(this); \ No newline at end of file diff --git a/src/directive.coffee b/src/directive.coffee index 81ae7a9..03f05e6 100644 --- a/src/directive.coffee +++ b/src/directive.coffee @@ -359,7 +359,7 @@ angular.module 'builder.directive', [ checked = [] for index of scope.inputArray when scope.inputArray[index] checked.push scope.options[index] ? scope.inputArray[index] - scope.inputText = checked.join ', ' + scope.inputText = checked , yes scope.$watch 'inputText', -> scope.updateInput scope.inputText # watch (management updated form objects @@ -383,10 +383,19 @@ angular.module 'builder.directive', [ scope.inputText = scope.formObject.options[0] # set default value - scope.$watch "default['#{scope.formObject.id}']", (value) -> + for index in [0...scope.default.length] by 1 + if scope.default[index].id == scope.formObject.id + break + + scope.$watch "default["+index+"]", (value) -> return if not value if scope.$component.arrayToText - scope.inputArray = value + for i in [0...scope.formObject.options.length] by 1 + if scope.formObject.options[i] in value.value + scope.inputArray.push scope.formObject.options[i] + else + scope.inputArray.push false + scope.inputArray; else - scope.inputText = value + scope.inputText = value.value ]