From cfdb610eae6f53def3b8b6bfdd64d646a43805ae Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 5 Jul 2017 14:48:10 +0100 Subject: [PATCH] support umd --- Gruntfile.js | 6 ---- index.js | 10 ++++-- package.json | 2 +- src/angular-flow.js | 2 -- src/directives/btn.js | 14 -------- src/directives/drag-events.js | 57 -------------------------------- src/directives/drop.js | 26 --------------- src/directives/events.js | 50 ---------------------------- src/directives/img.js | 22 ------------- src/directives/init.js | 43 ------------------------ src/directives/transfers.js | 10 ------ {dist => src}/ng-flow.js | 16 ++++++++- src/provider.js | 62 ----------------------------------- 13 files changed, 23 insertions(+), 297 deletions(-) delete mode 100644 src/angular-flow.js delete mode 100644 src/directives/btn.js delete mode 100644 src/directives/drag-events.js delete mode 100644 src/directives/drop.js delete mode 100644 src/directives/events.js delete mode 100644 src/directives/img.js delete mode 100644 src/directives/init.js delete mode 100644 src/directives/transfers.js rename {dist => src}/ng-flow.js (94%) delete mode 100644 src/provider.js diff --git a/Gruntfile.js b/Gruntfile.js index 47b2b37..e4cb1b2 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -19,16 +19,10 @@ module.exports = function(grunt) { flow: { files: { 'dist/ng-flow.js': [ - 'src/provider.js', - 'src/directives/init.js', - 'src/directives/*.js', 'src/*.js' ], 'dist/ng-flow-standalone.js': [ 'bower_components/flow.js/dist/flow.js', - 'src/provider.js', - 'src/directives/init.js', - 'src/directives/*.js', 'src/*.js' ] } diff --git a/index.js b/index.js index aa3d332..5442771 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,7 @@ -/* for module loading using webpack or similar package bundlers */ -window.Flow = require('./dist/ng-flow-standalone'); -module.exports = 'flow'; +/* backwards compatable with deprecated index.js*/ +try { + console.warn(new Error("use const ngFlow = require('@flowjs/ng-flow');")); +} catch (e) { + // do nothing +} +module.exports = require('./src/ng-flow'); diff --git a/package.json b/package.json index 41960da..fad3139 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "test": "grunt test" }, - "main": "dist/ng-flow.js", + "main": "src/ng-flow.js", "repository": { "type": "git", "url": "git://github.com/flowjs/ng-flow.git" diff --git a/src/angular-flow.js b/src/angular-flow.js deleted file mode 100644 index f6dd22d..0000000 --- a/src/angular-flow.js +++ /dev/null @@ -1,2 +0,0 @@ -angular.module('flow', ['flow.provider', 'flow.init', 'flow.events', 'flow.btn', - 'flow.drop', 'flow.transfers', 'flow.img', 'flow.dragEvents']); \ No newline at end of file diff --git a/src/directives/btn.js b/src/directives/btn.js deleted file mode 100644 index bdbafff..0000000 --- a/src/directives/btn.js +++ /dev/null @@ -1,14 +0,0 @@ -angular.module('flow.btn', ['flow.init']) -.directive('flowBtn', [function() { - return { - 'restrict': 'EA', - 'scope': false, - 'require': '^flowInit', - 'link': function(scope, element, attrs) { - var isDirectory = attrs.hasOwnProperty('flowDirectory'); - var isSingleFile = attrs.hasOwnProperty('flowSingleFile'); - var inputAttrs = attrs.hasOwnProperty('flowAttrs') && scope.$eval(attrs.flowAttrs); - scope.$flow.assignBrowse(element, isDirectory, isSingleFile, inputAttrs); - } - }; -}]); \ No newline at end of file diff --git a/src/directives/drag-events.js b/src/directives/drag-events.js deleted file mode 100644 index fda5517..0000000 --- a/src/directives/drag-events.js +++ /dev/null @@ -1,57 +0,0 @@ -angular.module('flow.dragEvents', ['flow.init']) -/** - * @name flowPreventDrop - * Prevent loading files then dropped on element - */ - .directive('flowPreventDrop', function() { - return { - 'scope': false, - 'link': function(scope, element, attrs) { - element.bind('drop dragover', function (event) { - event.preventDefault(); - }); - } - }; - }) -/** - * @name flowDragEnter - * executes `flowDragEnter` and `flowDragLeave` events - */ - .directive('flowDragEnter', ['$timeout', function($timeout) { - return { - 'scope': false, - 'link': function(scope, element, attrs) { - var promise; - var enter = false; - element.bind('dragover', function (event) { - if (!isFileDrag(event)) { - return ; - } - if (!enter) { - scope.$apply(attrs.flowDragEnter); - enter = true; - } - $timeout.cancel(promise); - event.preventDefault(); - }); - element.bind('dragleave drop', function (event) { - $timeout.cancel(promise); - promise = $timeout(function () { - scope.$eval(attrs.flowDragLeave); - promise = null; - enter = false; - }, 100); - }); - function isFileDrag(dragEvent) { - var fileDrag = false; - var dataTransfer = dragEvent.dataTransfer || dragEvent.originalEvent.dataTransfer; - angular.forEach(dataTransfer && dataTransfer.types, function(val) { - if (val === 'Files') { - fileDrag = true; - } - }); - return fileDrag; - } - } - }; - }]); diff --git a/src/directives/drop.js b/src/directives/drop.js deleted file mode 100644 index b028ec0..0000000 --- a/src/directives/drop.js +++ /dev/null @@ -1,26 +0,0 @@ -angular.module('flow.drop', ['flow.init']) -.directive('flowDrop', function() { - return { - 'scope': false, - 'require': '^flowInit', - 'link': function(scope, element, attrs) { - if (attrs.flowDropEnabled) { - scope.$watch(attrs.flowDropEnabled, function (value) { - if (value) { - assignDrop(); - } else { - unAssignDrop(); - } - }); - } else { - assignDrop(); - } - function assignDrop() { - scope.$flow.assignDrop(element); - } - function unAssignDrop() { - scope.$flow.unAssignDrop(element); - } - } - }; -}); diff --git a/src/directives/events.js b/src/directives/events.js deleted file mode 100644 index 9c4d8d8..0000000 --- a/src/directives/events.js +++ /dev/null @@ -1,50 +0,0 @@ -!function (angular) {'use strict'; - var module = angular.module('flow.events', ['flow.init']); - var events = { - fileSuccess: ['$file', '$message'], - fileProgress: ['$file'], - fileAdded: ['$file', '$event'], - filesAdded: ['$files', '$event'], - filesSubmitted: ['$files', '$event'], - fileRetry: ['$file'], - fileRemoved: ['$file'], - fileError: ['$file', '$message'], - uploadStart: [], - complete: [], - progress: [], - error: ['$message', '$file'] - }; - - angular.forEach(events, function (eventArgs, eventName) { - var name = 'flow' + capitaliseFirstLetter(eventName); - if (name == 'flowUploadStart') { - name = 'flowUploadStarted';// event alias - } - module.directive(name, [function() { - return { - require: '^flowInit', - controller: ['$scope', '$attrs', function ($scope, $attrs) { - $scope.$on('flow::' + eventName, function () { - var funcArgs = Array.prototype.slice.call(arguments); - var event = funcArgs.shift();// remove angular event - // remove flow object and ignore event if it is from parent directive - if ($scope.$flow !== funcArgs.shift()) { - return ; - } - var args = {}; - angular.forEach(eventArgs, function(value, key) { - args[value] = funcArgs[key]; - }); - if ($scope.$eval($attrs[name], args) === false) { - event.preventDefault(); - } - }); - }] - }; - }]); - }); - - function capitaliseFirstLetter(string) { - return string.charAt(0).toUpperCase() + string.slice(1); - } -}(angular); diff --git a/src/directives/img.js b/src/directives/img.js deleted file mode 100644 index cf15f1b..0000000 --- a/src/directives/img.js +++ /dev/null @@ -1,22 +0,0 @@ -angular.module('flow.img', ['flow.init']) -.directive('flowImg', [function() { - return { - 'scope': false, - 'require': '^flowInit', - 'link': function(scope, element, attrs) { - var file = attrs.flowImg; - scope.$watch(file, function (file) { - if (!file) { - return ; - } - var fileReader = new FileReader(); - fileReader.readAsDataURL(file.file); - fileReader.onload = function (event) { - scope.$apply(function () { - attrs.$set('src', event.target.result); - }); - }; - }); - } - }; -}]); \ No newline at end of file diff --git a/src/directives/init.js b/src/directives/init.js deleted file mode 100644 index fcc3f13..0000000 --- a/src/directives/init.js +++ /dev/null @@ -1,43 +0,0 @@ -angular.module('flow.init', ['flow.provider']) - .controller('flowCtrl', ['$scope', '$attrs', '$parse', 'flowFactory', - function ($scope, $attrs, $parse, flowFactory) { - - var options = angular.extend({}, $scope.$eval($attrs.flowInit)); - - // use existing flow object or create a new one - var flow = $scope.$eval($attrs.flowObject) || flowFactory.create(options); - - var catchAllHandler = function(eventName){ - var args = Array.prototype.slice.call(arguments); - args.shift(); - var event = $scope.$broadcast.apply($scope, ['flow::' + eventName, flow].concat(args)); - if ({ - 'progress':1, 'filesSubmitted':1, 'fileSuccess': 1, 'fileError': 1, 'complete': 1 - }[eventName]) { - $scope.$apply(); - } - if (event.defaultPrevented) { - return false; - } - }; - - flow.on('catchAll', catchAllHandler); - $scope.$on('$destroy', function(){ - flow.off('catchAll', catchAllHandler); - }); - - $scope.$flow = flow; - - if ($attrs.hasOwnProperty('flowName')) { - $parse($attrs.flowName).assign($scope, flow); - $scope.$on('$destroy', function () { - $parse($attrs.flowName).assign($scope); - }); - } - }]) - .directive('flowInit', [function() { - return { - scope: true, - controller: 'flowCtrl' - }; - }]); \ No newline at end of file diff --git a/src/directives/transfers.js b/src/directives/transfers.js deleted file mode 100644 index c295a24..0000000 --- a/src/directives/transfers.js +++ /dev/null @@ -1,10 +0,0 @@ -angular.module('flow.transfers', ['flow.init']) -.directive('flowTransfers', [function() { - return { - 'scope': true, - 'require': '^flowInit', - 'link': function(scope) { - scope.transfers = scope.$flow.files; - } - }; -}]); \ No newline at end of file diff --git a/dist/ng-flow.js b/src/ng-flow.js similarity index 94% rename from dist/ng-flow.js rename to src/ng-flow.js index e38ae54..cfae5ff 100644 --- a/dist/ng-flow.js +++ b/src/ng-flow.js @@ -1,3 +1,14 @@ +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + define(['angular', '@flowjs/flow.js'], factory); + } else if (typeof exports === 'object') { + // Node, CommonJS-like + module.exports = factory(require('angular'), require('@flowjs/flow.js')); + } else { + // Browser globals (root is window) + root.ngFlow = factory(root.angular, root.Flow); + } +}(this, function (angular, Flow) { /** * @description * var app = angular.module('App', ['flow.provider'], function(flowFactoryProvider){ @@ -286,4 +297,7 @@ angular.module('flow.transfers', ['flow.init']) }; }]); angular.module('flow', ['flow.provider', 'flow.init', 'flow.events', 'flow.btn', - 'flow.drop', 'flow.transfers', 'flow.img', 'flow.dragEvents']); \ No newline at end of file + 'flow.drop', 'flow.transfers', 'flow.img', 'flow.dragEvents']); + +return 'flow'; +})); diff --git a/src/provider.js b/src/provider.js deleted file mode 100644 index b024ab8..0000000 --- a/src/provider.js +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @description - * var app = angular.module('App', ['flow.provider'], function(flowFactoryProvider){ - * flowFactoryProvider.defaults = {target: '/'}; - * }); - * @name flowFactoryProvider - */ -angular.module('flow.provider', []) -.provider('flowFactory', function() { - 'use strict'; - /** - * Define the default properties for flow.js - * @name flowFactoryProvider.defaults - * @type {Object} - */ - this.defaults = {}; - - /** - * Flow, MaybeFlow or NotFlow - * @name flowFactoryProvider.factory - * @type {function} - * @return {Flow} - */ - this.factory = function (options) { - return new Flow(options); - }; - - /** - * Define the default events - * @name flowFactoryProvider.events - * @type {Array} - * @private - */ - this.events = []; - - /** - * Add default events - * @name flowFactoryProvider.on - * @function - * @param {string} event - * @param {Function} callback - */ - this.on = function (event, callback) { - this.events.push([event, callback]); - }; - - this.$get = function() { - var fn = this.factory; - var defaults = this.defaults; - var events = this.events; - return { - 'create': function(opts) { - // combine default options with global options and options - var flow = fn(angular.extend({}, defaults, opts)); - angular.forEach(events, function (event) { - flow.on(event[0], event[1]); - }); - return flow; - } - }; - }; -}); \ No newline at end of file