oxjs/source/Ox.UI/js/List/Ox.IconItem.js

181 lines
5.6 KiB
JavaScript
Raw Normal View History

// vim: et:ts=4:sw=4:sts=4:ft=js
2011-05-16 10:49:48 +00:00
/*@
Ox.IconItem <f:Ox.Element> IconItem Object
() -> <f> IconItem Object
(options) -> <f> IconItem Object
(options, self) -> <f> IconItem Object
options <o> Options object
2011-08-06 17:41:01 +00:00
borderRadius <n|0> border radius for icon images
2011-08-07 02:33:26 +00:00
iconHeight <n|128> icon height
iconWidth <n|128> icon width
imageHeight <n|128> icon image height
imageWidth <n|128> icon image width
2011-05-16 10:49:48 +00:00
id <s> element id
info <s> icon info
size <n|128> icon size
title <s> title
url <s> icon url
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.IconItem = function(options, self) {
//Ox.print('IconItem', options, self)
self = self || {};
var that = Ox.Element({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
2011-08-06 17:41:01 +00:00
borderRadius: 0,
2011-08-07 02:33:26 +00:00
iconHeight: 128,
iconWidth: 128,
imageHeight: 128,
imageWidth: 128,
itemHeight: 192,
itemWidth: 128,
2011-04-22 22:03:10 +00:00
id: '',
info: '',
title: '',
url: ''
})
.options(options || {});
2011-04-22 22:03:10 +00:00
Ox.extend(self, {
2011-08-07 02:33:26 +00:00
fontSize: self.options.itemWidth == 64 ? 6 : 9,
lineLength: self.options.itemWidth == 64 ? 15 : 23,
lines: self.options.itemWidth == 64 ? 4 : 5,
2011-04-27 19:49:18 +00:00
url: Ox.UI.PATH + 'png/transparent.png',
2011-04-22 22:03:10 +00:00
});
self.title = formatText(self.options.title, self.lines - 1, self.lineLength);
self.info = formatText(self.options.info, 5 - self.title.split('<br/>').length, self.lineLength);
that.css({
2011-08-07 02:33:26 +00:00
width: self.options.itemWidth + 'px',
height: self.options.itemHeight + 'px'
2011-04-22 22:03:10 +00:00
});
that.$icon = $('<div>')
.addClass('OxIcon')
.css({
2011-08-07 02:33:26 +00:00
top: self.options.iconWidth == 64 ? -64 : -124,
width: (self.options.iconWidth + 4) + 'px',
height: (self.options.iconHeight + 4) + 'px'
2011-04-22 22:03:10 +00:00
});
that.$iconImage = $('<img>')
.addClass('OxLoading OxTarget')
.attr({
src: self.url
})
.css({
2011-08-07 02:33:26 +00:00
width: self.options.imageWidth + 'px',
height: self.options.imageHeight + 'px',
2011-08-06 17:41:01 +00:00
borderRadius: self.options.borderRadius + 4 + 'px'
2011-04-22 22:03:10 +00:00
})
.mousedown(mousedown)
.mouseenter(mouseenter)
.mouseleave(mouseleave);
self.options.url && that.$iconImage.one('load', load);
that.$textBox = $('<div>')
.addClass('OxText')
.css({
2011-08-07 02:33:26 +00:00
top: self.options.iconHeight - self.options.itemWidth / 2 + 'px',
width: self.options.itemWidth + 4 + 'px',
height: (self.options.itemWidth == 64 ? 30 : 58) + 'px'
2011-06-01 10:59:30 +00:00
});
2011-04-22 22:03:10 +00:00
that.$text = $('<div>')
.addClass('OxTarget')
.css({
fontSize: self.fontSize + 'px'
})
.html(
self.title + '<br/><span class="OxInfo">' + self.info + '</span>'
)
.mouseenter(mouseenter)
.mouseleave(mouseleave);
that.$reflection = $('<div>')
.addClass('OxReflection')
.css({
2011-08-07 02:33:26 +00:00
top: self.options.iconHeight + 'px',
width: self.options.itemWidth + 4 + 'px',
height: self.options.itemHeight - self.options.iconHeight + 'px'
2011-04-22 22:03:10 +00:00
});
that.$reflectionImage = $('<img>')
.addClass('OxLoading')
.attr({
src: self.url
})
.css({
2011-08-07 02:33:26 +00:00
width: self.options.imageWidth + 'px',
height: self.options.imageHeight + 'px',
2011-04-22 22:03:10 +00:00
// firefox is 1px off when centering images with odd width and scaleY(-1)
2011-08-07 02:33:26 +00:00
paddingLeft: ($.browser.mozilla && self.options.imageWidth % 2 ? 1 : 0) + 'px',
borderRadius: self.options.borderRadius + 'px'
2011-04-22 22:03:10 +00:00
});
that.$gradient = $('<div>')
.css({
//top: (-self.options.size / 2) + 'px',
2011-08-07 02:33:26 +00:00
width: self.options.itemWidth + 'px',
height: self.options.itemWidth / 2 + 'px'
2011-04-22 22:03:10 +00:00
});
that.append(
that.$reflection.append(
that.$reflectionImage
).append(
that.$gradient
)
).append(
that.$textBox.append(
that.$text
)
).append(
that.$icon.append(
that.$iconImage
)
);
function formatText(text, maxLines, maxLength) {
var lines = Ox.wordwrap(text, maxLength, '<br/>', true, false).split('<br/>');
return Ox.map(lines, function(line, i) {
2011-04-22 22:03:10 +00:00
if (i < maxLines - 1) {
return line;
} else if (i == maxLines - 1) {
return lines.length == maxLines ? line : Ox.truncate(lines.map(function(line, i) {
2011-04-22 22:03:10 +00:00
return i < maxLines - 1 ? null : line;
}).join(' '), maxLength, '...', 'center');
} else {
return null;
}
}).join('<br/>');
}
function load() {
that.$iconImage.attr({
src: self.options.url
})
.one('load', function() {
that.$iconImage.removeClass('OxLoading');
that.$reflectionImage
.attr({
src: self.options.url
})
.removeClass('OxLoading');
});
}
function mousedown(e) {
// fixme: preventDefault keeps image from being draggable in safari - but also keeps the list from getting focus
// e.preventDefault();
}
function mouseenter() {
that.addClass('OxHover');
}
function mouseleave() {
that.removeClass('OxHover');
}
return that;
};