var iSharePoint = {
jQuery : $,
settings : {
columns : '.column',
zones : '',
widgetSelector: '.widget',
handleSelector: '.widget-head',
contentSelector: '.widget-content',
editSelector: '.widget-edit',
toolboxSelector: '#toolbox',
widgetPlaceholder: 'widget-placeholder',
badgeSelector: '.badge',
positionCookie: 'widgetPositionsCookie',
minimiseCookie: 'widgetMinimisedCookie',
colorCookie: 'widgetColorCookie'
},
init : function () {
settings = this.settings;
this.defineZones();
this.buttonFunctions();
this.makeSortable();
this.loadStateFromCookie();
this.activateColours();
},
defineZones : function () {
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
var temp = "";
$j(settings.columns).each(function () {
temp += "#" + $j(this).attr('id') + ",";
});
settings.zones = temp.substring(0, temp.length-1);
},
loadStateFromCookie : function() {
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
var PositionStates = getCookie(settings.positionCookie);
var MinimiseStates = getCookie(settings.minimiseCookie);
var ColorStates = getCookie(settings.colorCookie);
this.ProcessWidgetPositionData(PositionStates);
this.ProcessMinimiseData(MinimiseStates);
this.ProcessWidgetColorData(ColorStates);
updateBadge();
},
ProcessWidgetPositionData : function(PositionStates){
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
// Configure cookie
if (PositionStates) {
//Get the Array of Orders
var order = PositionStates.split('|');
//Get the Array of Zones
var zonearray = settings.zones.split(',');
for(o in order)
{
//Position the boxes in the correct locations
$j(settings.widgetSelector).each(function () {
//Loops through each PositionState in order object
if (order[o].search($j(this).attr('id')) != -1)
{
$j(zonearray[o]).append($j(this));
}
});
//Reorder the boxes in each zone
if (order[o] != "") {
var _workOrder = order[o].split(',');
for (i = 0; i < _workOrder.length; i++)
{
$j(zonearray[o]).append($j('#' + _workOrder[i]));
}
}
}
}
},
ProcessMinimiseData : function(MinimiseStates){
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
if (MinimiseStates != null) {
MinimiseStates = MinimiseStates.split(',');
for (i = 0; i < MinimiseStates.length; i++) {
var id = "#" + MinimiseStates[i];
$j(id).find(settings.contentSelector).slideToggle("slow");
$j(id).toggleClass("minimised");
}
}
},
ProcessWidgetColorData : function(ColorStates){
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
if (ColorStates != null) {
ColorStates = ColorStates.split(',');
for (i = 0; i < ColorStates.length; i++) {
var id = "#" + ColorStates[i].split('|')[0];
var color = ColorStates[i].split('|')[1];
$j(id).find('.iColorPicker').val(color);
$j(id).find('.iColorPicker').css("background-color",color);
$j(id).find(settings.handleSelector).css('backgroundColor', color);
}
}
},
activateColours : function () {
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
$j(".pickerTable").click(function () {
$j('.iColorPicker').each(function () {
var str = "";
str += $j(this).val();
$j(this).parents(settings.widgetSelector).find(settings.handleSelector).css('backgroundColor', str);
});
SaveState();
});
},
makeSortable : function () {
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
$j(settings.columns).sortable({
connectWith: $j(settings.columns),
handle: settings.handleSelector,
cursor: 'move',
revert: true,
placeholder: settings.widgetPlaceholder,
forcePlaceholderSize: true,
revert: 300,
delay: 100,
opacity: 0.8,
start: function (event, ui) {
$j(settings.columns).css("background-color","#e7e5e5");
},
stop: function (event, ui) {
$j(settings.columns).css("background-color","transparent");
$j(settings.toolboxSelector).css("background-color","#fff");
SaveState();
}
})
},
buttonFunctions : function() {
var iSharePoint = this,
$j = jQuery.noConflict(),
settings = this.settings;
$j('.slide').mousedown(function (e) {
// Stop event bubbling:
e.stopPropagation();
}).click(function () {
//To Do - Slide toolbox down
$j(settings.toolboxSelector).slideToggle("slow");
// Return false, prevent default action:
return false;
})
$j('.reset').mousedown(function (e) {
// Stop event bubbling:
e.stopPropagation();
}).click(function () {
if(confirm('Are you sure you want to reset your layout?')) {
deleteCookie(settings.positionCookie);
deleteCookie(settings.minimiseCookie);
deleteCookie(settings.colorCookie);
$j('.iColorPicker').each(function () {
$j(this).val('');
});
location.reload();
}
// Return false, prevent default action:
return false;
})
$j('.edit').mousedown(function (e) {
e.stopPropagation();
}).click(function () {
//To Do - What to do when clicked first time
$j(this).parents(settings.widgetSelector).find(settings.editSelector).slideToggle("slow");
return false;
})
// Create new anchor element with class of 'remove':
$j('.remove').mousedown(function (e) {
// Stop event bubbling:
e.stopPropagation();
}).click(function () {
// Confirm action - make sure that the user is sure:
if(confirm('Please confirm the removal of the widget. This can be restored from the widget toolbox at the bottom of the screen.')) {
$j(this).parents(settings.widgetSelector).appendTo(settings.toolboxSelector);
SaveState();
}
});
$j('.collapse').mousedown(function (e) {
e.stopPropagation();
}).click(function () {
$j(this).parents(settings.widgetSelector).find(settings.contentSelector).slideToggle("slow");
$j(this).parents(settings.widgetSelector).toggleClass("minimised");
SaveState();
return false;
})
},
};
function updateBadge()
{
$j = jQuery.noConflict(),
$j(iSharePoint.settings.badgeSelector).text($j(iSharePoint.settings.toolboxSelector + " > " + iSharePoint.settings.widgetSelector).size());
}
function SaveState()
{
//Place SharePoint in No Conflict Mode
var $j = jQuery.noConflict();
//Update the Badge
updateBadge();
// Create/write a cookie and store it for 1 day
setCookie(iSharePoint.settings.positionCookie, GetPositionState(), 1);
setCookie(iSharePoint.settings.minimiseCookie, GetMinimisedState(), 1);
setCookie(iSharePoint.settings.colorCookie, GetWidgetColor(), 1);
}
function GetPositionState()
{
//Get a local array of the Zones
var zones = iSharePoint.settings.zones.split(',');
//Holder variable for widget Positions
var widgetPositions = "";
//Loop through each zone to create a pipe delimited string
for(z in zones)
{
//Get the array of the current zone as CSV
widgetPositions += $j(zones[z]).sortable('toArray') + "|";
}
//Trim the end off the string
widgetPositions = widgetPositions.substring(0, widgetPositions.length-1);
return widgetPositions;
}
function GetMinimisedState()
{
var $j = jQuery.noConflict();
var list = new Array();
$j('.minimised').each(function () {
list.push($j(this).attr('id'));
});
return list;
}
function GetWidgetColor() {
var $j = jQuery.noConflict();
var list = new Array();
$j(iSharePoint.settings.widgetSelector).each(function () {
list.push($j(this).attr('id') + "|" + $j(this).find('.iColorPicker').val());
});
return list;
}
jQuery(document).ready(function(){
var $j = jQuery.noConflict();
$j(iSharePoint.settings.widgetSelector).corner();
updateBadge();
iSharePoint.init();
});