├── bower.json ├── package.json ├── LICENSE ├── README.md └── lib └── html5-sortable.js /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "html5-sortable-angularjs", 3 | "version": "0.0.1", 4 | "main": "lib/html5-sortable.js", 5 | "homepage": "http://bachvtuan.github.io/html5-sortable-angularjs/", 6 | "authors": [ 7 | "bachvtuan " 8 | ], 9 | "ignore": [ 10 | ".bowerrc", 11 | ".gitignore", 12 | ".jshintignore", 13 | ".jshintrc", 14 | "bower.json", 15 | "README.md", 16 | "LICENSE" 17 | ], 18 | "description": "HTML5 sortable for AngularJS", 19 | "keywords": [ 20 | "html5", 21 | "sortable", 22 | "angularjs" 23 | ], 24 | "license": "MIT" 25 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "html5-sortable-angularjs", 3 | "version": "0.0.1", 4 | "main": "lib/html5-sortable.js", 5 | "homepage": "http://bachvtuan.github.io/html5-sortable-angularjs/", 6 | "author": "bachvtuan ", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+ssh://git@github.com/bachvtuan/html5-sortable-angularjs.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/bachvtuan/html5-sortable-angularjs/issues" 16 | }, 17 | "description": "HTML5 sortable for AngularJS", 18 | "keywords": [ 19 | "html5", 20 | "sortable", 21 | "angularjs" 22 | ], 23 | "license": "MIT" 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 HTML5 Sortable Angularjs by bachvtuan - http://dethoima.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HTML5 SORTABLE FOR ANGULARJS 2 | ============================= 3 | 4 | I used sortable which provided by jqueryUI at this [link](https://github.com/angular-ui/ui-sortable), That's good but it's pretty heavy to use because not just sortable code is included, I just need sortable method, So I decided make sortable items by html5 instead, almost modern browsers support this. 5 | Below is some reference links that help me get an overview before coding this directive. 6 | 7 | - [UI.Sortable directive](https://github.com/angular-ui/ui-sortable) 8 | 9 | - [Native HTML5 Drag and Drop](http://www.html5rocks.com/en/tutorials/dnd/basics/) 10 | 11 | ## Demo 12 | 13 | [DEMO LINK](http://bachvtuan.github.io/html5-sortable-angularjs/) 14 | 15 | ## Directive features 16 | 1. Native html5 sortable( jquery no longer required ). 17 | 2. Support sortable which array is given ( pass by ngModel) 18 | 3. You can choose specific handle element on item element. 19 | 4. Offer callback when init directive or after item is dropped. 20 | 5. Auto update sortable DOM when ngmodel is changed or removed sub item. 21 | 6. Cross dropping 22 | 23 | ## Bower 24 | 25 | ``` 26 | bower install html5-sortable-angularjs 27 | ``` 28 | 29 | ## NPM 30 | 31 | ``` 32 | npm i html5-sortable-angularjs 33 | ``` 34 | 35 | 36 | ## How to use 37 | Include html5.sortable to your app 38 | 39 | var app = angular.module('app', [ 40 | 'html5.sortable' 41 | ]); 42 | 43 | Define any varriable in scope with type is array 44 | 45 | $scope.list = [ 46 | {id:1,letter:'A'}, 47 | {id:2,letter:'B'}, 48 | {id:3,letter:'C'}, 49 | {id:4,letter:'D'}, 50 | ]; 51 | Define sortable options 52 | 53 | //Options for sortable code 54 | $scope.sortable_option = { 55 | //Only allow draggable when click on handle element 56 | handle:'p.handle', 57 | //Construct method before sortable code 58 | construct:function(model){ 59 | for ( var i = 0; i < model.length; i++ ){ 60 | model[i].letter +=" (constructed)"; 61 | } 62 | }, 63 | //Callback after item is dropped 64 | stop:function(list, dropped_index){ 65 | list[ dropped_index].letter += " Dropped"; 66 | } 67 | }; 68 | 69 | 70 | 71 | **Below is example in template code** 72 | 73 |
74 |
75 |
{{item.letter}}
76 |

DRAG

