Browse Source

custom: simplify initialization

Falko Schindler 4 years ago
parent
commit
2b2aaac2d0

+ 3 - 5
nicegui/elements/custom_example.py

@@ -1,18 +1,16 @@
-import os.path
 from .custom_view import CustomView
 from .element import Element
 
 class CustomExampleView(CustomView):
 
-    vue_type = 'custom_example'
-    vue_filepath = os.path.realpath(__file__).replace('.py', '.js')
-
     def __init__(self, on_change):
 
         self.on_change = on_change
 
-        super().__init__(value=0)
+        super().__init__('custom_example', __file__, value=0)
+
         self.allowed_events = ['onAdd']
+
         self.initialize(temp=False, onAdd=self.handle_add)
 
     def handle_add(self, msg):

+ 6 - 1
nicegui/elements/custom_view.py

@@ -1,10 +1,15 @@
 import justpy as jp
+import os.path
 
 class CustomView(jp.JustpyBaseComponent):
 
     vue_dependencies = []
 
-    def __init__(self, **options):
+    def __init__(self, vue_type, filename, dependencies=[], **options):
+
+        self.vue_type = vue_type
+        self.vue_filepath = os.path.realpath(filename).replace('.py', '.js')
+        self.vue_dependencies = dependencies
 
         self.pages = {}
         self.classes = ''

+ 5 - 8
nicegui/elements/joystick.py

@@ -1,21 +1,18 @@
-import os.path
 from .custom_view import CustomView
 from .element import Element
 
 class JoystickView(CustomView):
 
-    vue_type = 'joystick'
-    vue_filepath = os.path.realpath(__file__).replace('.py', '.js')
-    vue_dependencies = [
-        'https://cdn.jsdelivr.net/npm/nipplejs@0.9.0/dist/nipplejs.min.js',
-    ]
-
     def __init__(self, on_move):
 
         self.on_move = on_move
 
-        super().__init__()
+        super().__init__('joystick', __file__, [
+            'https://cdn.jsdelivr.net/npm/nipplejs@0.9.0/dist/nipplejs.min.js',
+        ])
+
         self.allowed_events = ['onMove']
+
         self.initialize(temp=False, onMove=self.handle_move)
 
     def handle_move(self, msg):

+ 6 - 9
nicegui/elements/three.py

@@ -1,22 +1,19 @@
-import os.path
 from .custom_view import CustomView
 from .element import Element
 
 class ThreeView(CustomView):
 
-    vue_type = 'three'
-    vue_filepath = os.path.realpath(__file__).replace('.py', '.js')
-    vue_dependencies = [
-        'https://cdn.jsdelivr.net/npm/three@0.129.0/build/three.min.js',
-        'https://cdn.jsdelivr.net/npm/three@0.129.0/examples/js/controls/OrbitControls.js',
-    ]
-
     def __init__(self, on_click):
 
         self.on_click = on_click
 
-        super().__init__(camera_z=4)
+        super().__init__('three', __file__, [
+            'https://cdn.jsdelivr.net/npm/three@0.129.0/build/three.min.js',
+            'https://cdn.jsdelivr.net/npm/three@0.129.0/examples/js/controls/OrbitControls.js',
+        ], camera_z=4)
+
         self.allowed_events = ['onClick']
+
         self.initialize(temp=False, onClick=self.handle_click)
 
     def handle_click(self, msg):