summaryrefslogtreecommitdiff
path: root/wikipedia/widgets/category_layout_manager.js
diff options
context:
space:
mode:
authorRory MacQueen <rorymacqueen@gmail.com>2013-08-15 15:41:36 -0700
committerRory MacQueen <rorymacqueen@gmail.com>2013-08-15 15:41:36 -0700
commitfd81565c5527b99dd212b03c2f329a00e146f03b (patch)
tree7f3afa3eaf6258d34e98abf611f79cbfdb417ecf /wikipedia/widgets/category_layout_manager.js
parent544c137fc8524c5929c7672768775763ea10d848 (diff)
Reorganized import paths
Moved all widgets into widgets directory. Changed Endless Wikipedia file to expose wikipedia web view [endlessm/eos-sdk#260]
Diffstat (limited to 'wikipedia/widgets/category_layout_manager.js')
-rw-r--r--wikipedia/widgets/category_layout_manager.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/wikipedia/widgets/category_layout_manager.js b/wikipedia/widgets/category_layout_manager.js
new file mode 100644
index 0000000..a7be3bb
--- /dev/null
+++ b/wikipedia/widgets/category_layout_manager.js
@@ -0,0 +1,65 @@
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+
+const CategoryLayoutManager = new Lang.Class({
+ Name: 'CategoryLayoutManager',
+ Extends: Gtk.Grid,
+
+ _init: function(props) {
+ props = props || {};
+ props.column_homogeneous = true;
+ props.row_homogeneous = true;
+ this.parent(props);
+
+ this._childWidgets = [];
+ },
+
+ // Distribute children in two columns, except for the last one if an odd
+ // number; that should span two columns
+ _redistributeChildren: function() {
+ let numChildren = this._childWidgets.length;
+ let oddNumber = numChildren % 2 == 1;
+ this._childWidgets.forEach(function(child, index) {
+ let column = index % 2;
+ let row = Math.floor(index / 2);
+
+ if(child.get_parent() === this)
+ Gtk.Container.prototype.remove.call(this,
+ this._childWidgets[index]);
+
+ if(oddNumber && index == numChildren - 1)
+ this.attach(child, 0, row, 2, 1);
+ else
+ this.attach(child, column, row, 1, 1);
+ }, this);
+ },
+
+ add: function(child) {
+ this._childWidgets.push(child);
+ this._redistributeChildren();
+ },
+
+ remove: function(child) {
+ let index = this._childWidgets.indexOf(child);
+ if(index == -1) {
+ printerr('Widget', System.addressOf(child),
+ 'is not contained in CategoryLayoutManager');
+ return;
+ }
+ this._childWidgets.splice(index, 1); // remove
+ this._redistributeChildren();
+ }
+});
+
+// Gtk.init(null);
+// let w = new Gtk.Window();
+// let g = new CategoryLayoutManager();
+// let count = 7;
+// for(let i = 0; i < count; i++) {
+// let widget = new Gtk.Button({label: 'Widget ' + i});
+// g.add(widget);
+// }
+// w.add(g);
+// w.connect('destroy', Gtk.main_quit);
+// w.show_all();
+// Gtk.main();