1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
class FindObjectMap{constructor(t,r,e){return this.root=t,"object"!=typeof this.root&&PrivateFind.error("Неизвестный root!"),this._app=r,this._num=e,this.title=void 0,this}all(t){const r=this.root.querySelectorAll(t);if(void 0===r)return void PrivateFind.error("Нет обьектов для селектора!");let e=new Array;for(let t=0;t<r.length;t++)e[t]=new FindObjectMap(r[t],this._app,this._num+1);return e}property(t,r){return void 0===r?this.root[t]:(this.root[t]=r,this)}select(t){const r=this.root.querySelector(t);if(void 0!==r)return new FindObjectMap(r,this._app,this._num+1);PrivateFind.error("Нет обьектов для селектора!")}status(){return"object"==typeof this.root&&null!==this.root&&void 0!==this.root}read(){return this.root.innerHTML}text(t){return 0==arguments.length?this.root.textContent:(this.root.textContent=t,this)}html(t){return 0==arguments.length?this.root.innerHTML:(this.root.innerHTML=t,this)}add(t){const r=this.html();return this.html(r+String(t))}value(t){return 0==arguments.length?this.root.value:(this.root.value=t,this)}id(t){const r=this.root.querySelector("#"+t);if(void 0!==r)return new FindObjectMap(r,this._app,this._num+1);PrivateFind.error("No objects of selector")}tag(t){const r=this.root.getElementsByTagName(t);if(void 0!==r)return new FindObjectMap(r,this._app,this._num+1);PrivateFind.error("No objects of selector")}attach(t,r){return this.root.addEventListener(t,()=>{r(this._app,this)}),this}merge(){if(0!=this._num)return PrivateFind.error("Вы можете создавать merge-обьект только из корневого map(), но уровень вложенности данного map() равен "+String(this._num)+"!"),this;const t=this._app.getNumberOfGroupFromId(this._app.__id);return this._app.groups[t].html=new Array(this.read()),this}app(){return this._app}main(){return new FindObjectMap(this._app.root,this._app,0)}attribute(t,r){return this.root.setAttribute(t,r),this}}class Find{constructor(t){return this._lll="",this.root=t,this.iter=0,this.groups=new Array,this.groups_count=0,this._map=new FindObjectMap(this.root,this,0),this}static createApp(){return Find}static attach(t,r){return`${t}="${r.replace('"',""")}"`}static css(t){return`<link rel="stylesheet" href="${t.replace('"',""")}"></link>`}static js(t){return`<script src="${t.replace('"',""")}"><\/script>`}static content(t){return t}static search(t){return document.getElementById(t)}rootId(){return this.root.id}main(t){return this.root=t,this._map.root=t,this}group(t,r,e){let i,s;1==arguments.length?(i=this.__id,s=t):(i=t,s=r);let o=!1;void 0!==e&&(o=!0);const n=this.getNumberOfGroupFromId(i,!1);let h;if(h=-1==n||o?e:this.groups[n].meta,"object"!=typeof h&&(h=new Array),-1!=n){const t=this.groups[n].every;return this.groups[n]={id:i,html:s,meta:h,title:document.title,every:t},this}if(!Array.isArray(s))return PrivateFind.findTypeError(s,"object"),this;for(let t=0;t<s.length;t++)"string"!=typeof s[t]&&PrivateFind.findTypeError(s[t],"string");return this.groups[this.groups_count]={id:i,html:s,meta:h,every:!1},this.groups_count++,this}delete(t,r){let e,i;1==arguments.length?(e=this.__id,i=t):(e=t,i=r);const s=this.getNumberOfGroupFromId(e),o=this.groups[s].html.length;(i<0||i>=o)&&PrivateFind.error("You can't remove element "+i+"!"),i++;const n=i>=o?i:o,h=this.groups[s].html;return this.groups[s].html=h.slice(0,i-1).concat(h.slice(i,n)),this}stable(t,r){let e,i;1==arguments.length||0==arguments.length?(e=this.__id,i=0==arguments.length||t):(e=t,i=r),0!=this.groups.length&&-1!=this.getNumberOfGroupFromId(e,!1)||this.group(e,[],[]);const s=i;return this.groups[this.getNumberOfGroupFromId(e)].every=s,this}unstable(t){let r=t;return 0==arguments.length&&(r=this.__id),this.stable(r,!1)}correct(t,r,e){let i,s,o;return 2==arguments.length?(i=this.__id,s=t,o=r):(i=t,s=r,o=e),this.groups[this.getNumberOfGroupFromId(i)].html[s]=o,this}add(t,r){let e,i;if(1==arguments.length?(e=this.__id,i=t):(e=t,i=r),0==this.groups.length||-1==this.getNumberOfGroupFromId(e,!1))return this.group(e,i),this;const s=this.getNumberOfGroupFromId(e);return this.groups[s].html=this.groups[s].html.concat(i),this}component(){return this.map()}title(t,r){let e,i;1==arguments.length?(e=this.__id,i=t):(e=t,i=r),0!==this.groups.length&&-1!==this.getNumberOfGroupFromId(e,!1)||this.group(e,[]);const s=i,o=this.getNumberOfGroupFromId(e);return this.groups[o].title=String(s),this}meta(t,r){let e,i;if(1==arguments.length?(e=this.__id,i=t):(e=t,i=r),0==this.groups.length||-1==this.getNumberOfGroupFromId(e,!1))return this.group(e,[],i),this;const s=this.getNumberOfGroupFromId(e);return this.groups[s].meta=this.groups[s].meta.concat(i),this}select(t){return this.__id=t,this}draw(...t){return this._lll="",this.__draw(...t),this.root.innerHTML=this._lll,this}__draw(t){void 0===this.__id&&PrivateFind.error("Use select, before draw. Or you can use render.");let r=this.groups[PrivateFind.arrayFindElement(this.groups,this.__id)];if("object"!=typeof r)return PrivateFind.findTypeError(r,"object"),this;if("object"!=typeof r.html)return PrivateFind.findTypeError(r.html,"object"),this;const e="string"==typeof t&&"__undstta"===t;if(!e)for(let t=0;t<this.groups.length;t++)"boolean"==typeof this.groups[t].every&&this.groups[t].every&&(this.render(this.groups[t].id,"__undstta"),this.iter--);if(void 0!==r.title&&document.title!==r.title&&(document.title=r.title),"object"==typeof r.meta&&Array.isArray(r.meta)&&r.meta.length>0&&0===this.iter){let t=document.head.innerHTML;for(let e=0;e<r.meta.length;e++)"string"==typeof r.meta[e]&&(t+=r.meta[e]);document.head.innerHTML=t}let i="";for(let t=0;t<r.html.length;t++)"string"==typeof r.html[t]&&(i+=r.html[t]);return this.root.innerHTML!==i&&(e?0!==i.length&&(this._lll+=i):this._lll+=i),this.iter++,this}render(t,r){let e;e=0==arguments.length?this.__id:t;const i=this.__id;return this.select(e),this.draw(r),this.__id=i,this}map(){return void 0===this.__id&&PrivateFind.findTypeError(this.__id,"number или string"),this._map}getNumberOfGroupFromId(t,r){let e=!1;(void 0===typeof r||"boolean"==typeof r&&1==r)&&(e=!0);const i=PrivateFind.arrayFindElement(this.groups,t);let s=this.groups[i];if("object"!=typeof s)return e&&PrivateFind.findTypeError(s,"object"),-1;let o=s.html;return"object"!=typeof o?(e&&PrivateFind.findTypeError(o,"object"),-1):i}}class FindError extends Error{constructor(t){super(t),this.name="Find Framework Error"}}class PrivateFind{static arrayFindElement(t,r){for(let e=0;e<t.length;e++)if(t[e].id===r)return e;return-1}static error(t){throw new FindError(t)}static findTypeError(t,r){throw new TypeError(`Вы пытаетесь передать ${typeof t}, в то время как ожидается ${r}!`)}} |