yugrid
Version:
yugrid 现代化响应式 Grid 布局系统,支持 ES Module 和 CommonJS
2 lines (1 loc) • 8.01 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).YuGrid=e()}(this,function(){"use strict";function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,l(r.key),r)}}function i(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function o(t,e,n){return(e=l(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function s(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),n.push.apply(n,r)}return n}function a(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?s(Object(n),!0).forEach(function(e){o(t,e,n[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))})}return t}function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,i,o,s,a=[],u=!0,l=!1;try{if(o=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;u=!1}else for(;!(u=(r=o.call(n)).done)&&(a.push(r.value),a.length!==e);u=!0);}catch(t){l=!0,i=t}finally{try{if(!u&&null!=n.return&&(s=n.return(),Object(s)!==s))return}finally{if(l)throw i}}return a}}(t,e)||c(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}function c(t,n){if(t){if("string"==typeof t)return e(t,n);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(t,n):void 0}}var f,p={exports:{}};var d=(f||(f=1,function(t,e){var r={xs:0,sm:576,md:768,lg:992,xl:1200,xxl:1400},o={sm:540,md:720,lg:960,xl:1140,xxl:1320};function s(){var t,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:r,n=window.innerWidth,i=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=c(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,s=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){a=!0,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(a)throw o}}}}(Object.entries(e).sort(function(t,e){var n=u(t,2)[1];return u(e,2)[1]-n}));try{for(i.s();!(t=i.n()).done;){var o=u(t.value,2),s=o[0];if(n>=o[1])return s}}catch(t){i.e(t)}finally{i.f()}return"xs"}var l=function(){return i(function t(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};n(this,t),this.element="string"==typeof e?document.querySelector(e):e,this.options=a({fluid:!1,maxWidths:o,breakpoints:r},i),this.init()},[{key:"init",value:function(){this.element.classList.add("yu-container"),this.options.fluid&&this.element.classList.add("yu-container-fluid"),this.updateStyles(),this.bindEvents()}},{key:"updateStyles",value:function(){if(!this.options.fluid){var t=s(this.options.breakpoints),e=this.options.maxWidths[t];this.element.style.maxWidth=e?"".concat(e,"px"):"100%"}}},{key:"bindEvents",value:function(){var t=this;window.addEventListener("resize",function(){t.updateStyles()})}}])}(),f=function(){return i(function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};n(this,t),this.element="string"==typeof e?document.querySelector(e):e,this.options=a({gutter:0,justify:"start",align:"top",wrap:!0},r),this.init()},[{key:"init",value:function(){this.element.classList.add("yu-row"),"start"!==this.options.justify&&this.element.classList.add("yu-row-justify-".concat(this.options.justify)),"top"!==this.options.align&&this.element.classList.add("yu-row-align-".concat(this.options.align)),this.options.wrap||this.element.classList.add("yu-row-nowrap"),this.updateGutter()}},{key:"updateGutter",value:function(){if(this.options.gutter>0){var t=this.options.gutter/2;this.element.style.marginLeft="-".concat(t,"px"),this.element.style.marginRight="-".concat(t,"px"),this.element.querySelectorAll(".yu-col").forEach(function(e){e.style.paddingLeft="".concat(t,"px"),e.style.paddingRight="".concat(t,"px")})}}},{key:"setGutter",value:function(t){this.options.gutter=t,this.updateGutter()}}])}(),p=function(){return i(function t(e){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};n(this,t),this.element="string"==typeof e?document.querySelector(e):e,this.options=a({span:24,offset:0,push:0,pull:0,order:0,flex:null,breakpoints:r,responsive:{}},i),this.init()},[{key:"init",value:function(){this.element.classList.add("yu-col"),this.updateClasses(),this.bindEvents()}},{key:"updateClasses",value:function(){var t=this.element.classList;Array.from(t).filter(function(t){return t.startsWith("yu-col-")}).forEach(function(e){return t.remove(e)});var e=s(this.options.breakpoints),n=this.getConfigForBreakpoint(e);void 0!==n.span&&t.add("yu-col-".concat(n.span)),n.offset>0&&t.add("yu-col-offset-".concat(n.offset)),n.push>0&&t.add("yu-col-push-".concat(n.push)),n.pull>0&&t.add("yu-col-pull-".concat(n.pull)),0!==n.order&&(this.element.style.order=n.order),n.flex&&(this.element.style.flex=n.flex)}},{key:"getConfigForBreakpoint",value:function(t){for(var e=["xxl","xl","lg","md","sm","xs"],n=e.indexOf(t);n<e.length;n++){var r=e[n];if(this.options.responsive[r])return a(a({},this.options),this.options.responsive[r])}return this.options}},{key:"bindEvents",value:function(){var t=this;window.addEventListener("resize",function(){t.updateClasses()})}},{key:"setSpan",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;e?this.options.responsive[e]=a(a({},this.options.responsive[e]),{},{span:t}):this.options.span=t,this.updateClasses()}}])}();function d(t,e){return new l(t,e)}function y(t,e){return new f(t,e)}function h(t,e){return new p(t,e)}function v(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"[data-yu-grid]",e=document.querySelectorAll(t),n=[];return e.forEach(function(t){var e,r=t.dataset.yuGrid,i=t.dataset.yuOptions?JSON.parse(t.dataset.yuOptions):{};switch(r){case"container":e=new l(t,i);break;case"row":e=new f(t,i);break;case"col":e=new p(t,i)}e&&n.push(e)}),n}var m={Container:l,Row:f,Col:p,createContainer:d,createRow:y,createCol:h,initGrid:v,DEFAULT_BREAKPOINTS:r,DEFAULT_CONTAINER_MAX_WIDTHS:o,getCurrentBreakpoint:s};t.exports?(t.exports=m,t.exports.default=m):"undefined"!=typeof window&&(window.YuGrid=m);try{e.default=m,e.YuContainer=l,e.YuRow=f,e.YuCol=p,e.createContainer=d,e.createRow=y,e.createCol=h,e.initGrid=v,e.DEFAULT_BREAKPOINTS=r,e.DEFAULT_CONTAINER_MAX_WIDTHS=o,e.getCurrentBreakpoint=s}catch(t){}}(p,p.exports)),p.exports);return t(d)});