6291 |
11 Jun 13 |
nicklas |
/* $Id $ |
6291 |
11 Jun 13 |
nicklas |
2 |
------------------------------------------------------------------ |
6291 |
11 Jun 13 |
nicklas |
Copyright (C) 2013 Nicklas Nordborg |
6291 |
11 Jun 13 |
nicklas |
4 |
|
6291 |
11 Jun 13 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6291 |
11 Jun 13 |
nicklas |
Available at http://base.thep.lu.se/ |
6291 |
11 Jun 13 |
nicklas |
7 |
|
6291 |
11 Jun 13 |
nicklas |
BASE is free software; you can redistribute it and/or |
6291 |
11 Jun 13 |
nicklas |
modify it under the terms of the GNU General Public License |
6291 |
11 Jun 13 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6291 |
11 Jun 13 |
nicklas |
of the License, or (at your option) any later version. |
6291 |
11 Jun 13 |
nicklas |
12 |
|
6291 |
11 Jun 13 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6291 |
11 Jun 13 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6291 |
11 Jun 13 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6291 |
11 Jun 13 |
nicklas |
GNU General Public License for more details. |
6291 |
11 Jun 13 |
nicklas |
17 |
|
6291 |
11 Jun 13 |
nicklas |
You should have received a copy of the GNU General Public License |
6291 |
11 Jun 13 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6291 |
11 Jun 13 |
nicklas |
20 |
------------------------------------------------------------------ |
6291 |
11 Jun 13 |
nicklas |
21 |
|
6291 |
11 Jun 13 |
nicklas |
@author Nicklas |
6291 |
11 Jun 13 |
nicklas |
@since 3.3 |
6291 |
11 Jun 13 |
nicklas |
24 |
*/ |
7419 |
03 Nov 17 |
nicklas |
'use strict'; |
6291 |
11 Jun 13 |
nicklas |
26 |
|
6291 |
11 Jun 13 |
nicklas |
var Link = function() |
6291 |
11 Jun 13 |
nicklas |
28 |
{ |
6291 |
11 Jun 13 |
nicklas |
var link = {}; |
6291 |
11 Jun 13 |
nicklas |
var internal = {}; |
6291 |
11 Jun 13 |
nicklas |
31 |
|
6291 |
11 Jun 13 |
nicklas |
// Action value for items removed from the list |
6291 |
11 Jun 13 |
nicklas |
link.REMOVE = -1; |
6291 |
11 Jun 13 |
nicklas |
// Action value for items already in the list |
6291 |
11 Jun 13 |
nicklas |
link.EXISTING = 0; |
6291 |
11 Jun 13 |
nicklas |
// Action value for items added to the list |
6291 |
11 Jun 13 |
nicklas |
link.ADD = 1; |
6291 |
11 Jun 13 |
nicklas |
38 |
|
6291 |
11 Jun 13 |
nicklas |
39 |
/** |
6291 |
11 Jun 13 |
nicklas |
Add a new section with optional initial items to a list. |
6291 |
11 Jun 13 |
nicklas |
The section parameter is an object with the 'itemType' |
6291 |
11 Jun 13 |
nicklas |
property set to an ITEM value and optionally a 'name' property |
6291 |
11 Jun 13 |
nicklas |
if a header is desired. |
6291 |
11 Jun 13 |
nicklas |
44 |
|
6291 |
11 Jun 13 |
nicklas |
@param list The id or <select> list object |
6291 |
11 Jun 13 |
nicklas |
@param section A section object |
6291 |
11 Jun 13 |
nicklas |
@param initialItems Optional array with initial item data |
6291 |
11 Jun 13 |
nicklas |
@param initialAction Action value for items added to the list, |
6291 |
11 Jun 13 |
nicklas |
if not specified, 0 is assumed |
6291 |
11 Jun 13 |
nicklas |
50 |
*/ |
6291 |
11 Jun 13 |
nicklas |
link.addSection = function(list, section, initialItems, initialAction) |
6291 |
11 Jun 13 |
nicklas |
52 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6291 |
11 Jun 13 |
nicklas |
list.numSections = list.numSections ? 1 : list.numSections+1; |
6291 |
11 Jun 13 |
nicklas |
var itemType = section.itemType; |
6291 |
11 Jun 13 |
nicklas |
56 |
|
6291 |
11 Jun 13 |
nicklas |
// Add a section header if a section name is specified |
6291 |
11 Jun 13 |
nicklas |
if (section.name) |
6291 |
11 Jun 13 |
nicklas |
59 |
{ |
6291 |
11 Jun 13 |
nicklas |
var option = new Option('› '+section.name, itemType); |
6291 |
11 Jun 13 |
nicklas |
option.className = 'selectoptionheader'; |
6291 |
11 Jun 13 |
nicklas |
option.section = section; |
6291 |
11 Jun 13 |
nicklas |
list[list.length] = option; |
6291 |
11 Jun 13 |
nicklas |
64 |
} |
6291 |
11 Jun 13 |
nicklas |
65 |
|
6291 |
11 Jun 13 |
nicklas |
// Add initial items to the section |
6291 |
11 Jun 13 |
nicklas |
if (initialItems) |
6291 |
11 Jun 13 |
nicklas |
68 |
{ |
6291 |
11 Jun 13 |
nicklas |
if (!initialAction) initialAction = Link.EXISTING; |
6291 |
11 Jun 13 |
nicklas |
for (var i = 0; i < initialItems.length; i++) |
6291 |
11 Jun 13 |
nicklas |
71 |
{ |
6291 |
11 Jun 13 |
nicklas |
var item = initialItems[i]; |
6291 |
11 Jun 13 |
nicklas |
// Initialize item data |
6291 |
11 Jun 13 |
nicklas |
item.itemType = itemType; |
6291 |
11 Jun 13 |
nicklas |
item.action = initialAction; |
6291 |
11 Jun 13 |
nicklas |
item.originalValue = item.value; |
6291 |
11 Jun 13 |
nicklas |
77 |
|
6291 |
11 Jun 13 |
nicklas |
// Create new <select> option |
6291 |
11 Jun 13 |
nicklas |
var option = new Option(item.name, item.itemType+'.'+item.id); |
6291 |
11 Jun 13 |
nicklas |
// cross-reference link |
6291 |
11 Jun 13 |
nicklas |
option.item = item; |
6291 |
11 Jun 13 |
nicklas |
item.option = option; |
6291 |
11 Jun 13 |
nicklas |
83 |
|
6291 |
11 Jun 13 |
nicklas |
list[list.length] = option; |
6291 |
11 Jun 13 |
nicklas |
list.knownItems[list.knownItems.length] = item; |
6291 |
11 Jun 13 |
nicklas |
list.itemsById[item.itemType+'.'+item.id] = item; |
6291 |
11 Jun 13 |
nicklas |
87 |
} |
6291 |
11 Jun 13 |
nicklas |
88 |
} |
6291 |
11 Jun 13 |
nicklas |
89 |
} |
6291 |
11 Jun 13 |
nicklas |
90 |
|
6291 |
11 Jun 13 |
nicklas |
91 |
/** |
6291 |
11 Jun 13 |
nicklas |
Add an item to the list |
6291 |
11 Jun 13 |
nicklas |
93 |
*/ |
6291 |
11 Jun 13 |
nicklas |
link.addItem = function(list, itemType, item) |
6291 |
11 Jun 13 |
nicklas |
95 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6291 |
11 Jun 13 |
nicklas |
97 |
|
6291 |
11 Jun 13 |
nicklas |
var existing = list.itemsById[itemType+'.'+item.id]; |
6291 |
11 Jun 13 |
nicklas |
if (!existing) |
6291 |
11 Jun 13 |
nicklas |
100 |
{ |
6412 |
04 Feb 14 |
nicklas |
// New item (cloning is needed in case 'item' is created from another window) |
6412 |
04 Feb 14 |
nicklas |
item = { 'id': item.id, |
6412 |
04 Feb 14 |
nicklas |
'name': item.name, |
6412 |
04 Feb 14 |
nicklas |
'itemType': itemType, |
6412 |
04 Feb 14 |
nicklas |
'action': link.ADD, |
6412 |
04 Feb 14 |
nicklas |
'value': item.value, |
7030 |
25 Nov 15 |
nicklas |
'originalValue': item.originalValue != undefined ? item.originalValue : item.value |
6412 |
04 Feb 14 |
nicklas |
108 |
}; |
6291 |
11 Jun 13 |
nicklas |
109 |
|
6305 |
09 Aug 13 |
nicklas |
var option = new Option(item.name, item.itemType+'.'+item.id, true, true); |
6291 |
11 Jun 13 |
nicklas |
option.item = item; |
6291 |
11 Jun 13 |
nicklas |
item.option = option; |
6291 |
11 Jun 13 |
nicklas |
113 |
|
6291 |
11 Jun 13 |
nicklas |
var insertIndex = internal.getInsertIndex(list, itemType); |
6291 |
11 Jun 13 |
nicklas |
Forms.addListOption(list, insertIndex, option); |
6291 |
11 Jun 13 |
nicklas |
list.knownItems[list.knownItems.length] = item; |
6291 |
11 Jun 13 |
nicklas |
list.itemsById[item.itemType+'.'+item.id] = item; |
6291 |
11 Jun 13 |
nicklas |
118 |
} |
6291 |
11 Jun 13 |
nicklas |
else if (existing.option) |
6291 |
11 Jun 13 |
nicklas |
120 |
{ |
6291 |
11 Jun 13 |
nicklas |
// Selected item is already in the list |
6291 |
11 Jun 13 |
nicklas |
// No change, except marking it as selected |
6291 |
11 Jun 13 |
nicklas |
existing.option.selected = true; |
6291 |
11 Jun 13 |
nicklas |
124 |
} |
6291 |
11 Jun 13 |
nicklas |
else |
6291 |
11 Jun 13 |
nicklas |
126 |
{ |
6291 |
11 Jun 13 |
nicklas |
// Selected item is known, but not in the list |
6291 |
11 Jun 13 |
nicklas |
existing.action++; |
6300 |
19 Jun 13 |
nicklas |
existing.value = item.value; |
6291 |
11 Jun 13 |
nicklas |
130 |
|
6305 |
09 Aug 13 |
nicklas |
var option = new Option(existing.name, existing.itemType+'.'+existing.id, true, true); |
6291 |
11 Jun 13 |
nicklas |
option.item = existing; |
6291 |
11 Jun 13 |
nicklas |
existing.option = option; |
6291 |
11 Jun 13 |
nicklas |
134 |
|
6291 |
11 Jun 13 |
nicklas |
var insertIndex = internal.getInsertIndex(list, itemType); |
6291 |
11 Jun 13 |
nicklas |
Forms.addListOption(list, insertIndex, option); |
6291 |
11 Jun 13 |
nicklas |
137 |
} |
6291 |
11 Jun 13 |
nicklas |
138 |
} |
6291 |
11 Jun 13 |
nicklas |
139 |
|
6291 |
11 Jun 13 |
nicklas |
140 |
/** |
6294 |
13 Jun 13 |
nicklas |
Remove selected items from the list. |
6294 |
13 Jun 13 |
nicklas |
@param list The ID or element of the list to remove items from |
6294 |
13 Jun 13 |
nicklas |
@param addToList The ID or element of another list that the removed |
6294 |
13 Jun 13 |
nicklas |
items should be added to |
6291 |
11 Jun 13 |
nicklas |
145 |
*/ |
6294 |
13 Jun 13 |
nicklas |
link.removeSelected = function(list, addToList) |
6291 |
11 Jun 13 |
nicklas |
147 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6294 |
13 Jun 13 |
nicklas |
var sorted = 0; |
6294 |
13 Jun 13 |
nicklas |
if (addToList) |
6294 |
13 Jun 13 |
nicklas |
151 |
{ |
6294 |
13 Jun 13 |
nicklas |
addToList = Doc.element(addToList); |
6294 |
13 Jun 13 |
nicklas |
sorted = Data.get(addToList, 'is-sorted'); |
6294 |
13 Jun 13 |
nicklas |
154 |
} |
6294 |
13 Jun 13 |
nicklas |
155 |
|
6291 |
11 Jun 13 |
nicklas |
for (var i = 0; i < list.length; i++) |
6291 |
11 Jun 13 |
nicklas |
157 |
{ |
6291 |
11 Jun 13 |
nicklas |
var item = list[i].item; |
6291 |
11 Jun 13 |
nicklas |
if (list[i].selected && item && item.id) |
6291 |
11 Jun 13 |
nicklas |
160 |
{ |
6294 |
13 Jun 13 |
nicklas |
if (addToList) |
6294 |
13 Jun 13 |
nicklas |
162 |
{ |
6294 |
13 Jun 13 |
nicklas |
var option = new Option(item.name, item.id, false, true); |
6294 |
13 Jun 13 |
nicklas |
var insertIndex = sorted ? Forms.getInsertIndexOfSortedList(addToList, option.text) : addToList.length; |
6294 |
13 Jun 13 |
nicklas |
Forms.addListOption(addToList, insertIndex, option); |
6294 |
13 Jun 13 |
nicklas |
166 |
} |
6291 |
11 Jun 13 |
nicklas |
item.action--; |
6880 |
21 Apr 15 |
nicklas |
item.value = item.originalValue; |
6291 |
11 Jun 13 |
nicklas |
item.option = null; |
7030 |
25 Nov 15 |
nicklas |
Events.sendCustomEvent(list, 'base-removed', list[i]); |
6291 |
11 Jun 13 |
nicklas |
list[i--] = null; |
6291 |
11 Jun 13 |
nicklas |
172 |
} |
7419 |
03 Nov 17 |
nicklas |
173 |
} |
6291 |
11 Jun 13 |
nicklas |
174 |
} |
6291 |
11 Jun 13 |
nicklas |
175 |
|
6291 |
11 Jun 13 |
nicklas |
176 |
/** |
6294 |
13 Jun 13 |
nicklas |
Remove all items from the given list |
6294 |
13 Jun 13 |
nicklas |
@param list The ID or element of the list to remove items from |
6294 |
13 Jun 13 |
nicklas |
179 |
*/ |
6294 |
13 Jun 13 |
nicklas |
link.removeAll = function(list) |
6294 |
13 Jun 13 |
nicklas |
181 |
{ |
6294 |
13 Jun 13 |
nicklas |
list = Doc.element(list); |
6294 |
13 Jun 13 |
nicklas |
for (var i = 0; i < list.length; i++) |
6294 |
13 Jun 13 |
nicklas |
184 |
{ |
6294 |
13 Jun 13 |
nicklas |
var item = list[i].item; |
6294 |
13 Jun 13 |
nicklas |
if (item && item.id) |
6294 |
13 Jun 13 |
nicklas |
187 |
{ |
6294 |
13 Jun 13 |
nicklas |
item.action--; |
6880 |
21 Apr 15 |
nicklas |
item.value = item.originalValue; |
6294 |
13 Jun 13 |
nicklas |
item.option = null; |
6294 |
13 Jun 13 |
nicklas |
191 |
} |
6294 |
13 Jun 13 |
nicklas |
192 |
} |
6294 |
13 Jun 13 |
nicklas |
list.length = 0; |
6294 |
13 Jun 13 |
nicklas |
194 |
} |
6294 |
13 Jun 13 |
nicklas |
195 |
|
6294 |
13 Jun 13 |
nicklas |
196 |
/** |
6291 |
11 Jun 13 |
nicklas |
Event handler for removing selected items from a list. |
6291 |
11 Jun 13 |
nicklas |
The ID of the selection list should be specified in |
6294 |
13 Jun 13 |
nicklas |
'data-list-id' attribute. Optionally, the removed |
6294 |
13 Jun 13 |
nicklas |
items can be added to another list by setting |
6294 |
13 Jun 13 |
nicklas |
the 'data-remove-to' attribute to the ID of the other list. |
6291 |
11 Jun 13 |
nicklas |
202 |
*/ |
6291 |
11 Jun 13 |
nicklas |
link.removeOnClick = function(event) |
6291 |
11 Jun 13 |
nicklas |
204 |
{ |
6291 |
11 Jun 13 |
nicklas |
var listId = Data.get(event.currentTarget, 'list-id'); |
6294 |
13 Jun 13 |
nicklas |
var moveToListId = Data.get(event.currentTarget, 'remove-to'); |
6294 |
13 Jun 13 |
nicklas |
link.removeSelected(listId || event.currentTarget, moveToListId); |
6291 |
11 Jun 13 |
nicklas |
208 |
} |
6291 |
11 Jun 13 |
nicklas |
209 |
|
6291 |
11 Jun 13 |
nicklas |
210 |
/** |
6291 |
11 Jun 13 |
nicklas |
Event handler that opens a popup for selecting items to add to a |
6291 |
11 Jun 13 |
nicklas |
list. The ID of the selection list should be specified |
6291 |
11 Jun 13 |
nicklas |
in 'data-list-id' attribute, and the type of items to |
6291 |
11 Jun 13 |
nicklas |
select should be specified in 'data-item-type' attribute. |
6291 |
11 Jun 13 |
nicklas |
215 |
*/ |
6291 |
11 Jun 13 |
nicklas |
link.addOnClick = function(event) |
6291 |
11 Jun 13 |
nicklas |
217 |
{ |
6291 |
11 Jun 13 |
nicklas |
var listId = Data.get(event.currentTarget, 'list-id'); |
6291 |
11 Jun 13 |
nicklas |
var itemType = Data.get(event.currentTarget, 'item-type'); |
6291 |
11 Jun 13 |
nicklas |
var exclude = Data.get(event.currentTarget, 'exclude'); |
6306 |
13 Aug 13 |
nicklas |
var filter = Data.get(event.currentTarget, 'filter'); |
6291 |
11 Jun 13 |
nicklas |
222 |
|
6300 |
19 Jun 13 |
nicklas |
var listItems = link.getIdsInList(listId, itemType); |
6291 |
11 Jun 13 |
nicklas |
if (exclude) listItems[listItems.length] = exclude; |
6291 |
11 Jun 13 |
nicklas |
var url = '&exclude='+listItems.join(','); |
6306 |
13 Aug 13 |
nicklas |
if (filter) url += filter; |
6291 |
11 Jun 13 |
nicklas |
Dialogs.selectItem(itemType, listId, 1, url); |
6291 |
11 Jun 13 |
nicklas |
228 |
} |
6291 |
11 Jun 13 |
nicklas |
229 |
|
6291 |
11 Jun 13 |
nicklas |
230 |
/** |
6291 |
11 Jun 13 |
nicklas |
Export all ADD and REMOVE actions to hidden input fields in the same form |
6291 |
11 Jun 13 |
nicklas |
as the list is part of. The hidden form fields are named as '+ITEMTYPE' and |
6291 |
11 Jun 13 |
nicklas |
'-ITEMTYPE' where '+' are for added items and '-' for removed items. |
6291 |
11 Jun 13 |
nicklas |
The value of each hidden field is a comma separated list of the ID values. |
6320 |
11 Sep 13 |
nicklas |
@param list The id or list element |
6320 |
11 Sep 13 |
nicklas |
@param actionPrefix Force the given prefix instead of auto-selecting '+' or '-' |
6291 |
11 Jun 13 |
nicklas |
237 |
*/ |
6320 |
11 Sep 13 |
nicklas |
link.exportActions = function(list, actionPrefix) |
6291 |
11 Jun 13 |
nicklas |
239 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6291 |
11 Jun 13 |
nicklas |
var frm = list.form; |
6291 |
11 Jun 13 |
nicklas |
242 |
|
6291 |
11 Jun 13 |
nicklas |
for (var i = 0; i < list.knownItems.length; i++) |
6291 |
11 Jun 13 |
nicklas |
244 |
{ |
6291 |
11 Jun 13 |
nicklas |
var item = list.knownItems[i]; |
6300 |
19 Jun 13 |
nicklas |
246 |
|
6322 |
11 Sep 13 |
nicklas |
if (item.action != 0 || item.value != item.originalValue) |
6291 |
11 Jun 13 |
nicklas |
248 |
{ |
6320 |
11 Sep 13 |
nicklas |
var prefix = actionPrefix ? actionPrefix : (item.action < 0 ? '-' : '+'); |
6320 |
11 Sep 13 |
nicklas |
var actionKey = prefix + item.itemType; |
6291 |
11 Jun 13 |
nicklas |
if (!frm[actionKey]) |
6291 |
11 Jun 13 |
nicklas |
252 |
{ |
6389 |
07 Jan 14 |
nicklas |
Forms.addHidden(frm, actionKey, item.id); |
6291 |
11 Jun 13 |
nicklas |
254 |
} |
6291 |
11 Jun 13 |
nicklas |
else |
6291 |
11 Jun 13 |
nicklas |
256 |
{ |
6291 |
11 Jun 13 |
nicklas |
frm[actionKey].value += ','+item.id; |
6291 |
11 Jun 13 |
nicklas |
258 |
} |
6322 |
11 Sep 13 |
nicklas |
if (item.value != undefined) |
6300 |
19 Jun 13 |
nicklas |
260 |
{ |
6300 |
19 Jun 13 |
nicklas |
var valueKey = item.itemType+'.'+item.id; |
6300 |
19 Jun 13 |
nicklas |
if (!frm[valueKey]) |
6300 |
19 Jun 13 |
nicklas |
263 |
{ |
6389 |
07 Jan 14 |
nicklas |
Forms.addHidden(frm, valueKey, item.value); |
6300 |
19 Jun 13 |
nicklas |
265 |
} |
6300 |
19 Jun 13 |
nicklas |
else |
6300 |
19 Jun 13 |
nicklas |
267 |
{ |
6300 |
19 Jun 13 |
nicklas |
frm[valueKey].value = item.value; |
6300 |
19 Jun 13 |
nicklas |
269 |
} |
6300 |
19 Jun 13 |
nicklas |
270 |
} |
6291 |
11 Jun 13 |
nicklas |
271 |
} |
6291 |
11 Jun 13 |
nicklas |
272 |
} |
6291 |
11 Jun 13 |
nicklas |
273 |
} |
6291 |
11 Jun 13 |
nicklas |
274 |
|
6291 |
11 Jun 13 |
nicklas |
275 |
/** |
6291 |
11 Jun 13 |
nicklas |
Event handler for the 'base-selected' event sent by popup windows used |
6291 |
11 Jun 13 |
nicklas |
for selecting items.The event.detail object should contain the the following |
6291 |
11 Jun 13 |
nicklas |
properties: 'itemType', 'id' and 'name'. |
6291 |
11 Jun 13 |
nicklas |
279 |
*/ |
6291 |
11 Jun 13 |
nicklas |
link.onSelected = function(event) |
6291 |
11 Jun 13 |
nicklas |
281 |
{ |
6291 |
11 Jun 13 |
nicklas |
link.addItem(event.currentTarget, event.detail.itemType, event.detail); |
6291 |
11 Jun 13 |
nicklas |
283 |
} |
6291 |
11 Jun 13 |
nicklas |
284 |
|
6291 |
11 Jun 13 |
nicklas |
285 |
/** |
6291 |
11 Jun 13 |
nicklas |
Get an array with the ID values of all items |
6291 |
11 Jun 13 |
nicklas |
in the list. |
6291 |
11 Jun 13 |
nicklas |
@param list The ID or element of a selection list |
6291 |
11 Jun 13 |
nicklas |
@param itemType Required, the type of items to get the id for |
6291 |
11 Jun 13 |
nicklas |
290 |
*/ |
6300 |
19 Jun 13 |
nicklas |
link.getIdsInList = function(list, itemType) |
6291 |
11 Jun 13 |
nicklas |
292 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6291 |
11 Jun 13 |
nicklas |
var ids = []; |
6291 |
11 Jun 13 |
nicklas |
for (var i = 0; i < list.length; i++) |
6291 |
11 Jun 13 |
nicklas |
296 |
{ |
6291 |
11 Jun 13 |
nicklas |
var item = list[i].item; |
6291 |
11 Jun 13 |
nicklas |
if (item && item.itemType == itemType && item.id) |
6291 |
11 Jun 13 |
nicklas |
299 |
{ |
6291 |
11 Jun 13 |
nicklas |
ids[ids.length] = item.id; |
6291 |
11 Jun 13 |
nicklas |
301 |
} |
6291 |
11 Jun 13 |
nicklas |
302 |
} |
6291 |
11 Jun 13 |
nicklas |
return ids; |
6291 |
11 Jun 13 |
nicklas |
304 |
} |
6291 |
11 Jun 13 |
nicklas |
305 |
|
6291 |
11 Jun 13 |
nicklas |
306 |
/** |
6291 |
11 Jun 13 |
nicklas |
Get the index where an item of the given type should be inserted into |
6291 |
11 Jun 13 |
nicklas |
the list. |
6291 |
11 Jun 13 |
nicklas |
309 |
*/ |
6291 |
11 Jun 13 |
nicklas |
internal.getInsertIndex = function(list, itemType) |
6291 |
11 Jun 13 |
nicklas |
311 |
{ |
6291 |
11 Jun 13 |
nicklas |
list = Doc.element(list); |
6291 |
11 Jun 13 |
nicklas |
if (list.numSections == 1) return list.length; |
6291 |
11 Jun 13 |
nicklas |
314 |
|
6291 |
11 Jun 13 |
nicklas |
var insertIndex = -1; |
6291 |
11 Jun 13 |
nicklas |
for (var i = 0; i < list.length; i++) |
6291 |
11 Jun 13 |
nicklas |
317 |
{ |
6291 |
11 Jun 13 |
nicklas |
var itemOrSection = list[i].item || list[i].section; |
6291 |
11 Jun 13 |
nicklas |
if (itemOrSection && itemOrSection.itemType == itemType) |
6291 |
11 Jun 13 |
nicklas |
320 |
{ |
6291 |
11 Jun 13 |
nicklas |
insertIndex = i+1; |
6291 |
11 Jun 13 |
nicklas |
322 |
} |
6291 |
11 Jun 13 |
nicklas |
else if (insertIndex >= 0) |
6291 |
11 Jun 13 |
nicklas |
324 |
{ |
6291 |
11 Jun 13 |
nicklas |
return insertIndex; |
6291 |
11 Jun 13 |
nicklas |
326 |
} |
6291 |
11 Jun 13 |
nicklas |
327 |
} |
6291 |
11 Jun 13 |
nicklas |
return list.length; |
6291 |
11 Jun 13 |
nicklas |
329 |
} |
6291 |
11 Jun 13 |
nicklas |
330 |
|
6291 |
11 Jun 13 |
nicklas |
331 |
/** |
6291 |
11 Jun 13 |
nicklas |
Element initializer method that add event handler to 'add' and 'remove' |
6291 |
11 Jun 13 |
nicklas |
buttons as well as the link container itself. |
6291 |
11 Jun 13 |
nicklas |
334 |
*/ |
6291 |
11 Jun 13 |
nicklas |
internal.addLinkHandlers = function(element, autoInit) |
6291 |
11 Jun 13 |
nicklas |
336 |
{ |
6291 |
11 Jun 13 |
nicklas |
if (autoInit == 'add-link') |
6291 |
11 Jun 13 |
nicklas |
338 |
{ |
6291 |
11 Jun 13 |
nicklas |
Buttons.addClickHandler(element, Link.addOnClick); |
6291 |
11 Jun 13 |
nicklas |
340 |
} |
6291 |
11 Jun 13 |
nicklas |
else if (autoInit == 'remove-link') |
6291 |
11 Jun 13 |
nicklas |
342 |
{ |
6291 |
11 Jun 13 |
nicklas |
Buttons.addClickHandler(element, link.removeOnClick); |
6291 |
11 Jun 13 |
nicklas |
344 |
} |
6291 |
11 Jun 13 |
nicklas |
else if (autoInit == 'link-container') |
6291 |
11 Jun 13 |
nicklas |
346 |
{ |
6291 |
11 Jun 13 |
nicklas |
internal.initializeLinkContainer(element); |
6291 |
11 Jun 13 |
nicklas |
Events.addEventHandler(element, 'base-selected', link.onSelected); |
6291 |
11 Jun 13 |
nicklas |
Events.addEventHandler(element, 'dblclick', link.removeOnClick); |
6291 |
11 Jun 13 |
nicklas |
350 |
} |
6291 |
11 Jun 13 |
nicklas |
351 |
} |
6291 |
11 Jun 13 |
nicklas |
352 |
|
6291 |
11 Jun 13 |
nicklas |
353 |
/** |
6291 |
11 Jun 13 |
nicklas |
Initialize the link container by reading data from |
6291 |
11 Jun 13 |
nicklas |
the 'data-initial-items' attribute. This should be |
6291 |
11 Jun 13 |
nicklas |
an array with 'section' objects, each one defining the |
6291 |
11 Jun 13 |
nicklas |
properties: 'itemType', 'name' (optional), 'items' (=array |
6291 |
11 Jun 13 |
nicklas |
of initial items; optional) |
6291 |
11 Jun 13 |
nicklas |
359 |
*/ |
6291 |
11 Jun 13 |
nicklas |
internal.initializeLinkContainer = function(list) |
6291 |
11 Jun 13 |
nicklas |
361 |
{ |
6291 |
11 Jun 13 |
nicklas |
list.knownItems = []; |
6291 |
11 Jun 13 |
nicklas |
list.itemsById = []; |
6291 |
11 Jun 13 |
nicklas |
364 |
|
6291 |
11 Jun 13 |
nicklas |
var sections = Data.json(list, 'initial-items'); |
6291 |
11 Jun 13 |
nicklas |
if (!sections) return; |
6291 |
11 Jun 13 |
nicklas |
367 |
|
6291 |
11 Jun 13 |
nicklas |
var initialAction = Data.int(list, 'initial-action', Link.EXISTING); |
6291 |
11 Jun 13 |
nicklas |
for (var s = 0; s < sections.length; s++) |
6291 |
11 Jun 13 |
nicklas |
370 |
{ |
6291 |
11 Jun 13 |
nicklas |
var section = sections[s]; |
6291 |
11 Jun 13 |
nicklas |
link.addSection(list, section, section.items, initialAction); |
6291 |
11 Jun 13 |
nicklas |
373 |
} |
6291 |
11 Jun 13 |
nicklas |
374 |
} |
6291 |
11 Jun 13 |
nicklas |
375 |
|
6291 |
11 Jun 13 |
nicklas |
376 |
|
6291 |
11 Jun 13 |
nicklas |
Doc.addElementInitializer(internal.addLinkHandlers); |
6291 |
11 Jun 13 |
nicklas |
378 |
|
6291 |
11 Jun 13 |
nicklas |
return link; |
6291 |
11 Jun 13 |
nicklas |
380 |
}(); |