2017-01-31 00:47:18 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
RED.stack = (function() {
|
|
|
|
function createStack(options) {
|
|
|
|
var container = options.container;
|
2017-09-20 11:30:07 +02:00
|
|
|
container.addClass("red-ui-stack");
|
|
|
|
var contentHeight = 0;
|
2017-01-31 00:47:18 +01:00
|
|
|
var entries = [];
|
|
|
|
|
2017-04-07 00:17:06 +02:00
|
|
|
var visible = true;
|
2017-10-09 13:10:00 +02:00
|
|
|
// TODO: make this a singleton function - and watch out for stacks no longer
|
|
|
|
// in the DOM
|
2017-09-20 11:30:07 +02:00
|
|
|
var resizeStack = function() {
|
|
|
|
if (entries.length > 0) {
|
2017-10-09 13:10:00 +02:00
|
|
|
var headerHeight = 0;
|
|
|
|
entries.forEach(function(entry) {
|
|
|
|
headerHeight += entry.header.outerHeight();
|
|
|
|
});
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
var height = container.innerHeight();
|
2017-10-09 13:10:00 +02:00
|
|
|
contentHeight = height - headerHeight - (entries.length-1);
|
2017-09-20 11:30:07 +02:00
|
|
|
entries.forEach(function(e) {
|
|
|
|
e.contentWrap.height(contentHeight);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.fill && options.singleExpanded) {
|
|
|
|
$(window).resize(resizeStack);
|
|
|
|
$(window).focus(resizeStack);
|
|
|
|
}
|
2017-01-31 00:47:18 +01:00
|
|
|
return {
|
|
|
|
add: function(entry) {
|
|
|
|
entries.push(entry);
|
2017-04-07 00:17:06 +02:00
|
|
|
entry.container = $('<div class="palette-category">').appendTo(container);
|
|
|
|
if (!visible) {
|
|
|
|
entry.container.hide();
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
var header = $('<div class="palette-header"></div>').appendTo(entry.container);
|
2017-09-20 11:30:07 +02:00
|
|
|
entry.header = header;
|
|
|
|
entry.contentWrap = $('<div></div>',{style:"position:relative"}).appendTo(entry.container);
|
|
|
|
if (options.fill) {
|
|
|
|
entry.contentWrap.css("height",contentHeight);
|
|
|
|
}
|
|
|
|
entry.content = $('<div></div>').appendTo(entry.contentWrap);
|
2017-04-07 00:17:06 +02:00
|
|
|
if (entry.collapsible !== false) {
|
|
|
|
header.click(function() {
|
|
|
|
if (options.singleExpanded) {
|
|
|
|
if (!entry.isExpanded()) {
|
|
|
|
for (var i=0;i<entries.length;i++) {
|
|
|
|
if (entries[i].isExpanded()) {
|
|
|
|
entries[i].collapse();
|
|
|
|
}
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
entry.expand();
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
} else {
|
|
|
|
entry.toggle();
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
});
|
|
|
|
var icon = $('<i class="fa fa-angle-down"></i>').appendTo(header);
|
|
|
|
|
|
|
|
if (entry.expanded) {
|
2017-09-20 11:30:07 +02:00
|
|
|
entry.container.addClass("palette-category-expanded");
|
2017-04-07 00:17:06 +02:00
|
|
|
icon.addClass("expanded");
|
2017-01-31 00:47:18 +01:00
|
|
|
} else {
|
2017-09-20 11:30:07 +02:00
|
|
|
entry.contentWrap.hide();
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
} else {
|
2017-09-20 11:30:07 +02:00
|
|
|
$('<i style="opacity: 0.5;" class="fa fa-angle-down expanded"></i>').appendTo(header);
|
2017-04-07 00:17:06 +02:00
|
|
|
header.css("cursor","default");
|
|
|
|
}
|
|
|
|
entry.title = $('<span></span>').html(entry.title).appendTo(header);
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-31 00:47:18 +01:00
|
|
|
entry.toggle = function() {
|
|
|
|
if (entry.isExpanded()) {
|
|
|
|
entry.collapse();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
entry.expand();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
entry.expand = function() {
|
|
|
|
if (!entry.isExpanded()) {
|
2017-02-06 14:23:42 +01:00
|
|
|
if (entry.onexpand) {
|
|
|
|
entry.onexpand.call(entry);
|
|
|
|
}
|
2017-01-31 00:47:18 +01:00
|
|
|
icon.addClass("expanded");
|
2017-09-20 11:30:07 +02:00
|
|
|
entry.container.addClass("palette-category-expanded");
|
|
|
|
entry.contentWrap.slideDown(200);
|
2017-01-31 00:47:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
entry.collapse = function() {
|
|
|
|
if (entry.isExpanded()) {
|
|
|
|
icon.removeClass("expanded");
|
2017-09-20 11:30:07 +02:00
|
|
|
entry.container.removeClass("palette-category-expanded");
|
|
|
|
entry.contentWrap.slideUp(200);
|
2017-01-31 00:47:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
entry.isExpanded = function() {
|
2017-09-20 11:30:07 +02:00
|
|
|
return entry.container.hasClass("palette-category-expanded");
|
2017-01-31 00:47:18 +01:00
|
|
|
};
|
2017-09-20 11:30:07 +02:00
|
|
|
if (options.fill && options.singleExpanded) {
|
|
|
|
resizeStack();
|
|
|
|
}
|
2017-01-31 00:47:18 +01:00
|
|
|
return entry;
|
|
|
|
},
|
|
|
|
|
2017-04-07 00:17:06 +02:00
|
|
|
hide: function() {
|
|
|
|
visible = false;
|
|
|
|
entries.forEach(function(entry) {
|
|
|
|
entry.container.hide();
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function() {
|
|
|
|
visible = true;
|
|
|
|
entries.forEach(function(entry) {
|
|
|
|
entry.container.show();
|
|
|
|
});
|
|
|
|
return this;
|
2017-09-20 11:30:07 +02:00
|
|
|
},
|
|
|
|
resize: function() {
|
|
|
|
if (resizeStack) {
|
|
|
|
resizeStack();
|
|
|
|
}
|
2017-04-07 00:17:06 +02:00
|
|
|
}
|
2017-01-31 00:47:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
create: createStack
|
|
|
|
}
|
|
|
|
})();
|