2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "item-view-api.h"
23 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
24 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-factory.h>
25 #include <dali-toolkit/public-api/controls/scrollable/item-view/default-item-layout.h>
29 #include <actors/actor-wrapper.h>
30 #include <controls/control-wrapper.h>
31 #include <controls/item-factory-wrapper.h>
39 namespace // unanmed namespace
42 Toolkit::ItemView GetItemView( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
44 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
45 return Toolkit::ItemView::DownCast( handleWrapper->mHandle );
50 /***************************************
51 * ITEMVIEW API FUNCTIONS
52 ***************************************/
60 * @return {Object} itemView
62 Toolkit::Control ItemViewApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
64 v8::Isolate* isolate = args.GetIsolate();
65 v8::HandleScope handleScope( isolate );
68 Toolkit::ItemFactory& factory = ItemFactoryWrapper::GetItemFactoryFromParams( 1, found, isolate, args );
71 DALI_SCRIPT_EXCEPTION( isolate, "invalid ItemFactory parameter" );
72 return Toolkit::Control();
76 Toolkit::ItemView itemView = Toolkit::ItemView::New(factory);
77 ItemFactoryWrapper::SetItemView(factory, itemView);
83 * Query the number of layouts.
86 * @method getLayoutCount
87 * @return {Integer} The number of layouts.
89 void ItemViewApi::GetLayoutCount( const v8::FunctionCallbackInfo< v8::Value >& args)
91 v8::Isolate* isolate = args.GetIsolate();
92 v8::HandleScope handleScope( isolate );
94 Toolkit::ItemView itemView = GetItemView( isolate, args );
96 args.GetReturnValue().Set( v8::Integer::New( isolate, itemView.GetLayoutCount() ) );
104 * @param {Integer} layout The layout to be added
106 * // layout is one of the following
107 * dali.ITEM_LAYOUT_LIST
108 * dali.ITEM_LAYOUT_GRID
110 * itemView.addLayout( dali.ITEM_LAYOUT_LIST );
112 void ItemViewApi::AddLayout( const v8::FunctionCallbackInfo<v8::Value>& args )
114 v8::Isolate* isolate = args.GetIsolate();
115 v8::HandleScope handleScope( isolate );
117 Toolkit::ItemView itemView = GetItemView( isolate, args );
120 int layout = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
123 DALI_SCRIPT_EXCEPTION( isolate, "invalid layout parameter" );
127 Toolkit::ItemLayoutPtr layoutPtr = Toolkit::DefaultItemLayout::New( static_cast<Toolkit::DefaultItemLayout::Type>(layout) );
128 itemView.AddLayout( *layoutPtr );
135 * @method removeLayout
136 * @param {Integer} layoutIndex The index of the ItemView layouts which must be less than getLayoutCount().
138 void ItemViewApi::RemoveLayout( const v8::FunctionCallbackInfo<v8::Value>& args )
140 v8::Isolate* isolate = args.GetIsolate();
141 v8::HandleScope handleScope( isolate );
143 Toolkit::ItemView itemView = GetItemView( isolate, args );
146 int layoutIndex = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
149 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
153 itemView.RemoveLayout( layoutIndex );
157 * Activate one of the layouts. This will resize the ItemView and relayout actors within the ItemView.
160 * @method activateLayout
161 * @param {Integer} layoutIndex The index of the ItemView layout which must be less than getLayoutCount().
162 * @param {Object} targetSize An array of 3 numbers for the target ItemView and layout size.
163 * @param {Float} [durationSeconds] The time taken to relayout in seconds (0 by default for immediate).
165 void ItemViewApi::ActivateLayout( const v8::FunctionCallbackInfo<v8::Value>& args )
167 v8::Isolate* isolate = args.GetIsolate();
168 v8::HandleScope handleScope( isolate );
170 Toolkit::ItemView itemView = GetItemView( isolate, args );
173 int layoutIndex = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
176 DALI_SCRIPT_EXCEPTION( isolate, "invalid layout index parameter" );
181 Vector3 targetSize = V8Utils::GetVector3Parameter( PARAMETER_1, found, isolate, args );
184 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 targetSize size parameter missing" );
189 float durationSeconds = V8Utils::GetFloatParameter( PARAMETER_2, found, isolate, args, 0.0f ); // 0 by default for immediate activation
191 itemView.ActivateLayout( layoutIndex, targetSize, durationSeconds );
195 * Retrieve the target size of an item in the given layout.
196 * This will return the default size for the layout unless overridden by calling setLayoutItemSize().
199 * @method getItemSize
200 * @param {Integer} layoutIndex The index of the ItemView layout which must be less than getLayoutCount().
201 * @param {Integer} itemId The ID of an item in the layout.
202 * @param {Object} targetLayoutSize An array of 3 numbers for the target ItemView and layout size.
203 * @return {Object} The target size of the item {x, y, z}.
205 void ItemViewApi::GetItemSize( const v8::FunctionCallbackInfo<v8::Value>& args )
207 v8::Isolate* isolate = args.GetIsolate();
208 v8::HandleScope handleScope( isolate );
210 Toolkit::ItemView itemView = GetItemView( isolate, args );
213 int layoutIndex = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
216 DALI_SCRIPT_EXCEPTION( isolate, "invalid layout index parameter" );
221 int itemId = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 /* default */);
224 DALI_SCRIPT_EXCEPTION( isolate, "invalid item ID parameter" );
229 Vector3 targetLayoutSize = V8Utils::GetVector3Parameter( PARAMETER_2, found, isolate, args );
232 Toolkit::ItemLayoutPtr layoutPtr = itemView.GetLayout(layoutIndex);
234 layoutPtr->GetItemSize( itemId, targetLayoutSize, itemSize );
236 v8::Local<v8::Object> itemSizeObject = v8::Object::New( isolate );
238 itemSizeObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, itemSize.width ) );
239 itemSizeObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, itemSize.height ) );
240 itemSizeObject->Set( v8::String::NewFromUtf8( isolate, "z" ), v8::Integer::New( isolate, itemSize.depth ) );
242 args.GetReturnValue().Set( itemSizeObject );
246 DALI_SCRIPT_EXCEPTION( isolate, "invalid Vector3 target size parameter" );
251 * Set the size of the item for the given layout which overrides the default item size for the layout.
254 * @method setItemSize
255 * @param {Integer} layoutIndex The index of the ItemView layout which must be less than getLayoutCount().
256 * @param {Object} itemSize An array of 3 numbers for the size of the item.
258 * itemView.setLayoutItemSize( 0, [100.0, 50.0, 0.0] );
260 void ItemViewApi::SetItemSize( const v8::FunctionCallbackInfo<v8::Value>& args )
262 v8::Isolate* isolate = args.GetIsolate();
263 v8::HandleScope handleScope( isolate );
265 Toolkit::ItemView itemView = GetItemView( isolate, args );
268 int layoutIndex = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
271 DALI_SCRIPT_EXCEPTION( isolate, "invalid layout index parameter" );
276 Vector3 itemSize = V8Utils::GetVector3Parameter( PARAMETER_1, found, isolate, args );
279 Toolkit::ItemLayoutPtr layoutPtr = itemView.GetLayout(layoutIndex);
280 layoutPtr->SetItemSize( itemSize );
284 DALI_SCRIPT_EXCEPTION( isolate, "invalid item size parameter" );
289 * Scroll the current layout to a particular item.
290 * If calling this with zero second of duration immediately after calling activateLayout(),
291 * it will not work unless the duration of relayout animation for activateLayout is also
295 * @method scrollToItem
296 * @param {Integer} itemId The ID of an item in the layout.
297 * @param {Float} [durationSeconds] How long the scrolling takes in seconds (0 by default for instant scrolling to the particular item).
299 void ItemViewApi::ScrollToItem( const v8::FunctionCallbackInfo<v8::Value>& args )
301 v8::Isolate* isolate = args.GetIsolate();
302 v8::HandleScope handleScope( isolate );
304 Toolkit::ItemView itemView = GetItemView( isolate, args );
307 int itemId = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
310 DALI_SCRIPT_EXCEPTION( isolate, "invalid item Id parameter" );
315 float durationSeconds = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, 0.0f ); // 0 by default for instant scrolling
317 itemView.ScrollToItem( itemId, durationSeconds );
321 * Given the Item ID, this returns the accompanying actor.
325 * @param {Integer} itemId The Item ID of the actor required.
326 * @return {Object} The Actor corresponding to the Item ID.
328 void ItemViewApi::GetItem( const v8::FunctionCallbackInfo<v8::Value>& args )
330 v8::Isolate* isolate = args.GetIsolate();
331 v8::HandleScope handleScope( isolate );
333 Toolkit::ItemView itemView = GetItemView( isolate, args );
336 int itemId = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
340 Actor actor = itemView.GetItem( itemId );
345 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, actor );
346 args.GetReturnValue().Set( wrappedActor );
352 DALI_SCRIPT_EXCEPTION( isolate, "invalid item ID" );
358 * Returns the Item ID of the specified actor. The actor must be an item of ItemView.
362 * @param {Object} actor The actor whose Item ID is required.
363 * @return {Integer} The Item ID of the item.
365 void ItemViewApi::GetItemId( const v8::FunctionCallbackInfo<v8::Value>& args )
367 v8::Isolate* isolate = args.GetIsolate();
368 v8::HandleScope handleScope( isolate );
370 Toolkit::ItemView itemView = GetItemView( isolate, args );
373 Actor actor = V8Utils::GetActorParameter( 0, found, isolate, args );
376 args.GetReturnValue().Set( itemView.GetItemId(actor) );
380 DALI_SCRIPT_EXCEPTION( isolate, "invalid item actor parameter" );
386 * Get the range of items that are currently in ItemView.
389 * @method getItemsRange
390 * @return {Object} The range of items in the item ID {begin, end}.
392 void ItemViewApi::GetItemsRange( const v8::FunctionCallbackInfo<v8::Value>& args )
394 v8::Isolate* isolate = args.GetIsolate();
395 v8::HandleScope handleScope( isolate );
397 Toolkit::ItemView itemView = GetItemView( isolate, args );
399 Toolkit::ItemRange range(0, 0);
400 itemView.GetItemsRange(range);
402 v8::Local<v8::Object> itemRangeObject = v8::Object::New( isolate );
404 itemRangeObject->Set( v8::String::NewFromUtf8( isolate, "begin" ), v8::Integer::New( isolate, range.begin ) );
405 itemRangeObject->Set( v8::String::NewFromUtf8( isolate, "end" ), v8::Integer::New( isolate, range.end ) );
407 args.GetReturnValue().Set( itemRangeObject );
410 } // namespace V8Plugin