[dali_1.4.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / controls / item-factory-wrapper.h
1 #ifndef DALI_V8PLUGIN_ITEM_FACTORY_WRAPPER_H
2 #define DALI_V8PLUGIN_ITEM_FACTORY_WRAPPER_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <v8.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-factory.h>
25 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
26
27 // INTERNAL INCLUDES
28 #include <shared/base-wrapped-object.h>
29
30 namespace Dali
31 {
32
33 namespace V8Plugin
34 {
35
36
37 /**
38  * Wraps a Dali ItemFactory.
39  */
40 class ItemFactoryWrapper : public BaseWrappedObject
41 {
42
43 public:
44
45   /**
46    * Constructor
47    * @param factory DALi ItemFactory
48    * @param gc garbage collection interface
49    */
50   ItemFactoryWrapper( Toolkit::ItemFactory& factory,
51                 GarbageCollectorInterface& gc );
52
53   /**
54    * destructor
55    */
56   virtual ~ItemFactoryWrapper();
57
58   /**
59    * @brief Creates a new ItemFactory wrapped inside a Javascript Object.
60    * @note: the item template and data are passed as a parameter e.g. 'template'
61    * @param[in] args v8 function call arguments interpreted
62    */
63   static void NewItemFactory( const v8::FunctionCallbackInfo< v8::Value >& args);
64
65   /**
66    * @brief Wraps an ItemFactory
67    */
68   static v8::Handle<v8::Object> WrapItemFactory(v8::Isolate* isolate, Toolkit::ItemFactory& factory );
69
70   // The ItemFactory ObjectTemplates.
71   static v8::Persistent<v8::ObjectTemplate> mItemFactoryTemplate;
72
73   /**
74    * @brief Helper to get ItemFactory from the JavaScript object held in the given function argument
75    * @param[in] paramIndex Argument index the object is held in
76    * @param[in] found Whether ItemFactory is found in the given function parameter
77    * @param[in] isolate v8 isolated instance
78    * @param[in] args v8 function call arguments interpreted
79    */
80   static Toolkit::ItemFactory& GetItemFactoryFromParams( int paramIndex,
81                                                              bool& found,
82                                                              v8::Isolate* isolate,
83                                                              const v8::FunctionCallbackInfo< v8::Value >& args );
84
85   /**
86    * @brief Helper to store a weak handle of ItemView in the given ItemFactory
87    * @param[in] itemFactory The item factory used to provide items to the given item view
88    * @param[in] itemView The ItemView which uses the given item factory to create items
89    */
90   static void SetItemView(Toolkit::ItemFactory& itemFactory, Toolkit::ItemView itemView);
91
92   /**
93    * @return the wrapped item factory
94    */
95   Toolkit::ItemFactory& GetItemFactory();
96
97 private:
98
99   /**
100    * Helper to make the item factory template
101    */
102   static v8::Handle<v8::ObjectTemplate> MakeItemFactoryTemplate( v8::Isolate* isolate );
103
104   /**
105    * Helper, get a item factory template
106    */
107   static v8::Local<v8::ObjectTemplate> GetItemFactoryTemplate( v8::Isolate* isolate );
108
109   /**
110    * @brief get the value for a property for JavaScript object than contains a Dali ItemFactory.
111    * E.g. Get( "data", JavaScript object that wraps a Dali ItemFactory )
112    * @param[in] propertyName property name
113    * @param[in] info reference to PropertyCallbackInfo structure (contains the Javascript
114    * object and the return value).
115    */
116   static void PropertyGet( v8::Local<v8::String> propertyName,
117                               const v8::PropertyCallbackInfo<v8::Value>& info);
118
119   /**
120    * @brief Set the value for a property for JavaScript object than contains a Dali ItemFactory.
121    * E.g. Set( "data", itemData, JavaScript object that wraps a Dali ItemFactory)
122    * @param[in] propertyName property name
123    * @param[in] javaScriptValue javascript value to set, this is typically a number
124    * @param[in] info reference to PropertyCallbackInfo structure (contains the Javascript
125    * object).
126    */
127   static void PropertySet( v8::Local<v8::String> propertyName,
128                               v8::Local<v8::Value> javaScriptValue,
129                               const v8::PropertyCallbackInfo<v8::Value>& info);
130
131
132   /**
133    * @brief Extract a item factory wrapper from a javascript object
134    * @return item factory wrapper
135    */
136   static ItemFactoryWrapper* Unwrap( v8::Isolate* isolate, v8::Handle< v8::Object> obj);
137
138   Toolkit::ItemFactory& mItemFactory;
139
140 };
141
142 } // namespace V8Plugin
143
144 } // namespace Dali
145
146 #endif // DALI_V8PLUGIN_ITEM_FACTORY_WRAPPER_H