{"size": 3966, "ext": "js", "max_stars_repo_path": "packages/we-edit-representation-pagination/src/composable/hasChild.js", "max_stars_repo_name": "lalalic/we-ed", "max_stars_repo_stars_event_min_datetime": "2016-07-07T08:53:35.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-20T02:51:47.000Z", "max_issues_repo_path": "packages/we-edit-representation-pagination/src/composable/hasChild.js", "max_issues_repo_name": "lalalic/we-ed", "max_issues_repo_issues_event_min_datetime": "2018-04-12T09:01:45.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-14T10:36:06.000Z", "max_forks_repo_path": "packages/we-edit-representation-pagination/src/composable/hasChild.js", "max_forks_repo_name": "lalalic/we-ed", "max_forks_repo_forks_event_min_datetime": "2017-03-08T02:13:53.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-12T13:05:19.000Z"}
{"max_stars_count": 7, "max_issues_count": 15, "max_forks_count": 3, "avg_line_length": 30.984375, "max_line_length": 152, "alphanum_fraction": 0.5516893596}

import React, {Children,Fragment} from "react"
import PropTypes from "prop-types"
import memoize from "memoize-one"
import {dom} from "we-edit"
import ComposedAllTrigger from "./composed-all-trigger"
import Interseptor from "./interseptor"
export default A=>{
    if(A.already)
        return A
    return class __$1 extends A{
        static displayName=`pageable-${A.displayName}`
        static Interseptor=Interseptor
        static already=function(capable){
            return dom.Unknown.support.call(this,...arguments)
        }
        static contextTypes={
            ...super.contextTypes,
            debug: PropTypes.bool,
            //ModelTypes: PropTypes.object,
        }

        static childContextTypes = {
            ...super.childContextTypes,
            parent: PropTypes.object
        }

        constructor(){
            super(...arguments)
            this.computed = {composed: [] , allComposed:undefined}
            if(this.debug){
                console.debug(`${this.getComposeType()}[${this.props.id}] created`)
                this.state={computed:this.computed}
            }
        }


        getChildContext() {
            const superChildContext=super.getChildContext ? super.getChildContext() : {}
            return {
                ...superChildContext,
                parent: this
            }
        }

        /**
         * usually NoChild content should be composed according to nextAvailableSpace,
         * and then append to itself.composed[] and parent.appendComposed
         */
        render(){
            return (
                <Fragment>
                    {this.childrenArray(this.props.children)/****MUST use toArray(children), since recompose use this way to compose part of children*/}
                    {ComposedAllTrigger.createElement(this)}
                </Fragment>
            )
        }

        /**
         * children should call after a line composed out
         * a chance to add to self's composed
         */
        appendComposed(line) {

        }

        /**
         * children should call before composing line,
         * return next line rect {*width, [height]}
         */
        nextAvailableSpace(required = { width: 0, height: 0 }) {

        }

        isAllChildrenComposed() {
            return this.computed.allComposed
        }

        /**
         * allComposed is very important
         * if content is skipped composed, itself and grand content should be set as false, 
         * then this event of grand content should be disabled
         * @check this.notifyNotAllComposed
         * 
         * @check Recomposable.shouldComponentUpdate: reset allComposed when not isAllChildrenComposed,
         * so this event can works again 
         */
        onAllChildrenComposed() {
            if(this.computed.allComposed!==false){
                this.computed.allComposed=true
                console.debug(`${this.getComposeType()}[${this.props.id}] all children composed`)
            }
        }

        notifyNotAllComposed(current){
            current.computed.allComposed=false
            while(current=current.context.parent){
                current.computed.allComposed=false
            }
        }

        createComposed2Parent(content) {
            const {transformComposed=a=>a}=this.props
            return transformComposed(content)
        }

        get debug(){
            return !!this.context.debug||this.props.debug
        }
        
        closest(type){
            var fn=type
            if(typeof(type)=="string")
                fn=a=>a.getComposeType()==type

            var current=this
            while(current){
                if(!!fn(current))
                    return current
                if(current.context)
                    current=current.context.parent
            }
        }
        
        childrenArray=memoize((children=this.props.children)=>Children.toArray(children))
    }
}
