/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
/*
* Prerequisites: common.js
* mootools.v1.11.js
*/
// Picker class definition
var AlfPicker = new Class(
{
/* id of the picker */
id: null,
/* variable name being used */
varName: null,
/* form Id to submit when selection complete */
formClientId: null,
/* the item the picker will start with */
startId: null,
/* list of items currently selected */
selected: null,
/* list of items pre-selected */
preselected: null,
/* the current parent being shown */
parent: null,
/* the list of items currently displayed */
items: [],
/* parent stack for the Navigate Up action*/
stack: [],
/* row type toggle */
oddRow: true,
/* ajax service call to retrieve data */
service: null,
/* default icon to use if not provided by the associated service */
defaultIcon: null,
/* single selection mode flag */
singleSelect: false,
/* initial display style of the outer div */
initialDisplayStyle: null,
/* addition service request attributes if any */
requestAttributes: null,
initialize: function(id, varName, service, formClientId, singleSelect)
{
this.id = id;
this.varName = varName;
this.service = service;
this.formClientId = formClientId;
if (singleSelect != undefined)
{
this.singleSelect = singleSelect;
}
this.selected = [];
this.preselected = [];
},
setDefaultIcon: function(icon)
{
this.defaultIcon = icon;
},
setStartId: function(id)
{
this.startId = id;
},
setSelectedItems: function(jsonString)
{
this.preselected = Json.evaluate(jsonString);
},
setRequestAttributes: function(attrs)
{
this.requestAttributes = attrs;
},
showSelector: function()
{
// init selector state
this.selected = [];
this.stack = [];
this.initialDisplayStyle = $(this.id + "-noitems").getStyle("display");
$(this.id + "-selector").setStyle("display", "block");
$(this.id + "-selected").empty();
$(this.id + "-selected").setStyle("display", "block");
$(this.id + "-noitems").setStyle("display", "none");
if (this.singleSelect)
{
$(this.id + "-finish").setStyle("display", "none");
}
this.preselected.each(function(item, i)
{
this.addSelectedItem(item);
}, this);
// first ajax request for the children of the start item
this.getNodeData(this.startId, null, this.populateChildren);
},
childClicked: function(index)
{
this.hidePanels();
var item = this.items[index];
// add an extra property to record the scroll position for this item
item.scrollpos = $(this.id + "-results-list").scrollTop;
this.stack.push(item); // ready for the breadcrumb redraw after the child data request
this.getNodeData(item.id, null, this.populateChildren)
},
upClicked: function()
{
this.hidePanels();
// pop the parent off
var parent = this.stack.pop();
// peek for the grandparent - we may not have a grandparent (depending on start location) - so use the
// getNodeData(parent, child, ...) call to pass in the child rather than the parent and let server calculate it
var grandParent = null;
if (this.stack.length != 0)
{
grandParent = this.stack[this.stack.length-1];
}
this.getNodeData(grandParent != null ? grandParent.id : null, grandParent == null ? parent.id : null, this.populateChildren, parent.scrollpos);
},
addItem: function(index)
{
var item;
if (index != -1)
{
item = this.items[index];
}
else
{
item = this.parent;
}
if (this.singleSelect)
{
this.selected.push(item);
this.doneClicked();
}
else
{
this.addSelectedItem(item);
// hide the Add button as this item is now added
$(this.id + "-add-" + item.id).setStyle("display", "none");
}
},
addSelectedItem: function(item)
{
// add item to list of selected items
this.selected.push(item);
// add the item to list outside the selector
var itemId = this.id + "-sel-" + item.id;
var itemDiv = new Element("div", {"id": itemId, "class": "pickerSelectedItem"});
var itemSpan = new Element("span", {"class": "pickerSelectedItemText"});
itemSpan.appendText(item.name);
itemSpan.injectInside(itemDiv);
var actionSpan = new Element("span", {"class": "pickerSelectedItemAction"});
var actionScript = "javascript:" + this.varName + ".delItem('" + item.id + "');";
var actionLink = new Element("a", {"href": actionScript});
var deleteIcon = new Element("img", {"src": getContextPath() + "/images/icons/minus.gif", "class": "pickerSelectedIcon",
"border": 0, "title": "Remove", "alt": "Remove"});
deleteIcon.injectInside(actionLink);
actionLink.injectInside(actionSpan);
actionSpan.injectInside(itemDiv);
// add mouse enter/leave enter to toggle delete icon (and toggle margin on outer div)
itemDiv.addEvent('mouseenter', function(e) {
$E('.pickerSelectedIcon', itemDiv).setStyle("opacity", 1);
});
itemDiv.addEvent('mouseleave', function(e) {
$E('.pickerSelectedIcon', itemDiv).setStyle("opacity", 0);
});
// add the item to the main selected item div
itemDiv.injectInside($(this.id + "-selected"));
// set the background image now the itemdiv has been added to the DOM (for IE)
itemDiv.setStyle("background-image", "url(" + getContextPath() + item.icon + ")");
// set opacity the style now the item has been added to the DOM (for IE)
$E('.pickerSelectedIcon', itemDiv).setStyle("opacity", 0);
// apply the effect
var fx = new Fx.Styles(itemDiv, {duration: 1000, wait: false, transition: Fx.Transitions.Quad.easeOut});
fx.start({'background-color': ['#faf7ce', '#ffffff']});
},
delItem: function(itemId)
{
// remove item from the selected items list
for (i=0; i b.name) ? 1 : 0));
}
else
{
return (a.selectable == false) ? -1 : 1;
}
}
});