Passed
Push — master ( f898a6...8a0dc5 )
by Guangyu
08:08 queued 11s
created

admin/js/plugins/dropzone/ng-dropzone.js   A

Complexity

Total Complexity 18
Complexity/F 1.38

Size

Lines of Code 107
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 56
mnd 5
bc 5
fnc 13
dl 0
loc 107
rs 10
bpm 0.3846
cpm 1.3846
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F ng-dropzone.js ➔ factory 0 91 16
1
/**!
2
 * AngularJS dropzone directive
3
 * @author Uday Hiwarale <[email protected]>
4
 * https://www.github.com/thatisuday/ngDropzone
5
 */
6
 
7
 
8
(function(root){
9
    'use strict';
10
  function factory(angular, Dropzone){
11
      
12
    angular.module('thatisuday.dropzone', []).provider('dropzoneOps', function(){
13
      /*
14
       *  Add default options here
15
      **/
16
      var defOps = {
17
        //Add your options here
18
      };
19
      
20
      return {
21
        setOptions : function(newOps){
22
          angular.extend(defOps, newOps);
23
        },
24
        $get : function(){
25
          return defOps;
26
        }
27
      }
28
    }).directive('ngDropzone', ['$timeout', 'dropzoneOps', function($timeout, dropzoneOps){
29
      return {
30
        restrict : 'AE',
31
        template : '<div></div>',
32
        replace : true,
33
        scope : {
34
          options : '=?', //http://www.dropzonejs.com/#configuration-options
35
          callbacks : '=?', //http://www.dropzonejs.com/#events
36
          methods : '=?' //http://www.dropzonejs.com/#dropzone-methods
37
        },
38
        link : function(scope, iElem, iAttr){
39
          //Set options for dropzone {override from dropzone options provider}
40
          scope.options = scope.options || {};
41
          var initOps = angular.extend({}, dropzoneOps, scope.options);
42
          
43
          
44
          //Instantiate dropzone with initOps
45
          var dropzone = new Dropzone(iElem[0], initOps);
46
          
47
          
48
          /*********************************************/
49
          
50
          
51
          //Instantiate Dropzone methods (Control actions)
52
          scope.methods = scope.methods || {};
53
          
54
          scope.methods.getDropzone = function(){ 
55
            return dropzone; //Return dropzone instance
56
          };
57
          
58
          scope.methods.getAllFiles = function(){ 
59
            return dropzone.files; //Return all files
60
          };
61
          
62
          var controlMethods = [
63
            'removeFile', 'removeAllFiles', 'processQueue',
64
            'getAcceptedFiles', 'getRejectedFiles', 'getQueuedFiles', 'getUploadingFiles',
65
            'disable', 'enable', 'confirm', 'createThumbnailFromUrl'
66
          ];
67
          
68
          angular.forEach(controlMethods, function(methodName){
69
            scope.methods[methodName] = function(){
70
              dropzone[methodName].apply(dropzone, arguments);
71
              if(!scope.$$phase && !scope.$root.$$phase) scope.$apply();
72
            }
73
          });
74
          
75
          
76
          /*********************************************/
77
          
78
          
79
          //Set invents (callbacks)
80
          if(scope.callbacks){
81
            var callbackMethods = [
82
              'drop', 'dragstart', 'dragend',
83
              'dragenter', 'dragover', 'dragleave', 'addedfile', 'removedfile',
84
              'thumbnail', 'error', 'processing', 'uploadprogress',
85
              'sending', 'success', 'complete', 'canceled', 'maxfilesreached',
86
              'maxfilesexceeded', 'processingmultiple', 'sendingmultiple', 'successmultiple',
87
              'completemultiple', 'canceledmultiple', 'totaluploadprogress', 'reset', 'queuecomplete'
88
            ];
89
            angular.forEach(callbackMethods, function(method){
90
              var callback = (scope.callbacks[method] || angular.noop);
91
              dropzone.on(method, function(){
92
                callback.apply(null, arguments);
93
                if(!scope.$$phase && !scope.$root.$$phase) scope.$apply();
94
              });
95
            });
96
          }
97
        }
98
      }
99
    }]);
100
  }
101
  
102
  
103
104
  if ((typeof module === 'object') && module.exports) {
105
    /* CommonJS module */
106
    module.exports = factory(require('angular'), require('dropzone'));
107
  } else if (typeof define === 'function' && define.amd) {
108
    /* AMD module */
109
    define(['angular', 'dropzone'], factory);
110
  } else {
111
    /* Browser global */
112
    factory(root.angular, root.Dropzone);
113
  }
114
})(this);
115