-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoms.app.crud.js
1 lines (1 loc) · 3.27 KB
/
atoms.app.crud.js
1
(function(){"use strict";var __hasProp={}.hasOwnProperty,__extends=function(child,parent){function ctor(){this.constructor=child}for(var key in parent)__hasProp.call(parent,key)&&(child[key]=parent[key]);return ctor.prototype=parent.prototype,child.prototype=new ctor,child.__super__=parent.prototype,child},__indexOf=[].indexOf||function(item){for(var i=0,l=this.length;l>i;i++)if(i in this&&this[i]===item)return i;return-1};Atoms.Organism.Crud=function(_super){function Crud(){var _ref;Crud.__super__.constructor.apply(this,arguments),this.attributes.fields=this.attributes.fields||(null!=(_ref=this.entity.toClassObject())?_ref.attributes:void 0),this.entityConstructor=this.entity.toClassObject(),delete this.entity}return __extends(Crud,_super),Crud["extends"]=!0,Crud.events=["create","update","delete"],Crud["default"]={children:[{"Organism.Header":{id:"header",children:[{"Atom.Heading":{id:"title"}},{"Molecule.Navigation":{style:"right",children:[{"Atom.Button":{icon:"close",callbacks:["onClose"]}}]}}]},"Organism.Section":{id:"section",children:[{"Molecule.Form":{id:"form"}}]},"Organism.Footer":{id:"footer",children:[{"Molecule.Navigation":{id:"nav",children:[{"Atom.Button":{id:"delete",style:"left",text:"Delete",callbacks:["onDestroy"]}},{"Atom.Button":{id:"save",style:"right default",text:"Save",callbacks:["onSave"]}}]}}]}}]},Crud.prototype.create=function(attributes){return null==attributes&&(attributes={}),attributes.destroy=!1,delete attributes.entity,this.show(attributes)},Crud.prototype.show=function(attributes){var fields,_ref;return null==attributes&&(attributes={}),Crud.__super__.show.apply(this,arguments),this.header.title.el.html(attributes.title||this.attributes.title),this.entity=attributes.entity,fields=this.attributes.fields,attributes.fields&&(fields=attributes.fields||this.attributes.fields||(null!=(_ref=this.entity)?_ref.constructor.attributes:void 0)),this.__createFields(fields,attributes.required),this.entity&&this.section.form.value(this.entity),this.footer.nav["delete"].el[attributes.destroy?"show":"hide"]()},Crud.prototype.onSave=function(){var key,method,valid,value,values;valid=!0,values=this.section.form.value();for(key in values)if(value=values[key],__indexOf.call(this.attributes.required,key)>=0&&!value){valid=!1;break}return valid?(this.entity?(method="update",this.entity.updateAttributes(values)):(method="create",this.entity=this.entityConstructor.create(values)),this.trigger(method,this.entity),this.hide()):void 0},Crud.prototype.onDestroy=function(){return this.entity.destroy(),this.trigger("destroy",this.entity),this.hide()},Crud.prototype.onClose=function(){return this.hide()},Crud.prototype.__createFields=function(fields,required){var field,_i,_len,_results;for(null==required&&(required=this.attributes.required||[]),this.section.form.destroyChildren(),_results=[],_i=0,_len=fields.length;_len>_i;_i++)field=fields[_i],_results.push(this.section.form.appendChild("Atom.Input",{id:field,name:field,type:this.__type(field),placeholder:field,required:__indexOf.call(required,field)>=0?!0:void 0}));return _results},Crud.prototype.__type=function(field){var type,value,_ref;return type="text",value=typeof(null!=(_ref=this.entity)?_ref[field]:void 0),"number"===value&&(type="number"),"boolean"===value&&(type="checkbox"),type},Crud}(Atoms.Organism.Dialog)}).call(this);