github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/components/table/cell_builder.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package table 16 17 import ( 18 "github.com/erda-project/erda-infra/providers/component-protocol/components/commodel" 19 "github.com/erda-project/erda-infra/providers/component-protocol/cptype" 20 "github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil" 21 ) 22 23 type ( 24 // CellBuilder . 25 CellBuilder struct { 26 *Cell 27 } 28 // ITypedCellBuilder used to not export typedCellBuilder 29 ITypedCellBuilder interface { 30 getCellBuilder() *CellBuilder 31 Build() Cell 32 } 33 // typedCellBuilder . 34 typedCellBuilder struct { 35 *CellBuilder 36 } 37 ) 38 39 func (tb *typedCellBuilder) getCellBuilder() *CellBuilder { 40 return tb.CellBuilder 41 } 42 43 func newCellBuilder() *CellBuilder { 44 return &CellBuilder{Cell: &Cell{}} 45 } 46 47 func (cb *CellBuilder) typed() *typedCellBuilder { 48 return &typedCellBuilder{CellBuilder: cb} 49 } 50 51 // Build . 52 func (tb *typedCellBuilder) Build() Cell { 53 return *tb.Cell 54 } 55 56 // WithID . 57 func (tb *typedCellBuilder) WithID(id string) *typedCellBuilder { 58 tb.Cell.ID = id 59 return tb 60 } 61 62 // WithTip . 63 func (tb *typedCellBuilder) WithTip(tip string) *typedCellBuilder { 64 tb.Cell.Tip = tip 65 return tb 66 } 67 68 // WithOperations . 69 func (tb *typedCellBuilder) WithOperations(ops map[cptype.OperationKey]cptype.Operation) *typedCellBuilder { 70 tb.Cell.Operations = ops 71 return tb 72 } 73 74 // WithOperation . 75 func (tb *typedCellBuilder) WithOperation(opKey cptype.OperationKey, op cptype.Operation) *typedCellBuilder { 76 if tb.Cell.Operations == nil { 77 tb.Cell.Operations = make(map[cptype.OperationKey]cptype.Operation) 78 } 79 tb.Cell.Operations[opKey] = op 80 return tb 81 } 82 83 // NewTextCell . 84 func NewTextCell(text string) ITypedCellBuilder { 85 cb := newCellBuilder() 86 cb.Cell.Type = CellType(commodel.Text{}.ModelType()) 87 cputil.MustObjJSONTransfer(&commodel.Text{Text: text}, &cb.Data) 88 return cb.typed() 89 } 90 91 // NewCompleteTextCell . 92 func NewCompleteTextCell(text commodel.Text) ITypedCellBuilder { 93 cb := newCellBuilder() 94 cb.Cell.Type = CellType(commodel.Text{}.ModelType()) 95 cputil.MustObjJSONTransfer(&text, &cb.Data) 96 return cb.typed() 97 } 98 99 // NewKVCell . 100 func NewKVCell(k, v string) ITypedCellBuilder { 101 cb := newCellBuilder() 102 cb.Cell.Type = CellType(commodel.KV{}.ModelType()) 103 cputil.MustObjJSONTransfer(&commodel.KV{K: k, V: v}, &cb.Data) 104 return cb.typed() 105 } 106 107 // NewIconCell . 108 func NewIconCell(icon commodel.Icon) ITypedCellBuilder { 109 cb := newCellBuilder() 110 cb.Cell.Type = CellType(commodel.Icon{}.ModelType()) 111 cputil.MustObjJSONTransfer(&icon, &cb.Data) 112 return cb.typed() 113 } 114 115 // NewUserCell . 116 func NewUserCell(user commodel.User) ITypedCellBuilder { 117 cb := newCellBuilder() 118 cb.Cell.Type = CellType(commodel.User{}.ModelType()) 119 cputil.MustObjJSONTransfer(&user, &cb.Data) 120 return cb.typed() 121 } 122 123 // NewLabelsCell . 124 func NewLabelsCell(labels commodel.Labels) ITypedCellBuilder { 125 cb := newCellBuilder() 126 cb.Cell.Type = CellType(commodel.Labels{}.ModelType()) 127 cputil.MustObjJSONTransfer(&labels, &cb.Data) 128 return cb.typed() 129 } 130 131 // NewDropDownMenuCell . 132 func NewDropDownMenuCell(menu commodel.DropDownMenu) ITypedCellBuilder { 133 cb := newCellBuilder() 134 cb.Cell.Type = CellType(commodel.DropDownMenu{}.ModelType()) 135 cputil.MustObjJSONTransfer(&menu, &cb.Data) 136 return cb.typed() 137 } 138 139 // NewUserSelectorCell . 140 func NewUserSelectorCell(selector commodel.UserSelector) ITypedCellBuilder { 141 cb := newCellBuilder() 142 cb.Cell.Type = CellType(commodel.UserSelector{}.ModelType()) 143 cputil.MustObjJSONTransfer(&selector, &cb.Data) 144 return cb.typed() 145 } 146 147 // NewMoreOperationsCell . 148 func NewMoreOperationsCell(moreOperations commodel.MoreOperations) ITypedCellBuilder { 149 cb := newCellBuilder() 150 cb.Cell.Type = CellType(commodel.MoreOperations{}.ModelType()) 151 cputil.MustObjJSONTransfer(&moreOperations, &cb.Data) 152 return cb.typed() 153 } 154 155 // NewDurationCell . 156 func NewDurationCell(duration commodel.Duration) ITypedCellBuilder { 157 cb := newCellBuilder() 158 cb.Cell.Type = CellType(commodel.Duration{}.ModelType()) 159 cputil.MustObjJSONTransfer(&duration, &cb.Data) 160 return cb.typed() 161 } 162 163 // NewProgressBarCell . 164 func NewProgressBarCell(progressBar commodel.ProgressBar) ITypedCellBuilder { 165 cb := newCellBuilder() 166 cb.Cell.Type = CellType(commodel.ProgressBar{}.ModelType()) 167 cputil.MustObjJSONTransfer(&progressBar, &cb.Data) 168 return cb.typed() 169 }