1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 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 #ifndef OHOS_ACELITE_COMPONENT_FACTORY_H 16 #define OHOS_ACELITE_COMPONENT_FACTORY_H 17 18 #include "acelite_config.h" 19 20 #if (FEATURE_COMPONENT_ANALOG_CLOCK == 1) 21 #include "analog_clock_component.h" 22 #include "clock_hand_component.h" 23 #endif // FEATURE_COMPONENT_ANALOG_CLOCK 24 #if (FEATURE_COMPONENT_CAMERA == 1) 25 #include "camera_component.h" 26 #endif // FEATURE_COMPONENT_CAMERA 27 #if (FEATURE_COMPONENT_CANVAS == 1) 28 #include "canvas_component.h" 29 #endif // FEATURE_COMPONENT_CANVAS 30 #include "chart_component.h" 31 #include "circle_progress_component.h" 32 #include "component.h" 33 #include "div_component.h" 34 #include "fatal_handler.h" 35 #include "horizon_progress_component.h" 36 #include "image_animator_component.h" 37 #include "image_component.h" 38 #include "input_button_component.h" 39 #include "input_checkbox_component.h" 40 #if (FEATURE_COMPONENT_EDITTEXT == 1) 41 #include "input_edittext_component.h" 42 #endif // FEATURE_COMPONENT_EDITTEXT 43 #include "input_radio_component.h" 44 #include "js_fwk_common.h" 45 #include "key_parser.h" 46 #include "keys.h" 47 #include "list_component.h" 48 #include "marquee_component.h" 49 #include "non_copyable.h" 50 #include "picker_view_component.h" 51 #if (FEATURE_COMPONENT_QRCODE == 1) 52 #include "qrcode_component.h" 53 #endif 54 #include "slider_component.h" 55 #include "stack_component.h" 56 #include "swiper_component.h" 57 #include "switch_component.h" 58 #if (FEATURE_COMPONENT_TABS == 1) 59 #include "tab_bar_component.h" 60 #include "tab_content_component.h" 61 #include "tabs_component.h" 62 #endif // FEATURE_COMPONENT_TABS 63 #include "text_component.h" 64 #include "video_component.h" 65 66 namespace OHOS { 67 namespace ACELite { 68 class ComponentFactory final : public MemoryHeap { 69 public: 70 ACE_DISALLOW_COPY_AND_MOVE(ComponentFactory); ~ComponentFactory()71 ~ComponentFactory() {} CreateComponent(uint16_t componentNameId,jerry_value_t options,jerry_value_t children)72 static Component* CreateComponent(uint16_t componentNameId, jerry_value_t options, jerry_value_t children) 73 { 74 if (!KeyParser::IsKeyValid(componentNameId)) { 75 return nullptr; 76 } 77 78 JsAppContext* context = JsAppContext::GetInstance(); 79 if (context == nullptr) { 80 return nullptr; 81 } 82 AppStyleManager* styleManager = const_cast<AppStyleManager *>(context->GetStyleManager()); 83 84 Component* component; 85 switch (componentNameId) { 86 #if (FEATURE_COMPONENT_CAMERA == 1) 87 case K_CAMERA: 88 component = new CameraComponent(options, children, styleManager); 89 break; 90 #endif 91 #if (FEATURE_COMPONENT_CANVAS == 1) 92 case K_CANVAS: 93 component = new CanvasComponent(options, children, styleManager); 94 break; 95 #endif // FEATURE_COMPONENT_CANVAS 96 case K_DIV: 97 component = new DivComponent(options, children, styleManager); 98 break; 99 case K_STACK: 100 component = new StackComponent(options, children, styleManager); 101 break; 102 case K_IMAGE: 103 component = new ImageComponent(options, children, styleManager); 104 break; 105 case K_IMAGE_ANIMATOR: 106 component = new ImageAnimatorComponent(options, children, styleManager); 107 break; 108 case K_PROGRESS: { 109 uint16_t id = ComponentUtils::GetComponentType(options); 110 if (id == K_ARC) { 111 component = new CircleProgressComponent(options, children, styleManager); 112 } else { 113 // default type is horizon progress 114 component = new HorizonProgressComponent(options, children, styleManager); 115 } 116 break; 117 } 118 #if (FEATURE_COMPONENT_TABS == 1) 119 case K_TAB_BAR: 120 component = new TabBarComponent(options, children, styleManager); 121 break; 122 case K_TAB_CONTENT: 123 component = new TabContentComponent(options, children, styleManager); 124 break; 125 case K_TABS: 126 component = new TabsComponent(options, children, styleManager); 127 break; 128 #endif // FEATURE_COMPONENT_TABS 129 case K_TEXT: 130 component = new TextComponent(options, children, styleManager); 131 break; 132 case K_SWIPER: 133 component = new SwiperComponent(options, children, styleManager); 134 break; 135 case K_SWITCH: 136 component = new SwitchComponent(options, children, styleManager); 137 break; 138 case K_SLIDER: 139 component = new SliderComponent(options, children, styleManager); 140 break; 141 case K_LIST: 142 component = new ListComponent(options, children, styleManager); 143 break; 144 case K_LIST_ITEM: 145 component = new DivComponent(options, children, styleManager); 146 break; 147 #if (FEATURE_COMPONENT_ANALOG_CLOCK == 1) 148 case K_CLOCK_HAND: 149 component = new ClockHandComponent(options, children, styleManager); 150 break; 151 case K_ANALOG_CLOCK: 152 component = new AnalogClockComponent(options, children, styleManager); 153 break; 154 #endif // FEATURE_COMPONENT_ANALOG_CLOCK 155 case K_INPUT: { 156 uint16_t id = ComponentUtils::GetComponentType(options); 157 if (id == K_CHECKBOX) { 158 component = new InputCheckboxComponent(options, children, styleManager); 159 } else if (id == K_RADIO) { 160 component = new InputRadioComponent(options, children, styleManager); 161 } 162 #if (FEATURE_COMPONENT_EDITTEXT == 1) 163 else if (id == K_TEXT || id == K_PASSWORD) { 164 component = new InputEditTextComponent(options, children, styleManager); 165 } 166 #endif // FEATURE_COMPONENT_EDITTEXT 167 else { 168 // default type is input button 169 component = new InputButtonComponent(options, children, styleManager); 170 } 171 break; 172 } 173 case K_MARQUEE: 174 component = new MarqueeComponent(options, children, styleManager); 175 break; 176 case K_CHART: 177 component = new ChartComponent(options, children, styleManager); 178 break; 179 case K_PICKER_VIEW: 180 component = new PickerViewComponent(options, children, styleManager); 181 break; 182 #if (FEATURE_COMPONENT_QRCODE == 1) 183 case K_QRCODE: 184 component = new QrcodeComponent(options, children, styleManager); 185 break; 186 #endif // FEATURE_COMPONENT_QRCODE 187 #if (FEATURE_COMPONENT_VIDEO == 1) 188 case K_VIDEO: 189 component = new VideoComponent(options, children, styleManager); 190 break; 191 #endif // FEATURE_COMPONENT_VIDEO 192 default: 193 component = nullptr; 194 break; 195 } 196 197 FatalHandler::GetInstance().AttachComponentNode(component); 198 return component; 199 } 200 }; 201 } // namespace ACELite 202 } // namespace OHOS 203 204 #endif // OHOS_ACELITE_COMPONENT_FACTORY_H 205