Permalink
Browse files

adding a new file namespace

  • Loading branch information...
1 parent 2a20182 commit 45f4d604d1b3b43cd8b6dc5ff0a90b89eed61301 @draeton committed Dec 7, 2011
Showing with 109 additions and 87 deletions.
  1. +1 −0 build.xml
  2. +107 −0 src/file.js
  3. +0 −87 src/page.js
  4. +1 −0 tests/index.html
View
@@ -20,6 +20,7 @@
<file name="icon.js"/>
<file name="tmpl.js"/>
<file name="page.js"/>
+ <file name="file.js"/>
</filelist>
<filelist id="src.css" dir="${src.dir}">
View
@@ -0,0 +1,107 @@
+// ## Stitches.File
+//
+// [http://draeton.github.com/stitches](http://draeton.github.com/stitches)
+//
+// Copyright 2011, Matthew Cobbs
+// Licensed under the MIT license.
+//
+/*global jQuery, Stitches */
+(function (window, Stitches, $) {
+
+ "use strict";
+
+ // ## Stitches.Fle namespace
+ //
+ // Holds all Fle procesing methods
+ Stitches.File = (function () {
+ return {
+ // ### handleFiles
+ //
+ // Loops through `files`; sends images to `addFile`
+ //
+ // @param {FileList} files From a drop event
+ handleFiles: function (files) {
+ Stitches.filesQueue = 0;
+
+ for (var i = 0, l = files.length; i < l; i++) {
+ var file = files[i];
+
+ if (/jpeg|png|gif/.test(file.type)) {
+ Stitches.Page.addFile(file);
+ }
+ }
+ },
+
+ // ### addFile
+ //
+ // Increments the `filesQueue` to track when all images have been processed.
+ // Starts up a new `FileReader` to read in the image as data
+ //
+ // @param {File} file
+ addFile: function (file) {
+ Stitches.filesCount++;
+ Stitches.filesQueue++;
+
+ Stitches.Page.setButtonDisabled(true, ["generate", "clear", "sprite", "stylesheet"]);
+
+ if (Stitches.filesCount === 1) {
+ Stitches.Page.$droplabel.fadeOut("fast");
+ }
+
+ var reader = new FileReader();
+ reader.onloadend = Stitches.Page.handleFileLoad.bind(file);
+ reader.readAsDataURL(file);
+ },
+
+ // ### handleFileLoad
+ //
+ // When the `FileReader` has loaded the file, this creates a new icon
+ // and adds it to the file list in the widget
+ //
+ // @param {Event} evt
+ handleFileLoad: function (evt) {
+ Stitches.filesQueue--;
+
+ var icon = new Stitches.Icon(this.name, evt.target.result);
+ var $li = $(Stitches.Page.templates.icon(icon)).data("icon", icon);
+ Stitches.Page.$filelist.append($li);
+ $li.fadeIn("fast");
+
+ if (Stitches.filesQueue === 0) {
+ Stitches.Page.setButtonDisabled(false, ["generate", "clear"]);
+ }
+ },
+
+ // ### removeFile
+ //
+ // Removes a file from the file list
+ //
+ // @param {Event} evt
+ removeFile: function (evt) {
+ Stitches.Page.setButtonDisabled(true, ["sprite", "stylesheet"]);
+
+ $(this).parent().fadeOut("fast", function () {
+ $(this).remove();
+ });
+
+ Stitches.filesCount--;
+ if (Stitches.filesCount === 0) {
+ Stitches.Page.setButtonDisabled(true, ["generate", "clear"]);
+ Stitches.Page.$droplabel.fadeIn("fast");
+ }
+
+ return false;
+ },
+
+ // ### removeAllFiles
+ //
+ // Clear all files from the file list
+ removeAllFiles: function () {
+ Stitches.Page.$filelist.find("a.remove").each(function () {
+ Stitches.Page.removeFile.call(this);
+ });
+ }
+ };
+ })();
+
+})(window, Stitches, jQuery);
View
@@ -148,93 +148,6 @@
$(buttons).each(function (idx, val) {
Stitches.Page.buttons["$" + val][action + "Class"]("disabled");
});
- },
-
- // ### handleFiles
- //
- // Loops through `files`; sends images to `addFile`
- //
- // @param {FileList} files From a drop event
- handleFiles: function (files) {
- Stitches.filesQueue = 0;
-
- for (var i = 0, l = files.length; i < l; i++) {
- var file = files[i];
-
- if (/jpeg|png|gif/.test(file.type)) {
- Stitches.Page.addFile(file);
- }
- }
- },
-
- // ### addFile
- //
- // Increments the `filesQueue` to track when all images have been processed.
- // Starts up a new `FileReader` to read in the image as data
- //
- // @param {File} file
- addFile: function (file) {
- Stitches.filesCount++;
- Stitches.filesQueue++;
-
- Stitches.Page.setButtonDisabled(true, ["generate", "clear", "sprite", "stylesheet"]);
-
- if (Stitches.filesCount === 1) {
- Stitches.Page.$droplabel.fadeOut("fast");
- }
-
- var reader = new FileReader();
- reader.onloadend = Stitches.Page.handleFileLoad.bind(file);
- reader.readAsDataURL(file);
- },
-
- // ### handleFileLoad
- //
- // When the `FileReader` has loaded the file, this creates a new icon
- // and adds it to the file list in the widget
- //
- // @param {Event} evt
- handleFileLoad: function (evt) {
- Stitches.filesQueue--;
-
- var icon = new Stitches.Icon(this.name, evt.target.result);
- var $li = $(Stitches.Page.templates.icon(icon)).data("icon", icon);
- Stitches.Page.$filelist.append($li);
- $li.fadeIn("fast");
-
- if (Stitches.filesQueue === 0) {
- Stitches.Page.setButtonDisabled(false, ["generate", "clear"]);
- }
- },
-
- // ### removeFile
- //
- // Removes a file from the file list
- //
- // @param {Event} evt
- removeFile: function (evt) {
- Stitches.Page.setButtonDisabled(true, ["sprite", "stylesheet"]);
-
- $(this).parent().fadeOut("fast", function () {
- $(this).remove();
- });
-
- Stitches.filesCount--;
- if (Stitches.filesCount === 0) {
- Stitches.Page.setButtonDisabled(true, ["generate", "clear"]);
- Stitches.Page.$droplabel.fadeIn("fast");
- }
-
- return false;
- },
-
- // ### removeAllFiles
- //
- // Clear all files from the file list
- removeAllFiles: function () {
- Stitches.Page.$filelist.find("a.remove").each(function () {
- Stitches.Page.removeFile.call(this);
- });
}
};
})();
View
@@ -16,6 +16,7 @@
<script src="../src/icon.js"></script>
<script src="../src/tmpl.js"></script>
<script src="../src/page.js"></script>
+ <script src="../src/file.js"></script>
<!-- test runner files -->
<script src="qunit/qunit.js"></script>

0 comments on commit 45f4d60

Please sign in to comment.