77 |
78 |
79 | 80 | If you don't want using option, you can edit to html-sortable="" in template. 81 | 82 | ## LICENSE 83 | 84 | MIT 85 | 86 | ### Done 87 | Have fun ! -------------------------------------------------------------------------------- /lib/html5-sortable.js: -------------------------------------------------------------------------------- 1 | /* 2 | Author:bachvtuan@gmail.com 3 | https://github.com/bachvtuan/html5-sortable-angularjs 4 | A directive that support sortable list via html5 for angularjs 5 | Read the readme.md and take a look at example code before using. 6 | */ 7 | 8 | var sortable_app = angular.module('html5.sortable', []); 9 | sortable_app.directive('htmlSortable', ["$parse", "$timeout", "$log", "$window", function($parse, $timeout, $log, $window) { 10 | 11 | return { 12 | restrict: 'A', 13 | require: '?ngModel', 14 | scope: { 15 | htmlSortable: '=', 16 | ngModel : '=', 17 | ngExtraSortable:'=' 18 | }, 19 | 20 | //scope: true, // optionally create a child scope 21 | link: function(scope, element, attrs,ngModel) { 22 | //var model = $parse(attrs.htmlSortable); 23 | /*attrs.html5Sortable*/ 24 | 25 | var sortable = {}; 26 | sortable.is_handle = false; 27 | sortable.in_use = false; 28 | 29 | sortable.handleDragStart = function(e) { 30 | 31 | if ( sortable.options && angular.isDefined(sortable.options.disableDrag) ) { 32 | if ( sortable.options.disableDrag(ngModel.$modelValue, angular.element(this)) === true ) { 33 | e.preventDefault(); 34 | return; 35 | } 36 | } 37 | 38 | $window['drag_source'] = null; 39 | $window['drag_source_extra'] = null; 40 | 41 | if ( sortable.options && !sortable.is_handle && sortable.options.handle ){ 42 | e.preventDefault(); 43 | return; 44 | } 45 | 46 | sortable.is_handle = false; 47 | e.dataTransfer.effectAllowed = 'move'; 48 | //Fixed on firefox and IE 11 49 | if (sortable.browser != "IE"){ 50 | e.dataTransfer.setData('text/plain', 'anything'); 51 | } 52 | 53 | 54 | $window['drag_source'] = this; 55 | $window['drag_source_extra'] = element.extra_data; 56 | 57 | // this/e.target is the source node. 58 | this.classList.add('moving'); 59 | }; 60 | 61 | sortable.handleDragOver = function(e) { 62 | if (e.preventDefault) { 63 | e.preventDefault(); // Allows us to drop. 64 | } 65 | 66 | e.dataTransfer.dropEffect = 'move'; 67 | 68 | if ( !this.classList.contains('over') ){ 69 | this.classList.add('over'); 70 | } 71 | 72 | //return false; 73 | }; 74 | 75 | sortable.handleDragEnter = function(e) { 76 | e.preventDefault(); // Allows us to drop on mobile with HTML5 Drag and Drop Shim 77 | if ( !this.classList.contains('over') ){ 78 | this.classList.add('over'); 79 | } 80 | }; 81 | 82 | sortable.handleDragLeave = function(e) { 83 | this.classList.remove('over'); 84 | }; 85 | 86 | sortable.handleDrop = function(e) { 87 | // this/e.target is current target element. 88 | if (e.stopPropagation) { 89 | // stops the browser from redirecting. 90 | e.stopPropagation(); 91 | } 92 | e.preventDefault(); 93 | this.classList.remove('over'); 94 | 95 | // Don't do anything if we're dropping on the same column we're dragging. 96 | if ( $window['drag_source'] != this) { 97 | 98 | if ( $window['drag_source'] == null){ 99 | $log.info("Invalid sortable"); 100 | return; 101 | } 102 | 103 | 104 | var source_model = $window['drag_source'].model; 105 | var drop_index = this.index; 106 | 107 | if (ngModel.$modelValue.indexOf(source_model) != -1){ 108 | 109 | var drag_index = $window['drag_source'].index; 110 | var temp = angular.copy(ngModel.$modelValue[drag_index]); 111 | 112 | sortable.unbind(); 113 | 114 | ngModel.$modelValue.splice(drag_index,1); 115 | ngModel.$modelValue.splice(drop_index,0, temp); 116 | 117 | } 118 | else if ( sortable.options.allow_cross ){ 119 | ngModel.$modelValue.splice(drop_index,0, source_model); 120 | } 121 | else{ 122 | $log.info("disabled cross dropping"); 123 | return; 124 | } 125 | 126 | //return; 127 | scope.$apply(); 128 | 129 | if ( sortable.options && angular.isDefined(sortable.options.stop) ){ 130 | $log.info('Make callback'); 131 | sortable.options.stop(ngModel.$modelValue,drop_index, 132 | element.extra_data,$window['drag_source_extra']); 133 | } 134 | } 135 | 136 | return false; 137 | }; 138 | 139 | sortable.handleDragEnd = function(e) { 140 | // this/e.target is the source node. 141 | [].forEach.call(sortable.cols_, function (col) { 142 | col.classList.remove('over'); 143 | col.classList.remove('moving'); 144 | }); 145 | 146 | }; 147 | 148 | //Unbind all events are registed before 149 | sortable.unbind = function(){ 150 | 151 | $log.info('Unbind sortable'); 152 | [].forEach.call(sortable.cols_, function (col) { 153 | col.removeAttribute('draggable'); 154 | col.removeEventListener('dragstart', sortable.handleDragStart, false); 155 | col.removeEventListener('dragenter', sortable.handleDragEnter, false); 156 | col.removeEventListener('dragover', sortable.handleDragOver, false); 157 | col.removeEventListener('dragleave', sortable.handleDragLeave, false); 158 | col.removeEventListener('drop', sortable.handleDrop, false); 159 | col.removeEventListener('dragend', sortable.handleDragEnd, false); 160 | }); 161 | sortable.in_use = false; 162 | } 163 | 164 | sortable.activehandle = function(){ 165 | sortable.is_handle = true; 166 | } 167 | 168 | sortable.register_drop = function(element_children){ 169 | element_children.addEventListener('drop', sortable.handleDrop, false); 170 | element_children.addEventListener('dragstart', sortable.handleDragStart, false); 171 | element_children.addEventListener('dragenter', sortable.handleDragEnter, false); 172 | element_children.addEventListener('dragover', sortable.handleDragOver, false); 173 | element_children.addEventListener('dragleave', sortable.handleDragLeave, false); 174 | element_children.addEventListener('drop', sortable.handleDrop, false); 175 | element_children.addEventListener('dragend', sortable.handleDragEnd, false); 176 | } 177 | 178 | sortable.getBrowser = function(){ 179 | var browser_agent = $window.navigator.userAgent; 180 | if ( browser_agent.indexOf(".NET") != -1 ){ 181 | //IE 11 182 | return "IE"; 183 | } 184 | else if ( browser_agent.indexOf("Firefox") != -1 ){ 185 | return "Firefox"; 186 | } 187 | else{ 188 | return "Chrome"; 189 | } 190 | } 191 | 192 | sortable.update = function(){ 193 | $log.info("Update sortable"); 194 | $window['drag_source'] = null; 195 | var index = 0; 196 | 197 | //This's empty list, so just need listen drop from other 198 | if ( ngModel.$modelValue.length == 0 ){ 199 | if (element[0].children.length >0){ 200 | //Set index = 0( simulate first index ) 201 | element[0].children[0].index = 0; 202 | sortable.register_drop(element[0].children[0]); 203 | } 204 | return; 205 | } 206 | 207 | this.browser = this.getBrowser(); 208 | 209 | this.cols_ = element[0].children; 210 | 211 | [].forEach.call(this.cols_, function (col) { 212 | if ( sortable.options && sortable.options.handle){ 213 | var handle = col.querySelectorAll(sortable.options.handle)[0]; 214 | handle.addEventListener('mousedown', sortable.activehandle, false); 215 | } 216 | 217 | col.index = index; 218 | col.model = ngModel.$modelValue[index]; 219 | 220 | index++; 221 | 222 | col.setAttribute('draggable', 'true'); // Enable columns to be draggable. 223 | sortable.register_drop(col); 224 | }); 225 | 226 | sortable.in_use = true; 227 | } 228 | 229 | if (ngModel) { 230 | ngModel.$render = function() { 231 | $timeout(function(){ 232 | //Init flag indicate the first load sortable is done or not 233 | sortable.first_load = false; 234 | 235 | scope.$watch('ngExtraSortable',function(value){ 236 | element.extra_data = value; 237 | //sortable.extra_data = value; 238 | }); 239 | 240 | scope.$watch('htmlSortable', function(value) { 241 | 242 | sortable.options = angular.copy(value) ; 243 | 244 | if (value == "destroy" ){ 245 | if (sortable.in_use){ 246 | sortable.unbind(); 247 | sortable.in_use = false; 248 | } 249 | return; 250 | } 251 | 252 | if ( !angular.isDefined(sortable.options)){ 253 | sortable.options = {}; 254 | } 255 | 256 | if ( !angular.isDefined(sortable.options.allow_cross)){ 257 | sortable.options.allow_cross = false 258 | } 259 | 260 | if ( angular.isDefined(sortable.options.construct) ){ 261 | sortable.options.construct(ngModel.$modelValue); 262 | } 263 | 264 | element[0].classList.add('html5-sortable'); 265 | sortable.update(); 266 | $timeout(function(){ 267 | sortable.first_load = true; 268 | }) 269 | }, true); 270 | 271 | //Watch ngModel and narrate it 272 | scope.$watch('ngModel', function(value) { 273 | if ( !sortable.first_load || sortable.options == 'destroy' ){ 274 | //Ignore on first load 275 | return; 276 | } 277 | 278 | $timeout(function(){ 279 | sortable.update(); 280 | }); 281 | 282 | },true); 283 | 284 | }); 285 | }; 286 | } 287 | else{ 288 | $log.info('Missing ng-model in template'); 289 | } 290 | } 291 | }; 292 | }]); 293 | --------------------------------------------------------------------------------