Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 84c4fc4

Browse files
thgreasidouglasduteil
authored andcommitted
Prettified to 2 indents to reduce +-'s with PR.
Changed angular version requirements to 1.x from 1.0.x.
1 parent 3ff67a5 commit 84c4fc4

File tree

2 files changed

+98
-96
lines changed

2 files changed

+98
-96
lines changed

bower.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"package.json"
1717
],
1818
"dependencies": {
19-
"angular": "~1.0.x",
20-
"jquery-ui": ">= 1.9"
19+
"angular": "~1.x",
20+
"jquery-ui": ">= 1.9",
21+
"jquery-simulate": "latest"
2122
},
2223
"devDependencies": {
23-
"angular-mocks": "~1.0.x",
24-
"jquery-simulate": "latest"
24+
"angular-mocks": "~1.x"
2525
}
2626
}

src/sortable.js

+94-92
Original file line numberDiff line numberDiff line change
@@ -6,119 +6,121 @@
66
@param [ui-sortable] {object} Options to pass to $.fn.sortable() merged onto ui.config
77
*/
88
angular.module('ui.sortable', [])
9-
.value('uiSortableConfig',{})
10-
.directive('uiSortable', [ 'uiSortableConfig', '$log',
11-
function(uiSortableConfig, log) {
12-
return {
13-
require: '?ngModel',
14-
link: function(scope, element, attrs, ngModel) {
15-
16-
function combineCallbacks(first,second){
17-
if( second && (typeof second === 'function') ){
18-
return function(e,ui){
19-
first(e,ui);
20-
second(e,ui);
21-
};
22-
}
23-
return first;
24-
}
25-
26-
var opts = {};
27-
28-
var callbacks = {
29-
receive: null,
30-
remove:null,
31-
start:null,
32-
stop:null,
33-
update:null
34-
};
9+
.value('uiSortableConfig', {})
10+
.directive('uiSortable', ['uiSortableConfig', '$log',
11+
function(uiSortableConfig, log) {
12+
return {
13+
require: '?ngModel',
14+
link: function(scope, element, attrs, ngModel) {
15+
16+
function combineCallbacks(first, second) {
17+
if (second && (typeof second === 'function')) {
18+
return function(e, ui) {
19+
first(e, ui);
20+
second(e, ui);
21+
};
22+
}
23+
return first;
24+
}
3525

36-
var apply = function(e, ui) {
37-
if (ui.item.sortable.resort || ui.item.sortable.relocate) {
38-
scope.$apply();
39-
}
40-
};
26+
var opts = {};
4127

42-
angular.extend(opts, uiSortableConfig);
28+
var callbacks = {
29+
receive: null,
30+
remove: null,
31+
start: null,
32+
stop: null,
33+
update: null
34+
};
4335

44-
if (ngModel) {
36+
var apply = function(e, ui) {
37+
if (ui.item.sortable.resort || ui.item.sortable.relocate) {
38+
scope.$apply();
39+
}
40+
};
4541

46-
ngModel.$render = function() {
47-
element.sortable( 'refresh' );
48-
};
42+
angular.extend(opts, uiSortableConfig);
4943

50-
callbacks.start = function(e, ui) {
51-
// Save position of dragged item
52-
ui.item.sortable = { index: ui.item.index() };
53-
};
44+
if (ngModel) {
5445

55-
callbacks.update = function(e, ui) {
56-
// For some reason the reference to ngModel in stop() is wrong
57-
ui.item.sortable.resort = ngModel;
58-
};
46+
ngModel.$render = function() {
47+
element.sortable('refresh');
48+
};
5949

60-
callbacks.receive = function(e, ui) {
61-
ui.item.sortable.relocate = true;
62-
// added item to array into correct position and set up flag
63-
ngModel.$modelValue.splice(ui.item.index(), 0, ui.item.sortable.moved);
50+
callbacks.start = function(e, ui) {
51+
// Save position of dragged item
52+
ui.item.sortable = {
53+
index: ui.item.index()
6454
};
55+
};
6556

66-
callbacks.remove = function(e, ui) {
67-
// copy data into item
68-
if (ngModel.$modelValue.length === 1) {
69-
ui.item.sortable.moved = ngModel.$modelValue.splice(0, 1)[0];
70-
} else {
71-
ui.item.sortable.moved = ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0];
72-
}
73-
};
57+
callbacks.update = function(e, ui) {
58+
// For some reason the reference to ngModel in stop() is wrong
59+
ui.item.sortable.resort = ngModel;
60+
};
7461

75-
callbacks.stop = function(e, ui) {
76-
// digest all prepared changes
77-
if (ui.item.sortable.resort && !ui.item.sortable.relocate) {
62+
callbacks.receive = function(e, ui) {
63+
ui.item.sortable.relocate = true;
64+
// added item to array into correct position and set up flag
65+
ngModel.$modelValue.splice(ui.item.index(), 0, ui.item.sortable.moved);
66+
};
7867

79-
// Fetch saved and current position of dropped element
80-
var end, start;
81-
start = ui.item.sortable.index;
82-
end = ui.item.index();
68+
callbacks.remove = function(e, ui) {
69+
// copy data into item
70+
if (ngModel.$modelValue.length === 1) {
71+
ui.item.sortable.moved = ngModel.$modelValue.splice(0, 1)[0];
72+
} else {
73+
ui.item.sortable.moved = ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0];
74+
}
75+
};
8376

84-
// Reorder array and apply change to scope
85-
ui.item.sortable.resort.$modelValue.splice(end, 0, ui.item.sortable.resort.$modelValue.splice(start, 1)[0]);
77+
callbacks.stop = function(e, ui) {
78+
// digest all prepared changes
79+
if (ui.item.sortable.resort && !ui.item.sortable.relocate) {
8680

87-
}
88-
};
81+
// Fetch saved and current position of dropped element
82+
var end, start;
83+
start = ui.item.sortable.index;
84+
end = ui.item.index();
8985

90-
scope.$watch(attrs.uiSortable, function(newVal){
91-
angular.forEach(newVal, function(value, key){
86+
// Reorder array and apply change to scope
87+
ui.item.sortable.resort.$modelValue.splice(end, 0, ui.item.sortable.resort.$modelValue.splice(start, 1)[0]);
9288

93-
if( callbacks[key] ){
94-
// wrap the callback
95-
value = combineCallbacks( callbacks[key], value );
89+
}
90+
};
9691

97-
if ( key === 'stop' ){
98-
// call apply after stop
99-
value = combineCallbacks( value, apply );
100-
}
101-
}
92+
scope.$watch(attrs.uiSortable, function(newVal) {
93+
angular.forEach(newVal, function(value, key) {
10294

103-
element.sortable('option', key, value);
104-
});
105-
}, true);
95+
if (callbacks[key]) {
96+
// wrap the callback
97+
value = combineCallbacks(callbacks[key], value);
10698

107-
angular.forEach(callbacks, function(value, key ){
99+
if (key === 'stop') {
100+
// call apply after stop
101+
value = combineCallbacks(value, apply);
102+
}
103+
}
108104

109-
opts[key] = combineCallbacks(value, opts[key]);
105+
element.sortable('option', key, value);
110106
});
107+
}, true);
111108

112-
// call apply after stop
113-
opts.stop = combineCallbacks( opts.stop, apply );
109+
angular.forEach(callbacks, function(value, key) {
114110

115-
} else {
116-
log.info('ui.sortable: ngModel not provided!', element);
117-
}
111+
opts[key] = combineCallbacks(value, opts[key]);
112+
});
113+
114+
// call apply after stop
115+
opts.stop = combineCallbacks(opts.stop, apply);
118116

119-
// Create sortable
120-
element.sortable(opts);
117+
} else {
118+
log.info('ui.sortable: ngModel not provided!', element);
121119
}
122-
};
123-
}
124-
]);
120+
121+
// Create sortable
122+
element.sortable(opts);
123+
}
124+
};
125+
}
126+
]);

0 commit comments

Comments
 (0)