app.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. (function () {
  2. 'use strict';
  3. var byId = function (id) { return document.getElementById(id); },
  4. loadScripts = function (desc, callback) {
  5. var deps = [], key, idx = 0;
  6. for (key in desc) {
  7. deps.push(key);
  8. }
  9. (function _next() {
  10. var pid,
  11. name = deps[idx],
  12. script = document.createElement('script');
  13. script.type = 'text/javascript';
  14. script.src = desc[deps[idx]];
  15. pid = setInterval(function () {
  16. if (window[name]) {
  17. clearTimeout(pid);
  18. deps[idx++] = window[name];
  19. if (deps[idx]) {
  20. _next();
  21. } else {
  22. callback.apply(null, deps);
  23. }
  24. }
  25. }, 30);
  26. document.getElementsByTagName('head')[0].appendChild(script);
  27. })()
  28. },
  29. console = window.console;
  30. if (!console.log) {
  31. console.log = function () {
  32. alert([].join.apply(arguments, ' '));
  33. };
  34. }
  35. Sortable.create(byId('foo'), {
  36. group: "words",
  37. animation: 150,
  38. store: {
  39. get: function (sortable) {
  40. var order = localStorage.getItem(sortable.options.group);
  41. return order ? order.split('|') : [];
  42. },
  43. set: function (sortable) {
  44. var order = sortable.toArray();
  45. localStorage.setItem(sortable.options.group, order.join('|'));
  46. }
  47. },
  48. onAdd: function (evt){ console.log('onAdd.foo:', [evt.item, evt.from]); },
  49. onUpdate: function (evt){ console.log('onUpdate.foo:', [evt.item, evt.from]); },
  50. onRemove: function (evt){ console.log('onRemove.foo:', [evt.item, evt.from]); },
  51. onStart:function(evt){ console.log('onStart.foo:', [evt.item, evt.from]);},
  52. onSort:function(evt){ console.log('onStart.foo:', [evt.item, evt.from]);},
  53. onEnd: function(evt){ console.log('onEnd.foo:', [evt.item, evt.from]);}
  54. });
  55. Sortable.create(byId('bar'), {
  56. group: "words",
  57. animation: 150,
  58. onAdd: function (evt){ console.log('onAdd.bar:', evt.item); },
  59. onUpdate: function (evt){ console.log('onUpdate.bar:', evt.item); },
  60. onRemove: function (evt){ console.log('onRemove.bar:', evt.item); },
  61. onStart:function(evt){ console.log('onStart.foo:', evt.item);},
  62. onEnd: function(evt){ console.log('onEnd.foo:', evt.item);}
  63. });
  64. // Multi groups
  65. Sortable.create(byId('multi'), {
  66. animation: 150,
  67. draggable: '.tile',
  68. handle: '.tile__name'
  69. });
  70. [].forEach.call(byId('multi').getElementsByClassName('tile__list'), function (el){
  71. Sortable.create(el, {
  72. group: 'photo',
  73. animation: 150
  74. });
  75. });
  76. // Editable list
  77. var editableList = Sortable.create(byId('editable'), {
  78. animation: 150,
  79. filter: '.js-remove',
  80. onFilter: function (evt) {
  81. evt.item.parentNode.removeChild(evt.item);
  82. }
  83. });
  84. byId('addUser').onclick = function () {
  85. Ply.dialog('prompt', {
  86. title: 'Add',
  87. form: { name: 'name' }
  88. }).done(function (ui) {
  89. var el = document.createElement('li');
  90. el.innerHTML = ui.data.name + '<i class="js-remove">✖</i>';
  91. editableList.el.appendChild(el);
  92. });
  93. };
  94. // Advanced groups
  95. [{
  96. name: 'advanced',
  97. pull: true,
  98. put: true
  99. },
  100. {
  101. name: 'advanced',
  102. pull: 'clone',
  103. put: false
  104. }, {
  105. name: 'advanced',
  106. pull: false,
  107. put: true
  108. }].forEach(function (groupOpts, i) {
  109. Sortable.create(byId('advanced-' + (i + 1)), {
  110. sort: (i != 1),
  111. group: groupOpts,
  112. animation: 150
  113. });
  114. });
  115. // 'handle' option
  116. Sortable.create(byId('handle-1'), {
  117. handle: '.drag-handle',
  118. animation: 150
  119. });
  120. // Angular example
  121. angular.module('todoApp', ['ng-sortable'])
  122. .constant('ngSortableConfig', {onEnd: function() {
  123. console.log('default onEnd()');
  124. }})
  125. .controller('TodoController', ['$scope', function ($scope) {
  126. $scope.todos = [
  127. {text: 'learn angular', done: true},
  128. {text: 'build an angular app', done: false}
  129. ];
  130. $scope.addTodo = function () {
  131. $scope.todos.push({text: $scope.todoText, done: false});
  132. $scope.todoText = '';
  133. };
  134. $scope.remaining = function () {
  135. var count = 0;
  136. angular.forEach($scope.todos, function (todo) {
  137. count += todo.done ? 0 : 1;
  138. });
  139. return count;
  140. };
  141. $scope.archive = function () {
  142. var oldTodos = $scope.todos;
  143. $scope.todos = [];
  144. angular.forEach(oldTodos, function (todo) {
  145. if (!todo.done) $scope.todos.push(todo);
  146. });
  147. };
  148. }])
  149. .controller('TodoControllerNext', ['$scope', function ($scope) {
  150. $scope.todos = [
  151. {text: 'learn Sortable', done: true},
  152. {text: 'use ng-sortable', done: false},
  153. {text: 'Enjoy', done: false}
  154. ];
  155. $scope.remaining = function () {
  156. var count = 0;
  157. angular.forEach($scope.todos, function (todo) {
  158. count += todo.done ? 0 : 1;
  159. });
  160. return count;
  161. };
  162. $scope.sortableConfig = { group: 'todo', animation: 150 };
  163. 'Start End Add Update Remove Sort'.split(' ').forEach(function (name) {
  164. $scope.sortableConfig['on' + name] = console.log.bind(console, name);
  165. });
  166. }]);
  167. })();
  168. // Background
  169. document.addEventListener("DOMContentLoaded", function () {
  170. function setNoiseBackground(el, width, height, opacity) {
  171. var canvas = document.createElement("canvas");
  172. var context = canvas.getContext("2d");
  173. canvas.width = width;
  174. canvas.height = height;
  175. for (var i = 0; i < width; i++) {
  176. for (var j = 0; j < height; j++) {
  177. var val = Math.floor(Math.random() * 255);
  178. context.fillStyle = "rgba(" + val + "," + val + "," + val + "," + opacity + ")";
  179. context.fillRect(i, j, 1, 1);
  180. }
  181. }
  182. el.style.background = "url(" + canvas.toDataURL("image/png") + ")";
  183. }
  184. setNoiseBackground(document.getElementsByTagName('body')[0], 50, 50, 0.02);
  185. }, false);