Permalink
Browse files

removed header from src file. build v0.0.1 w/ grunt

  • Loading branch information...
1 parent 39f1d02 commit 64e250e336c231f19a23a36ba83a03b734b8b49b @derickbailey committed May 25, 2012
Showing with 319 additions and 6 deletions.
  1. +159 −0 lib/amd/backbone.picky.js
  2. +5 −0 lib/amd/backbone.picky.min.js
  3. +150 −0 lib/backbone.picky.js
  4. +5 −0 lib/backbone.picky.min.js
  5. +0 −6 src/backbone.picky.js
View
@@ -0,0 +1,159 @@
+// Backbone.Picky, v0.0.1
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/derickbailey/backbone.picky
+(function (root, factory) {
+ if (typeof define === 'function' && define.amd) {
+ // AMD. Register as an anonymous module.
+ define(["_", "Backbone"], factory);
+ }
+}(this, function (_, Backbone) {
+ Backbone.Picky = (function (Backbone, _) {
+ var Picky = {};
+
+ // Picky.SingleSelect
+ // ------------------
+
+ // Coming Soon!
+
+ // Picky.MultiSelect
+ // -----------------
+ // A mult-select mixin for Backbone.Collection, allowing a collection to
+ // have multiple items selected, including `selectAll` and `selectNone`
+ // capabilities.
+
+ Picky.MultiSelect = function (collection) {
+ this.collection = collection;
+ this.selected = {};
+ };
+
+ _.extend(Picky.MultiSelect.prototype, {
+
+ // Select a specified model, make sure the
+ // model knows it's selected, and hold on to
+ // the selected model.
+ select: function (model) {
+ if (this.selected[model.cid]) { return; }
+
+ this.selected[model.cid] = model;
+ model.select();
+ calculateSelectedLength(this);
+ },
+
+ // Deselect a specified model, make sure the
+ // model knows it has been deselected, and remove
+ // the model from the selected list.
+ deselect: function (model) {
+ if (!this.selected[model.cid]) { return; }
+
+ delete this.selected[model.cid];
+ model.deselect();
+ calculateSelectedLength(this);
+ },
+
+ // Select all models in this collection
+ selectAll: function () {
+ this.each(function (model) { model.select(); });
+ calculateSelectedLength(this);
+ },
+
+ // Deselect all models in this collection
+ selectNone: function () {
+ if (this.selectedLength === 0) { return; }
+ this.each(function (model) { model.deselect(); });
+ calculateSelectedLength(this);
+ },
+
+ // Toggle select all / none. If some are selected, it
+ // will select all. If all are selected, it will select
+ // none. If none are selected, it will select all.
+ toggleSelectAll: function () {
+ if (this.selectedLength === this.length) {
+ this.selectNone();
+ } else {
+ this.selectAll();
+ }
+ }
+ });
+
+ // Picky.Selectable
+ // ----------------
+ // A selectable mixin for Backbone.Model, allowing a model to be selected,
+ // enabling it to work with Picky.MultiSelect or on it's own
+
+ Picky.Selectable = function (model) {
+ this.model = model;
+ };
+
+ _.extend(Picky.Selectable.prototype, {
+
+ // Select this model, and tell our
+ // collection that we're selected
+ select: function () {
+ if (this.selected) { return; }
+
+ this.selected = true;
+ this.trigger("selected");
+
+ if (this.collection) {
+ this.collection.select(this);
+ }
+ },
+
+ // Deselect this model, and tell our
+ // collection that we're deselected
+ deselect: function () {
+ if (!this.selected) { return; }
+
+ this.selected = false;
+ this.trigger("deselected");
+
+ if (this.collection) {
+ this.collection.deselect(this);
+ }
+ },
+
+ // Change selected to the opposite of what
+ // it currently is
+ toggleSelected: function () {
+ if (this.selected) {
+ this.deselect();
+ } else {
+ this.select();
+ }
+ }
+ });
+
+ // Helper Methods
+ // --------------
+
+ // Calculate the number of selected items in a collection
+ // and update the collection with that length. Trigger events
+ // from the collection based on the number of selected items.
+ var calculateSelectedLength = function (collection) {
+ collection.selectedLength = _.size(collection.selected);
+
+ var selectedLength = collection.selectedLength;
+ var length = collection.length;
+
+ if (selectedLength === length) {
+ collection.trigger("select:all", collection);
+ return;
+ }
+
+ if (selectedLength === 0) {
+ collection.trigger("select:none", collection);
+ return;
+ }
+
+ if (selectedLength > 0 && selectedLength < length) {
+ collection.trigger("select:some", collection);
+ return;
+ }
+ };
+
+ return Picky;
+ })(Backbone, _);
+
+ return Backbone.Picky;
+}));

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,150 @@
+// Backbone.Picky, v0.0.1
+// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
+// Distributed under MIT license
+// http://github.com/derickbailey/backbone.picky
+Backbone.Picky = (function (Backbone, _) {
+ var Picky = {};
+
+ // Picky.SingleSelect
+ // ------------------
+
+ // Coming Soon!
+
+ // Picky.MultiSelect
+ // -----------------
+ // A mult-select mixin for Backbone.Collection, allowing a collection to
+ // have multiple items selected, including `selectAll` and `selectNone`
+ // capabilities.
+
+ Picky.MultiSelect = function (collection) {
+ this.collection = collection;
+ this.selected = {};
+ };
+
+ _.extend(Picky.MultiSelect.prototype, {
+
+ // Select a specified model, make sure the
+ // model knows it's selected, and hold on to
+ // the selected model.
+ select: function (model) {
+ if (this.selected[model.cid]) { return; }
+
+ this.selected[model.cid] = model;
+ model.select();
+ calculateSelectedLength(this);
+ },
+
+ // Deselect a specified model, make sure the
+ // model knows it has been deselected, and remove
+ // the model from the selected list.
+ deselect: function (model) {
+ if (!this.selected[model.cid]) { return; }
+
+ delete this.selected[model.cid];
+ model.deselect();
+ calculateSelectedLength(this);
+ },
+
+ // Select all models in this collection
+ selectAll: function () {
+ this.each(function (model) { model.select(); });
+ calculateSelectedLength(this);
+ },
+
+ // Deselect all models in this collection
+ selectNone: function () {
+ if (this.selectedLength === 0) { return; }
+ this.each(function (model) { model.deselect(); });
+ calculateSelectedLength(this);
+ },
+
+ // Toggle select all / none. If some are selected, it
+ // will select all. If all are selected, it will select
+ // none. If none are selected, it will select all.
+ toggleSelectAll: function () {
+ if (this.selectedLength === this.length) {
+ this.selectNone();
+ } else {
+ this.selectAll();
+ }
+ }
+ });
+
+ // Picky.Selectable
+ // ----------------
+ // A selectable mixin for Backbone.Model, allowing a model to be selected,
+ // enabling it to work with Picky.MultiSelect or on it's own
+
+ Picky.Selectable = function (model) {
+ this.model = model;
+ };
+
+ _.extend(Picky.Selectable.prototype, {
+
+ // Select this model, and tell our
+ // collection that we're selected
+ select: function () {
+ if (this.selected) { return; }
+
+ this.selected = true;
+ this.trigger("selected");
+
+ if (this.collection) {
+ this.collection.select(this);
+ }
+ },
+
+ // Deselect this model, and tell our
+ // collection that we're deselected
+ deselect: function () {
+ if (!this.selected) { return; }
+
+ this.selected = false;
+ this.trigger("deselected");
+
+ if (this.collection) {
+ this.collection.deselect(this);
+ }
+ },
+
+ // Change selected to the opposite of what
+ // it currently is
+ toggleSelected: function () {
+ if (this.selected) {
+ this.deselect();
+ } else {
+ this.select();
+ }
+ }
+ });
+
+ // Helper Methods
+ // --------------
+
+ // Calculate the number of selected items in a collection
+ // and update the collection with that length. Trigger events
+ // from the collection based on the number of selected items.
+ var calculateSelectedLength = function (collection) {
+ collection.selectedLength = _.size(collection.selected);
+
+ var selectedLength = collection.selectedLength;
+ var length = collection.length;
+
+ if (selectedLength === length) {
+ collection.trigger("select:all", collection);
+ return;
+ }
+
+ if (selectedLength === 0) {
+ collection.trigger("select:none", collection);
+ return;
+ }
+
+ if (selectedLength > 0 && selectedLength < length) {
+ collection.trigger("select:some", collection);
+ return;
+ }
+ };
+
+ return Picky;
+})(Backbone, _);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,9 +1,3 @@
-// Backbone.Picky - v0.0.1
-// Copyright (C)2012 Derick Bailey, Muted Solutions, LLC. All Rights Reserved.
-//
-// Distributed under MIT license.
-// http://github.com/derickbailey/backbone.picky
-
Backbone.Picky = (function (Backbone, _) {
var Picky = {};

0 comments on commit 64e250e

Please sign in to comment.