From 93b22b9feae11c5bbc8538e2f3aa2adc02e4c4c3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 22 May 2014 07:17:39 -0400 Subject: [PATCH] feature(util) add forEach --- lib/client/listeners.js | 16 +++++++--------- lib/util.js | 14 ++++++++++++++ test/lib/util.js | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/client/listeners.js b/lib/client/listeners.js index 9051284e..190939ce 100644 --- a/lib/client/listeners.js +++ b/lib/client/listeners.js @@ -324,18 +324,17 @@ var Util, DOM, CloudCmd; } function dragndrop() { - var panelsEl = DOM.getByClassAll('panel'), - panels = Util.slice(panelsEl), + var panels = DOM.getByClassAll('panel'), preventDefault = function (event) { event.preventDefault(); }, toggle = function () { - panels.forEach(function(panel) { + Util.forEach(panels, function(panel) { DOM.toggleClass(panel, 'selected-panel'); }); }, onDrop = function (event) { - var filesData, files, + var files, func = CloudCmd.refresh, dir = Info.dirPath, load = function(file, callback) { @@ -352,11 +351,10 @@ var Util, DOM, CloudCmd; preventDefault(event); toggle(); - filesData = event.dataTransfer.files; - files = Util.slice(filesData); - + files = event.dataTransfer.files; + if (files.length) { - files.forEach(function(file) { + Util.forEach(files, function(file) { func = Util.bind(load, file, func); }); @@ -366,7 +364,7 @@ var Util, DOM, CloudCmd; Events.add(['dragenter', 'dragleave'], toggle); - panels.forEach(function(panel) { + Util.forEach(panels, function(panel) { Events.add('dragover', preventDefault, panel); Events.add('drop', onDrop, panel); }); diff --git a/lib/util.js b/lib/util.js index ce0a0c14..d5be47a1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -736,6 +736,20 @@ return ret; }; + /** + * function calls forEach for all array like variables + * + * @param array + */ + this.forEach = function(array, callback) { + var ret = []; + + if (array) + ret = [].forEach.call(array, callback); + + return ret; + }; + /** * function execute param function in * try...catch block diff --git a/test/lib/util.js b/test/lib/util.js index 8eb114f4..4dfea99d 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -138,6 +138,26 @@ }); + describe('forEach', function() { + it('should call arrays forEach on any type', function() { + var str = '', + obj = { + 0: 'a', + 1: 'b', + 2: 'c', + length: 3 + }; + + Util.forEach(obj, function(item) { + str += item; + }); + + str.should.be.equal('abc'); + + }); + + }); + }); })();