From 56b4013f0ce5c981eccf184e1cb8e91267be817e Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 25 Jun 2012 16:39:31 +0200 Subject: [PATCH] update widget example --- .../ui/widget_design_patterns/css/example.css | 7 +- .../ui/widget_design_patterns/js/example.js | 252 ++++++++++++++++-- .../widget_design_patterns/png/pandora32.png | Bin 0 -> 4328 bytes 3 files changed, 241 insertions(+), 18 deletions(-) create mode 100644 examples/ui/widget_design_patterns/png/pandora32.png diff --git a/examples/ui/widget_design_patterns/css/example.css b/examples/ui/widget_design_patterns/css/example.css index 3c00d862..f3cb4ac9 100644 --- a/examples/ui/widget_design_patterns/css/example.css +++ b/examples/ui/widget_design_patterns/css/example.css @@ -1,3 +1,6 @@ -OxMyBox.focused { - box-shadow: inset 0 0 1px black; +.OxMyBox { + float: left; +} +.OxMyInvertibleBox { + cursor: pointer; } \ No newline at end of file diff --git a/examples/ui/widget_design_patterns/js/example.js b/examples/ui/widget_design_patterns/js/example.js index 18671d4a..8dfaca47 100644 --- a/examples/ui/widget_design_patterns/js/example.js +++ b/examples/ui/widget_design_patterns/js/example.js @@ -16,7 +16,7 @@ be accessed from outside, but since `self` itself is an argument of the /* Load the UI module. */ -Ox.load('UI', function() { +Ox.load(['Image', 'UI'], function() { /* Create our own namespace. @@ -56,13 +56,14 @@ Ox.load('UI', function() { var that = Ox.Element({}, self) .defaults({ color: [128, 128, 128], - size: 128 + size: [128, 128] }) .options(options || {}) .update({ color: setColor, size: setSize - }); + }) + .addClass('OxMyBox'); /* The second part of the "constructor" function can be thought of as the @@ -73,8 +74,8 @@ Ox.load('UI', function() { be accessible across all the widget's methods, we can be sure that inside such methods, any local `var` is actually local to the method. */ - self.minSize = 128; - self.maxSize = 384; + self.minSize = 1; + self.maxSize = 256; setColor(); setSize(); @@ -103,12 +104,12 @@ Ox.load('UI', function() { Before setting the size, we make sure the value is between `minSize` and `maxSize`. */ - self.options.size = Ox.limit( - self.options.size, self.minSize, self.maxSize - ); + self.options.size = self.options.size.map(function(value) { + return Ox.limit(value, self.minSize, self.maxSize); + }); that.css({ - height: self.options.size + 'px', - width: self.options.size + 'px' + width: self.options.size[0] + 'px', + height: self.options.size[1] + 'px' }); } @@ -136,14 +137,233 @@ Ox.load('UI', function() { }; + Ox.My.InvertibleBox = function(options, self) { + + self = self || {}; + var that = Ox.My.Box({}, self); + that.defaults(Ox.extend(that.defaults(), { + inverted: false + })) + .options(options || {}) + .update({ + color: setColor, + inverted: setColor + }) + .addClass('OxMyInvertibleBox') + .bindEvent({ + doubleclick: function() { + that.invert(); + } + }); + + self.options.inverted && setColor(); + + function getInvertedColor() { + return self.options.color.map(function(value) { + return 255 - value; + }); + } + + function setColor() { + that.css({backgroundColor: 'rgb(' + ( + self.options.inverted ? getInvertedColor() : self.options.color + ).join(', ') + ')'}); + return false; + } + + that.invert = function() { + that.options({inverted: !self.options.inverted}); + }; + + return that; + + }; + + Ox.My.MetaBox = function(options, self) { + + self = self || {}; + var that = Ox.My.Box(options || {}, self) + .update({color: setColor}); + + self.inverting = false; + self.sizes = [ + Ox.splitInt(self.options.size[0], self.options.color[0].length), + Ox.splitInt(self.options.size[1], self.options.color.length) + ]; + + self.$boxes = self.options.color.map(function(array, y) { + return array.map(function(color, x) { + return Ox.My.InvertibleBox({ + color: color, + size: [self.sizes[0][x], self.sizes[1][y]] + }) + .update({ + inverted: function() { + if (!self.inverting) { + self.inverting = true; + self.$boxes[y][x].invert(); + that.invert(); + return false; + } + } + }) + .appendTo(that); + }); + }); + + function setColor() { + self.$boxes.forEach(function(array, y) { + array.forEach(function($box, x) { + $box.options({color: self.options.color[y][x]}); + }); + }); + } + + that.invert = function() { + self.inverting = true; + self.$boxes.forEach(function(array) { + array.forEach(function($box) { + $box.invert(); + }); + }); + self.inverting = false; + }; + + return that; + + }; + + Ox.My.PixelBox = function(options, self) { + + self = self || {}; + var that = Ox.My.Box(options || {}, self) + .update({ + color: setColor + }); + + self.$pixel = Ox.My.MetaBox(Ox.extend(options, { + color: getColor() + }), self) + .appendTo(that); + + self.$pixel.invert = function() { + self.inverting = true; + self.$boxes.forEach(function(array) { + array.forEach(function($box, x) { + $box.options({ + color: $box.options('color').map(function(value, i) { + return i == x ? 255 - value : value + }) + }); + }); + }); + self.inverting = false; + that.triggerEvent('invert'); + }; + + function getColor() { + return [[ + [self.options.color[0], 0, 0], + [0, self.options.color[1], 0], + [0, 0, self.options.color[2]] + ]]; + } + + function setColor() { + self.$pixel.options({color: getColor()}); + } + + that.invert = function() { + self.$pixel.invert(); + }; + + return that; + + }; + + Ox.My.ImageBox = function(options, self) { + + self = self || {}; + var that = Ox.My.Box(options || {}, self); + + self.inverting = false; + + Ox.Image(self.options.image, function(image) { + var size = image.getSize(); + size = [size.width, size.height]; + self.sizes = size.map(function(value, index) { + return Ox.splitInt(self.options.size[index], value); + }); + self.$boxes = Ox.range(size[1]).map(function(y) { + return Ox.range(size[0]).map(function(x) { + return Ox.My.PixelBox({ + color: image.pixel(x, y).slice(0, 3), + size: [self.sizes[0][x], self.sizes[1][y]] + }) + .bindEvent({ + invert: function() { + if (!self.inverting) { + self.inverting = true; + self.$boxes[y][x].invert(); + that.invert(); + return false; + } + } + }) + .appendTo(that); + }); + }); + }); + + that.invert = function() { + self.inverting = true; + self.$boxes.forEach(function(array) { + array.forEach(function($box) { + $box.invert(); + }); + }); + self.inverting = false; + }; + + return that; + + }; + /* -
-    Ox.My.Box({color: [255, 0, 0]})
-        .appendTo(Ox.$body)
-        .options({size: 256})
-        .showOptions();
-    
+ This is left as an exercise for the reader ;) */ + Ox.My.VideoBox = function(options, self) { + + }; + + window.My = {}; + My.$div = $('
') + .css({float: 'left', width: '256px', height: '256px'}) + .appendTo(Ox.$body); + My.$box = Ox.My.Box({ + color: [255, 0, 0] + }).appendTo(My.$div); + My.$invertibleBox = Ox.My.InvertibleBox({ + color: [0, 0, 255] + }).appendTo(My.$div); + My.$metaBox = Ox.My.MetaBox({ + color: [ + [[255, 0, 0], [255, 255, 0], [0, 255, 0]], + [[0, 255, 255], [0, 0, 255], [255, 0, 255]] + ] + }).appendTo(My.$div); + My.$pixelBox = Ox.My.PixelBox({ + color: [255, 128, 0] + }).appendTo(My.$div); + setTimeout(function() { + My.$imageBox = Ox.My.ImageBox({ + image: 'png/pandora32.png', + size: [256, 256] + }).appendTo(Ox.$body); + }); + + + return; /* Now we create a new widget that subclasses `Ox.My.Box`. diff --git a/examples/ui/widget_design_patterns/png/pandora32.png b/examples/ui/widget_design_patterns/png/pandora32.png new file mode 100644 index 0000000000000000000000000000000000000000..111e03058d26c78b708ef4f734ef74e092043f55 GIT binary patch literal 4328 zcmVP)KLZ*U+IBfRsybQWXdwQbLP>6pAqfylh#{fb6;Z(vMMVS~$e@S=j*ftg6;Uhf59&ghTmgWD0l;*T zI709Y^p6lP1rIRMx#05C~cW=H_Aw*bJ-5DT&Z2n+x)QHX^p z00esgV8|mQcmRZ%02D^@S3L16t`O%c004NIvOKvYIYoh62rY33S640`D9%Y2D-rV&neh&#Q1i z007~1e$oCcFS8neI|hJl{-P!B1ZZ9hpmq0)X0i`JwE&>$+E?>%_LC6RbVIkUx0b+_+BaR3cnT7Zv!AJxW zizFb)h!jyGOOZ85F;a?DAXP{m@;!0_IfqH8(HlgRxt7s3}k3K`kFu>>-2Q$QMFfPW!La{h336o>X zu_CMttHv6zR;&ZNiS=X8v3CR#fknUxHUxJ0uoBa_M6WNWeqIg~6QE69c9o#eyhGvpiOA@W-aonk<7r1(?fC{oI5N*U!4 zfg=2N-7=cNnjjOr{yriy6mMFgG#l znCF=fnQv8CDz++o6_Lscl}eQ+l^ZHARH>?_s@|##Rr6KLRFA1%Q+=*RRWnoLsR`7U zt5vFIcfW3@?wFpwUVxrVZ>QdQz32KIeJ}k~{cZZE^+ya? z2D1z#2HOnI7(B%_ac?{wFUQ;QQA1tBKtrWrm0_3Rgps+?Jfqb{jYbcQX~taRB;#$y zZN{S}1|}gUOHJxc?wV3fxuz+mJ4`!F$IZ;mqRrNsHJd##*D~ju=bP7?-?v~|c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