slash_admin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +95 -0
  3. data/app/assets/config/relax_admin_manifest.js +2 -0
  4. data/app/assets/images/slash_admin/en.png +0 -0
  5. data/app/assets/images/slash_admin/favicon.png +0 -0
  6. data/app/assets/images/slash_admin/fr.png +0 -0
  7. data/app/assets/javascripts/slash_admin/application.js +344 -0
  8. data/app/assets/javascripts/slash_admin/custom.js +0 -0
  9. data/app/assets/stylesheets/slash_admin/alert.scss +29 -0
  10. data/app/assets/stylesheets/slash_admin/application.scss +1469 -0
  11. data/app/assets/stylesheets/slash_admin/colors.scss +17 -0
  12. data/app/assets/stylesheets/slash_admin/custom.scss +0 -0
  13. data/app/controllers/slash_admin/application_controller.rb +17 -0
  14. data/app/controllers/slash_admin/base_controller.rb +45 -0
  15. data/app/controllers/slash_admin/batch_actions_controller.rb +17 -0
  16. data/app/controllers/slash_admin/dashboard_controller.rb +11 -0
  17. data/app/controllers/slash_admin/models_controller.rb +325 -0
  18. data/app/controllers/slash_admin/security/sessions_controller.rb +29 -0
  19. data/app/controllers/slash_admin/selectize_controller.rb +49 -0
  20. data/app/helpers/slash_admin/application_helper.rb +212 -0
  21. data/app/helpers/slash_admin/menu_helper.rb +14 -0
  22. data/app/helpers/slash_admin/widgets_helper.rb +7 -0
  23. data/app/jobs/slash_admin/application_job.rb +4 -0
  24. data/app/mailers/slash_admin/application_mailer.rb +6 -0
  25. data/app/models/slash_admin/admin.rb +46 -0
  26. data/app/models/slash_admin/admin_ability.rb +11 -0
  27. data/app/models/slash_admin/application_record.rb +5 -0
  28. data/app/views/layouts/slash_admin/admin_user.html.erb +16 -0
  29. data/app/views/layouts/slash_admin/application.html.erb +40 -0
  30. data/app/views/slash_admin/base/_data_belongs_to.html.erb +37 -0
  31. data/app/views/slash_admin/base/_data_list.html.erb +276 -0
  32. data/app/views/slash_admin/base/_data_nestable.html.erb +33 -0
  33. data/app/views/slash_admin/base/_data_new.html.erb +16 -0
  34. data/app/views/slash_admin/base/_data_show.html.erb +72 -0
  35. data/app/views/slash_admin/base/_filters.html.erb +60 -0
  36. data/app/views/slash_admin/base/_translatable_fields.html.erb +23 -0
  37. data/app/views/slash_admin/base/edit.html.erb +26 -0
  38. data/app/views/slash_admin/base/index.html.erb +47 -0
  39. data/app/views/slash_admin/base/index.xls.erb +27 -0
  40. data/app/views/slash_admin/base/nestable.html.erb +28 -0
  41. data/app/views/slash_admin/base/new.html.erb +26 -0
  42. data/app/views/slash_admin/base/show.html.erb +22 -0
  43. data/app/views/slash_admin/custom_fields/_color.html.erb +2 -0
  44. data/app/views/slash_admin/custom_fields/_email.html.erb +2 -0
  45. data/app/views/slash_admin/custom_fields/_google_map.html.erb +72 -0
  46. data/app/views/slash_admin/custom_fields/_nested_belongs_to.html.erb +55 -0
  47. data/app/views/slash_admin/custom_fields/_password.html.erb +2 -0
  48. data/app/views/slash_admin/custom_fields/_select.html.erb +2 -0
  49. data/app/views/slash_admin/custom_fields/_tags.html.erb +2 -0
  50. data/app/views/slash_admin/custom_fields/_wysiwyg.html.erb +2 -0
  51. data/app/views/slash_admin/dashboard/home.html.erb +5 -0
  52. data/app/views/slash_admin/dashboard/widgets/_statistic_chart.html.erb +0 -0
  53. data/app/views/slash_admin/dashboard/widgets/_statistic_progress_tile.html.erb +29 -0
  54. data/app/views/slash_admin/fields/_belongs_to.html.erb +9 -0
  55. data/app/views/slash_admin/fields/_boolean.html.erb +2 -0
  56. data/app/views/slash_admin/fields/_carrierwave.html.erb +15 -0
  57. data/app/views/slash_admin/fields/_date.html.erb +2 -0
  58. data/app/views/slash_admin/fields/_decimal.html.erb +2 -0
  59. data/app/views/slash_admin/fields/_form_group.html.erb +12 -0
  60. data/app/views/slash_admin/fields/_has_many.html.erb +12 -0
  61. data/app/views/slash_admin/fields/_has_many_row.html.erb +18 -0
  62. data/app/views/slash_admin/fields/_has_one.html.erb +24 -0
  63. data/app/views/slash_admin/fields/_integer.html.erb +2 -0
  64. data/app/views/slash_admin/fields/_json.html.erb +14 -0
  65. data/app/views/slash_admin/fields/_nested_has_many.html.erb +14 -0
  66. data/app/views/slash_admin/fields/_number.html.erb +2 -0
  67. data/app/views/slash_admin/fields/_string.html.erb +2 -0
  68. data/app/views/slash_admin/fields/_text.html.erb +2 -0
  69. data/app/views/slash_admin/security/sessions/new.html.erb +30 -0
  70. data/app/views/slash_admin/shared/_batch_actions.html.erb +21 -0
  71. data/app/views/slash_admin/shared/_better_paginate.html.erb +30 -0
  72. data/app/views/slash_admin/shared/_breadcrumb.html.erb +30 -0
  73. data/app/views/slash_admin/shared/_debug.html.erb +24 -0
  74. data/app/views/slash_admin/shared/_errors_data_new.html.erb +12 -0
  75. data/app/views/slash_admin/shared/_header.html.erb +38 -0
  76. data/app/views/slash_admin/shared/_menu.html.erb +57 -0
  77. data/app/views/slash_admin/shared/_new_form_buttons.html.erb +9 -0
  78. data/app/views/slash_admin/shared/_sub_header.html.erb +11 -0
  79. data/config/initializers/validators.rb +13 -0
  80. data/config/locales/slash_admin.en.yml +67 -0
  81. data/config/locales/slash_admin.fr.yml +72 -0
  82. data/config/routes.rb +16 -0
  83. data/db/migrate/20170512104248_create_slash_admin_admins.rb +19 -0
  84. data/lib/batch_translation.rb +19 -0
  85. data/lib/generators/slash_admin/controllers/controllers_generator.rb +16 -0
  86. data/lib/generators/slash_admin/controllers/templates/controllers.erb +9 -0
  87. data/lib/generators/slash_admin/install/install_generator.rb +13 -0
  88. data/lib/generators/slash_admin/install/templates/install.erb +14 -0
  89. data/lib/generators/slash_admin/override_admin/override_admin_generator.rb +13 -0
  90. data/lib/generators/slash_admin/override_admin/templates/admin.erb +46 -0
  91. data/lib/generators/slash_admin/override_session/override_session_generator.rb +13 -0
  92. data/lib/generators/slash_admin/override_session/templates/session.erb +27 -0
  93. data/lib/generators/slash_admin/permissions/permissions_generator.rb +13 -0
  94. data/lib/generators/slash_admin/permissions/templates/permissions.erb +11 -0
  95. data/lib/slash_admin.rb +29 -0
  96. data/lib/slash_admin/engine.rb +8 -0
  97. data/lib/slash_admin/version.rb +3 -0
  98. data/lib/tasks/slash_admin_tasks.rake +4 -0
  99. data/vendor/assets/javascripts/bootstrap-material-datetimepicker.js +1295 -0
  100. data/vendor/assets/javascripts/codemirror/codemirror.js +9657 -0
  101. data/vendor/assets/javascripts/codemirror/lint/json-lint.js +37 -0
  102. data/vendor/assets/javascripts/codemirror/lint/jsonlint.js +432 -0
  103. data/vendor/assets/javascripts/codemirror/lint/lint.js +252 -0
  104. data/vendor/assets/javascripts/codemirror/mode/javascript.js +865 -0
  105. data/vendor/assets/javascripts/jquery.nestable.js +910 -0
  106. data/vendor/assets/javascripts/jquery.tagsinput-revisited.min.js +5 -0
  107. data/vendor/assets/javascripts/toastr.js +6 -0
  108. data/vendor/assets/stylesheets/animate.css +1579 -0
  109. data/vendor/assets/stylesheets/bootstrap-material-datetimepicker.css +82 -0
  110. data/vendor/assets/stylesheets/codemirror/codemirror.css +346 -0
  111. data/vendor/assets/stylesheets/codemirror/lint/lint.css +73 -0
  112. data/vendor/assets/stylesheets/codemirror/theme/relax-seti.css +41 -0
  113. data/vendor/assets/stylesheets/jquery.nestable.css +121 -0
  114. data/vendor/assets/stylesheets/jquery.tagsinput-revisited.min.css +1 -0
  115. data/vendor/assets/stylesheets/sweetalert.css +935 -0
  116. data/vendor/assets/stylesheets/toastr.css +1 -0
  117. metadata +509 -0
@@ -0,0 +1,910 @@
1
+ /*!
2
+ * Nestable jQuery Plugin - Copyright (c) 2014 Ramon Smit - https://github.com/RamonSmit/Nestable
3
+ */
4
+ ;
5
+ (function($, window, document, undefined) {
6
+ var hasTouch = 'ontouchstart' in window;
7
+
8
+ /**
9
+ * Detect CSS pointer-events property
10
+ * events are normally disabled on the dragging element to avoid conflicts
11
+ * https://github.com/ausi/Feature-detection-technique-for-pointer-events/blob/master/modernizr-pointerevents.js
12
+ */
13
+ var hasPointerEvents = (function() {
14
+ var el = document.createElement('div'),
15
+ docEl = document.documentElement;
16
+ if(!('pointerEvents' in el.style)) {
17
+ return false;
18
+ }
19
+ el.style.pointerEvents = 'auto';
20
+ el.style.pointerEvents = 'x';
21
+ docEl.appendChild(el);
22
+ var supports = window.getComputedStyle && window.getComputedStyle(el, '').pointerEvents === 'auto';
23
+ docEl.removeChild(el);
24
+ return !!supports;
25
+ })();
26
+
27
+ var eStart = hasTouch ? 'touchstart' : 'mousedown',
28
+ eMove = hasTouch ? 'touchmove' : 'mousemove',
29
+ eEnd = hasTouch ? 'touchend' : 'mouseup',
30
+ eCancel = hasTouch ? 'touchcancel' : 'mouseup';
31
+
32
+ var defaults = {
33
+ contentCallback: function(item) {return item.content || '' ? item.content : item.id;},
34
+ listNodeName: 'ol',
35
+ itemNodeName: 'li',
36
+ handleNodeName: 'div',
37
+ contentNodeName: 'span',
38
+ rootClass: 'dd',
39
+ listClass: 'dd-list',
40
+ itemClass: 'dd-item',
41
+ dragClass: 'dd-dragel',
42
+ handleClass: 'dd-handle',
43
+ contentClass: 'dd-content',
44
+ collapsedClass: 'dd-collapsed',
45
+ placeClass: 'dd-placeholder',
46
+ noDragClass: 'dd-nodrag',
47
+ noChildrenClass: 'dd-nochildren',
48
+ emptyClass: 'dd-empty',
49
+ expandBtnHTML: '<button class="dd-expand" data-action="expand" type="button">Expand</button>',
50
+ collapseBtnHTML: '<button class="dd-collapse" data-action="collapse" type="button">Collapse</button>',
51
+ group: 0,
52
+ maxDepth: 5,
53
+ threshold: 20,
54
+ fixedDepth: false, //fixed item's depth
55
+ fixed: false,
56
+ includeContent: false,
57
+ callback: function(l, e, p) {},
58
+ onDragStart: function(l, e, p) {},
59
+ listRenderer: function(children, options) {
60
+ var html = '<' + options.listNodeName + ' class="' + options.listClass + '">';
61
+ html += children;
62
+ html += '</' + options.listNodeName + '>';
63
+
64
+ return html;
65
+ },
66
+ itemRenderer: function(item_attrs, content, children, options, item) {
67
+ var item_attrs_string = $.map(item_attrs, function(value, key) {
68
+ return ' ' + key + '="' + value + '"';
69
+ }).join(' ');
70
+
71
+ var html = '<' + options.itemNodeName + item_attrs_string + '>';
72
+ html += '<' + options.handleNodeName + ' class="' + options.handleClass + '">';
73
+ html += '<' + options.contentNodeName + ' class="' + options.contentClass + '">';
74
+ html += content;
75
+ html += '</' + options.contentNodeName + '>';
76
+ html += '</' + options.handleNodeName + '>';
77
+ html += children;
78
+ html += '</' + options.itemNodeName + '>';
79
+
80
+ return html;
81
+ }
82
+ };
83
+
84
+ function Plugin(element, options) {
85
+ this.w = $(document);
86
+ this.el = $(element);
87
+ if(!options) {
88
+ options = defaults;
89
+ }
90
+ if(options.rootClass !== undefined && options.rootClass !== 'dd') {
91
+ options.listClass = options.listClass ? options.listClass : options.rootClass + '-list';
92
+ options.itemClass = options.itemClass ? options.itemClass : options.rootClass + '-item';
93
+ options.dragClass = options.dragClass ? options.dragClass : options.rootClass + '-dragel';
94
+ options.handleClass = options.handleClass ? options.handleClass : options.rootClass + '-handle';
95
+ options.collapsedClass = options.collapsedClass ? options.collapsedClass : options.rootClass + '-collapsed';
96
+ options.placeClass = options.placeClass ? options.placeClass : options.rootClass + '-placeholder';
97
+ options.noDragClass = options.noDragClass ? options.noDragClass : options.rootClass + '-nodrag';
98
+ options.noChildrenClass = options.noChildrenClass ? options.noChildrenClass : options.rootClass + '-nochildren';
99
+ options.emptyClass = options.emptyClass ? options.emptyClass : options.rootClass + '-empty';
100
+ }
101
+
102
+ this.options = $.extend({}, defaults, options);
103
+
104
+ // build HTML from serialized JSON if passed
105
+ if(this.options.json !== undefined) {
106
+ this._build();
107
+ }
108
+
109
+ this.init();
110
+ }
111
+
112
+ Plugin.prototype = {
113
+
114
+ init: function() {
115
+ var list = this;
116
+
117
+ list.reset();
118
+
119
+ list.el.data('nestable-group', this.options.group);
120
+
121
+ list.placeEl = $('<div class="' + list.options.placeClass + '"/>');
122
+
123
+ $.each(this.el.find(list.options.itemNodeName), function(k, el) {
124
+ var item = $(el),
125
+ parent = item.parent();
126
+ list.setParent(item);
127
+ if(parent.hasClass(list.options.collapsedClass)) {
128
+ list.collapseItem(parent.parent());
129
+ }
130
+ });
131
+
132
+ list.el.on('click', 'button', function(e) {
133
+ if(list.dragEl || (!hasTouch && e.button !== 0)) {
134
+ return;
135
+ }
136
+ var target = $(e.currentTarget),
137
+ action = target.data('action'),
138
+ item = target.parents(list.options.itemNodeName).eq(0);
139
+ if(action === 'collapse') {
140
+ list.collapseItem(item);
141
+ }
142
+ if(action === 'expand') {
143
+ list.expandItem(item);
144
+ }
145
+ });
146
+
147
+ var onStartEvent = function(e) {
148
+ var handle = $(e.target);
149
+ if(!handle.hasClass(list.options.handleClass)) {
150
+ if(handle.closest('.' + list.options.noDragClass).length) {
151
+ return;
152
+ }
153
+ handle = handle.closest('.' + list.options.handleClass);
154
+ }
155
+ if(!handle.length || list.dragEl || (!hasTouch && e.which !== 1) || (hasTouch && e.touches.length !== 1)) {
156
+ return;
157
+ }
158
+ e.preventDefault();
159
+ list.dragStart(hasTouch ? e.touches[0] : e);
160
+ };
161
+
162
+ var onMoveEvent = function(e) {
163
+ if(list.dragEl) {
164
+ e.preventDefault();
165
+ list.dragMove(hasTouch ? e.touches[0] : e);
166
+ }
167
+ };
168
+
169
+ var onEndEvent = function(e) {
170
+ if(list.dragEl) {
171
+ e.preventDefault();
172
+ list.dragStop(hasTouch ? e.touches[0] : e);
173
+ }
174
+ };
175
+
176
+ if(hasTouch) {
177
+ list.el[0].addEventListener(eStart, onStartEvent, false);
178
+ window.addEventListener(eMove, onMoveEvent, false);
179
+ window.addEventListener(eEnd, onEndEvent, false);
180
+ window.addEventListener(eCancel, onEndEvent, false);
181
+ }
182
+ else {
183
+ list.el.on(eStart, onStartEvent);
184
+ list.w.on(eMove, onMoveEvent);
185
+ list.w.on(eEnd, onEndEvent);
186
+ }
187
+
188
+ var destroyNestable = function()
189
+ {
190
+ if(hasTouch) {
191
+ list.el[0].removeEventListener(eStart, onStartEvent, false);
192
+ window.removeEventListener(eMove, onMoveEvent, false);
193
+ window.removeEventListener(eEnd, onEndEvent, false);
194
+ window.removeEventListener(eCancel, onEndEvent, false);
195
+ }
196
+ else {
197
+ list.el.off(eStart, onStartEvent);
198
+ list.w.off(eMove, onMoveEvent);
199
+ list.w.off(eEnd, onEndEvent);
200
+ }
201
+
202
+ list.el.off('click');
203
+ list.el.unbind('destroy-nestable');
204
+
205
+ list.el.data("nestable", null);
206
+ };
207
+
208
+ list.el.bind('destroy-nestable', destroyNestable);
209
+
210
+ },
211
+
212
+ destroy: function ()
213
+ {
214
+ this.el.trigger('destroy-nestable');
215
+ },
216
+
217
+ add: function (item)
218
+ {
219
+ var listClassSelector = '.' + this.options.listClass;
220
+ var tree = $(this.el).children(listClassSelector);
221
+
222
+ if (item.parent_id !== undefined) {
223
+ tree = tree.find('[data-id="' + item.parent_id + '"]');
224
+ delete item.parent_id;
225
+
226
+ if (tree.children(listClassSelector).length === 0) {
227
+ tree = tree.append(this.options.listRenderer('', this.options))
228
+ }
229
+
230
+ tree = tree.find(listClassSelector);
231
+ }
232
+
233
+ tree.append(this._buildItem(item, this.options));
234
+ },
235
+
236
+ replace: function (item)
237
+ {
238
+ var html = this._buildItem(item, this.options);
239
+
240
+ this._getItemById(item.id)
241
+ .html(html);
242
+ },
243
+
244
+ remove: function (itemId)
245
+ {
246
+ var options = this.options;
247
+ var buttonsSelector = '[data-action="expand"], [data-action="collapse"]';
248
+
249
+ this._getItemById(itemId)
250
+ .remove();
251
+
252
+ // remove empty children lists
253
+ var emptyListsSelector = '.' + options.listClass + ':not(:has(*))';
254
+ $(this.el).find(emptyListsSelector).remove();
255
+
256
+ // remove buttons if parents do not have children
257
+ $(this.el).find(buttonsSelector).each(function() {
258
+ var siblings = $(this).siblings('.' + options.listClass);
259
+ if (siblings.length === 0) {
260
+ $(this).remove();
261
+ }
262
+ });
263
+ },
264
+
265
+ _getItemById: function(itemId) {
266
+ return $(this.el).children('.' + this.options.listClass)
267
+ .find('[data-id="' + itemId + '"]');
268
+ },
269
+
270
+ _build: function() {
271
+ var json = this.options.json;
272
+
273
+ if(typeof json === 'string') {
274
+ json = JSON.parse(json);
275
+ }
276
+
277
+ $(this.el).html(this._buildList(json, this.options));
278
+ },
279
+
280
+ _buildList: function(items, options) {
281
+ if(!items) {
282
+ return '';
283
+ }
284
+
285
+ var children = '';
286
+ var that = this;
287
+
288
+ $.each(items, function(index, sub) {
289
+ children += that._buildItem(sub, options);
290
+ });
291
+
292
+ return options.listRenderer(children, options);
293
+ },
294
+
295
+ _buildItem: function(item, options) {
296
+ function escapeHtml(text) {
297
+ var map = {
298
+ '&': '&amp;',
299
+ '<': '&lt;',
300
+ '>': '&gt;',
301
+ '"': '&quot;',
302
+ "'": '&#039;'
303
+ };
304
+
305
+ return text + "".replace(/[&<>"']/g, function(m) { return map[m]; });
306
+ }
307
+
308
+ function filterClasses(classes) {
309
+ var new_classes = {};
310
+
311
+ for(var k in classes) {
312
+ // Remove duplicates
313
+ new_classes[classes[k]] = classes[k];
314
+ }
315
+
316
+ return new_classes;
317
+ }
318
+
319
+ function createClassesString(item, options) {
320
+ var classes = item.classes || {};
321
+
322
+ if(typeof classes == 'string') {
323
+ classes = [classes];
324
+ }
325
+
326
+ var item_classes = filterClasses(classes);
327
+ item_classes[options.itemClass] = options.itemClass;
328
+
329
+ // create class string
330
+ return $.map(item_classes, function(val) {
331
+ return val;
332
+ }).join(' ');
333
+ }
334
+
335
+ function createDataAttrs(attr) {
336
+ attr = $.extend({}, attr);
337
+
338
+ delete attr.children;
339
+ delete attr.classes;
340
+ delete attr.content;
341
+
342
+ var data_attrs = {};
343
+
344
+ $.each(attr, function(key, value) {
345
+ if(typeof value == 'object') {
346
+ value = JSON.stringify(value);
347
+ }
348
+
349
+ data_attrs["data-" + key] = escapeHtml(value);
350
+ });
351
+
352
+ return data_attrs;
353
+ }
354
+
355
+ var item_attrs = createDataAttrs(item);
356
+ item_attrs["class"] = createClassesString(item, options);
357
+
358
+ var content = options.contentCallback(item);
359
+ var children = this._buildList(item.children, options);
360
+
361
+ return options.itemRenderer(item_attrs, content, children, options, item);
362
+ },
363
+
364
+ serialize: function() {
365
+ var data, list = this, step = function(level) {
366
+ var array = [],
367
+ items = level.children(list.options.itemNodeName);
368
+ items.each(function() {
369
+ var li = $(this),
370
+ item = $.extend({}, li.data()),
371
+ sub = li.children(list.options.listNodeName);
372
+
373
+ if(list.options.includeContent) {
374
+ var content = li.find('.' + list.options.contentClass).html();
375
+
376
+ if(content) {
377
+ item.content = content;
378
+ }
379
+ }
380
+
381
+ if(sub.length) {
382
+ item.children = step(sub);
383
+ }
384
+ array.push(item);
385
+ });
386
+ return array;
387
+ };
388
+ data = step(list.el.find(list.options.listNodeName).first());
389
+ return data;
390
+ },
391
+
392
+ asNestedSet: function() {
393
+ var list = this, o = list.options, depth = -1, ret = [], lft = 1;
394
+ var items = list.el.find(o.listNodeName).first().children(o.itemNodeName);
395
+
396
+ items.each(function () {
397
+ lft = traverse(this, depth + 1, lft);
398
+ });
399
+
400
+ ret = ret.sort(function(a,b){ return (a.lft - b.lft); });
401
+ return ret;
402
+
403
+ function traverse(item, depth, lft) {
404
+ var rgt = lft + 1, id, pid;
405
+
406
+ if ($(item).children(o.listNodeName).children(o.itemNodeName).length > 0 ) {
407
+ depth++;
408
+ $(item).children(o.listNodeName).children(o.itemNodeName).each(function () {
409
+ rgt = traverse($(this), depth, rgt);
410
+ });
411
+ depth--;
412
+ }
413
+
414
+ id = parseInt($(item).attr('data-id'));
415
+ pid = parseInt($(item).parent(o.listNodeName).parent(o.itemNodeName).attr('data-id')) || '';
416
+
417
+ if (id) {
418
+ ret.push({"id": id, "parent_id": pid, "depth": depth, "lft": lft, "rgt": rgt});
419
+ }
420
+
421
+ lft = rgt + 1;
422
+ return lft;
423
+ }
424
+ },
425
+
426
+ returnOptions: function() {
427
+ return this.options;
428
+ },
429
+
430
+ serialise: function() {
431
+ return this.serialize();
432
+ },
433
+
434
+ toHierarchy: function(options) {
435
+
436
+ var o = $.extend({}, this.options, options),
437
+ sDepth = o.startDepthCount || 0,
438
+ ret = [];
439
+
440
+ $(this.element).children(o.items).each(function() {
441
+ var level = _recursiveItems(this);
442
+ ret.push(level);
443
+ });
444
+
445
+ return ret;
446
+
447
+ function _recursiveItems(item) {
448
+ var id = ($(item).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
449
+ if (id) {
450
+ var currentItem = {
451
+ "id": id[2]
452
+ };
453
+ if ($(item).children(o.listType).children(o.items).length > 0) {
454
+ currentItem.children = [];
455
+ $(item).children(o.listType).children(o.items).each(function() {
456
+ var level = _recursiveItems(this);
457
+ currentItem.children.push(level);
458
+ });
459
+ }
460
+ return currentItem;
461
+ }
462
+ }
463
+ },
464
+
465
+ toArray: function() {
466
+
467
+ var o = $.extend({}, this.options, this),
468
+ sDepth = o.startDepthCount || 0,
469
+ ret = [],
470
+ left = 2,
471
+ list = this,
472
+ element = list.el.find(list.options.listNodeName).first();
473
+
474
+ var items = element.children(list.options.itemNodeName);
475
+ items.each(function() {
476
+ left = _recursiveArray($(this), sDepth + 1, left);
477
+ });
478
+
479
+ ret = ret.sort(function(a, b) {
480
+ return (a.left - b.left);
481
+ });
482
+
483
+ return ret;
484
+
485
+ function _recursiveArray(item, depth, left) {
486
+
487
+ var right = left + 1,
488
+ id,
489
+ pid;
490
+ var new_item = item.children(o.options.listNodeName).children(o.options.itemNodeName); /// .data()
491
+
492
+ if (item.children(o.options.listNodeName).children(o.options.itemNodeName).length > 0) {
493
+ depth++;
494
+ item.children(o.options.listNodeName).children(o.options.itemNodeName).each(function() {
495
+ right = _recursiveArray($(this), depth, right);
496
+ });
497
+ depth--;
498
+ }
499
+
500
+ id = item.data().id;
501
+
502
+
503
+ if (depth === sDepth + 1) {
504
+ pid = o.rootID;
505
+ } else {
506
+
507
+ var parentItem = (item.parent(o.options.listNodeName)
508
+ .parent(o.options.itemNodeName)
509
+ .data());
510
+ pid = parentItem.id;
511
+
512
+ }
513
+
514
+ if (id) {
515
+ ret.push({
516
+ "id": id,
517
+ "parent_id": pid,
518
+ "depth": depth,
519
+ "left": left,
520
+ "right": right
521
+ });
522
+ }
523
+
524
+ left = right + 1;
525
+ return left;
526
+ }
527
+
528
+ },
529
+
530
+ reset: function() {
531
+ this.mouse = {
532
+ offsetX: 0,
533
+ offsetY: 0,
534
+ startX: 0,
535
+ startY: 0,
536
+ lastX: 0,
537
+ lastY: 0,
538
+ nowX: 0,
539
+ nowY: 0,
540
+ distX: 0,
541
+ distY: 0,
542
+ dirAx: 0,
543
+ dirX: 0,
544
+ dirY: 0,
545
+ lastDirX: 0,
546
+ lastDirY: 0,
547
+ distAxX: 0,
548
+ distAxY: 0
549
+ };
550
+ this.moving = false;
551
+ this.dragEl = null;
552
+ this.dragRootEl = null;
553
+ this.dragDepth = 0;
554
+ this.hasNewRoot = false;
555
+ this.pointEl = null;
556
+ },
557
+
558
+ expandItem: function(li) {
559
+ li.removeClass(this.options.collapsedClass);
560
+ },
561
+
562
+ collapseItem: function(li) {
563
+ var lists = li.children(this.options.listNodeName);
564
+ if(lists.length) {
565
+ li.addClass(this.options.collapsedClass);
566
+ }
567
+ },
568
+
569
+ expandAll: function() {
570
+ var list = this;
571
+ list.el.find(list.options.itemNodeName).each(function() {
572
+ list.expandItem($(this));
573
+ });
574
+ },
575
+
576
+ collapseAll: function() {
577
+ var list = this;
578
+ list.el.find(list.options.itemNodeName).each(function() {
579
+ list.collapseItem($(this));
580
+ });
581
+ },
582
+
583
+ setParent: function(li) {
584
+ if(li.children(this.options.listNodeName).length) {
585
+ // make sure NOT showing two or more sets data-action buttons
586
+ li.children('[data-action]').remove();
587
+ li.prepend($(this.options.expandBtnHTML));
588
+ li.prepend($(this.options.collapseBtnHTML));
589
+ }
590
+ },
591
+
592
+ unsetParent: function(li) {
593
+ li.removeClass(this.options.collapsedClass);
594
+ li.children('[data-action]').remove();
595
+ li.children(this.options.listNodeName).remove();
596
+ },
597
+
598
+ dragStart: function(e) {
599
+ var mouse = this.mouse,
600
+ target = $(e.target),
601
+ dragItem = target.closest(this.options.itemNodeName);
602
+
603
+ var position = {};
604
+ position.top = e.pageY;
605
+ position.left = e.pageX;
606
+
607
+ this.options.onDragStart.call(this, this.el, dragItem, position);
608
+
609
+ this.placeEl.css('height', dragItem.height());
610
+
611
+ mouse.offsetX = e.pageX - dragItem.offset().left;
612
+ mouse.offsetY = e.pageY - dragItem.offset().top;
613
+ mouse.startX = mouse.lastX = e.pageX;
614
+ mouse.startY = mouse.lastY = e.pageY;
615
+
616
+ this.dragRootEl = this.el;
617
+ this.dragEl = $(document.createElement(this.options.listNodeName)).addClass(this.options.listClass + ' ' + this.options.dragClass);
618
+ this.dragEl.css('width', dragItem.outerWidth());
619
+
620
+ this.setIndexOfItem(dragItem);
621
+
622
+ // fix for zepto.js
623
+ //dragItem.after(this.placeEl).detach().appendTo(this.dragEl);
624
+ dragItem.after(this.placeEl);
625
+ dragItem[0].parentNode.removeChild(dragItem[0]);
626
+ dragItem.appendTo(this.dragEl);
627
+
628
+ $(document.body).append(this.dragEl);
629
+ this.dragEl.css({
630
+ 'left': e.pageX - mouse.offsetX,
631
+ 'top': e.pageY - mouse.offsetY
632
+ });
633
+ // total depth of dragging item
634
+ var i, depth,
635
+ items = this.dragEl.find(this.options.itemNodeName);
636
+ for(i = 0; i < items.length; i++) {
637
+ depth = $(items[i]).parents(this.options.listNodeName).length;
638
+ if(depth > this.dragDepth) {
639
+ this.dragDepth = depth;
640
+ }
641
+ }
642
+ },
643
+
644
+ setIndexOfItem: function(item, index) {
645
+ if((typeof index) === 'undefined') {
646
+ index = [];
647
+ }
648
+
649
+ index.unshift(item.index());
650
+
651
+ if($(item[0].parentNode)[0] !== this.dragRootEl[0]) {
652
+ this.setIndexOfItem($(item[0].parentNode), index);
653
+ }
654
+ else {
655
+ this.dragEl.data('indexOfItem', index);
656
+ }
657
+ },
658
+
659
+ restoreItemAtIndex: function(dragElement) {
660
+ var indexArray = this.dragEl.data('indexOfItem'),
661
+ currentEl = this.el;
662
+
663
+ for(i = 0; i < indexArray.length; i++) {
664
+ if((indexArray.length - 1) === parseInt(i)) {
665
+ placeElement(currentEl, dragElement);
666
+ return
667
+ }
668
+ currentEl = currentEl[0].children[indexArray[i]];
669
+ }
670
+
671
+ function placeElement(currentEl, dragElement) {
672
+ if(indexArray[indexArray.length - 1] === 0) {
673
+ $(currentEl).prepend(dragElement.clone());
674
+ }
675
+ else {
676
+ $(currentEl.children[indexArray[indexArray.length - 1] - 1]).after(dragElement.clone());
677
+ }
678
+ }
679
+ },
680
+
681
+ dragStop: function(e) {
682
+ // fix for zepto.js
683
+ //this.placeEl.replaceWith(this.dragEl.children(this.options.itemNodeName + ':first').detach());
684
+ var el = this.dragEl.children(this.options.itemNodeName).first();
685
+ el[0].parentNode.removeChild(el[0]);
686
+ this.placeEl.replaceWith(el);
687
+
688
+ var position = {};
689
+ position.top = e.pageY;
690
+ position.left = e.pageX;
691
+
692
+ if(this.hasNewRoot) {
693
+ if(this.options.fixed === true) {
694
+ this.restoreItemAtIndex(el);
695
+ }
696
+ else {
697
+ this.el.trigger('lostItem');
698
+ }
699
+ this.dragRootEl.trigger('gainedItem');
700
+ }
701
+ else {
702
+ this.dragRootEl.trigger('change');
703
+ }
704
+
705
+ this.dragEl.remove();
706
+ this.options.callback.call(this, this.dragRootEl, el, position);
707
+
708
+ this.reset();
709
+ },
710
+
711
+ dragMove: function(e) {
712
+ var list, parent, prev, next, depth,
713
+ opt = this.options,
714
+ mouse = this.mouse;
715
+
716
+ this.dragEl.css({
717
+ 'left': e.pageX - mouse.offsetX,
718
+ 'top': e.pageY - mouse.offsetY
719
+ });
720
+
721
+ // mouse position last events
722
+ mouse.lastX = mouse.nowX;
723
+ mouse.lastY = mouse.nowY;
724
+ // mouse position this events
725
+ mouse.nowX = e.pageX;
726
+ mouse.nowY = e.pageY;
727
+ // distance mouse moved between events
728
+ mouse.distX = mouse.nowX - mouse.lastX;
729
+ mouse.distY = mouse.nowY - mouse.lastY;
730
+ // direction mouse was moving
731
+ mouse.lastDirX = mouse.dirX;
732
+ mouse.lastDirY = mouse.dirY;
733
+ // direction mouse is now moving (on both axis)
734
+ mouse.dirX = mouse.distX === 0 ? 0 : mouse.distX > 0 ? 1 : -1;
735
+ mouse.dirY = mouse.distY === 0 ? 0 : mouse.distY > 0 ? 1 : -1;
736
+ // axis mouse is now moving on
737
+ var newAx = Math.abs(mouse.distX) > Math.abs(mouse.distY) ? 1 : 0;
738
+
739
+ // do nothing on first move
740
+ if(!mouse.moving) {
741
+ mouse.dirAx = newAx;
742
+ mouse.moving = true;
743
+ return;
744
+ }
745
+
746
+ // calc distance moved on this axis (and direction)
747
+ if(mouse.dirAx !== newAx) {
748
+ mouse.distAxX = 0;
749
+ mouse.distAxY = 0;
750
+ }
751
+ else {
752
+ mouse.distAxX += Math.abs(mouse.distX);
753
+ if(mouse.dirX !== 0 && mouse.dirX !== mouse.lastDirX) {
754
+ mouse.distAxX = 0;
755
+ }
756
+ mouse.distAxY += Math.abs(mouse.distY);
757
+ if(mouse.dirY !== 0 && mouse.dirY !== mouse.lastDirY) {
758
+ mouse.distAxY = 0;
759
+ }
760
+ }
761
+ mouse.dirAx = newAx;
762
+
763
+ /**
764
+ * move horizontal
765
+ */
766
+ if(mouse.dirAx && mouse.distAxX >= opt.threshold) {
767
+ // reset move distance on x-axis for new phase
768
+ mouse.distAxX = 0;
769
+ prev = this.placeEl.prev(opt.itemNodeName);
770
+ // increase horizontal level if previous sibling exists, is not collapsed, and can have children
771
+ if(mouse.distX > 0 && prev.length && !prev.hasClass(opt.collapsedClass) && !prev.hasClass(opt.noChildrenClass)) {
772
+ // cannot increase level when item above is collapsed
773
+ list = prev.find(opt.listNodeName).last();
774
+ // check if depth limit has reached
775
+ depth = this.placeEl.parents(opt.listNodeName).length;
776
+ if(depth + this.dragDepth <= opt.maxDepth) {
777
+ // create new sub-level if one doesn't exist
778
+ if(!list.length) {
779
+ list = $('<' + opt.listNodeName + '/>').addClass(opt.listClass);
780
+ list.append(this.placeEl);
781
+ prev.append(list);
782
+ this.setParent(prev);
783
+ }
784
+ else {
785
+ // else append to next level up
786
+ list = prev.children(opt.listNodeName).last();
787
+ list.append(this.placeEl);
788
+ }
789
+ }
790
+ }
791
+ // decrease horizontal level
792
+ if(mouse.distX < 0) {
793
+ // we can't decrease a level if an item preceeds the current one
794
+ next = this.placeEl.next(opt.itemNodeName);
795
+ if(!next.length) {
796
+ parent = this.placeEl.parent();
797
+ this.placeEl.closest(opt.itemNodeName).after(this.placeEl);
798
+ if(!parent.children().length) {
799
+ this.unsetParent(parent.parent());
800
+ }
801
+ }
802
+ }
803
+ }
804
+
805
+ var isEmpty = false;
806
+
807
+ // find list item under cursor
808
+ if(!hasPointerEvents) {
809
+ this.dragEl[0].style.visibility = 'hidden';
810
+ }
811
+ this.pointEl = $(document.elementFromPoint(e.pageX - document.body.scrollLeft, e.pageY - (window.pageYOffset || document.documentElement.scrollTop)));
812
+ if(!hasPointerEvents) {
813
+ this.dragEl[0].style.visibility = 'visible';
814
+ }
815
+ if(this.pointEl.hasClass(opt.handleClass)) {
816
+ this.pointEl = this.pointEl.closest(opt.itemNodeName);
817
+ }
818
+ if(this.pointEl.hasClass(opt.emptyClass)) {
819
+ isEmpty = true;
820
+ }
821
+ else if(!this.pointEl.length || !this.pointEl.hasClass(opt.itemClass)) {
822
+ return;
823
+ }
824
+
825
+ // find parent list of item under cursor
826
+ var pointElRoot = this.pointEl.closest('.' + opt.rootClass),
827
+ isNewRoot = this.dragRootEl.data('nestable-id') !== pointElRoot.data('nestable-id');
828
+
829
+ /**
830
+ * move vertical
831
+ */
832
+ if(!mouse.dirAx || isNewRoot || isEmpty) {
833
+ // check if groups match if dragging over new root
834
+ if(isNewRoot && opt.group !== pointElRoot.data('nestable-group')) {
835
+ return;
836
+ }
837
+
838
+ // fixed item's depth, use for some list has specific type, eg:'Volume, Section, Chapter ...'
839
+ if(this.options.fixedDepth && this.dragDepth + 1 !== this.pointEl.parents(opt.listNodeName).length) {
840
+ return;
841
+ }
842
+
843
+ // check depth limit
844
+ depth = this.dragDepth - 1 + this.pointEl.parents(opt.listNodeName).length;
845
+ if(depth > opt.maxDepth) {
846
+ return;
847
+ }
848
+ var before = e.pageY < (this.pointEl.offset().top + this.pointEl.height() / 2);
849
+ parent = this.placeEl.parent();
850
+ // if empty create new list to replace empty placeholder
851
+ if(isEmpty) {
852
+ list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
853
+ list.append(this.placeEl);
854
+ this.pointEl.replaceWith(list);
855
+ }
856
+ else if(before) {
857
+ this.pointEl.before(this.placeEl);
858
+ }
859
+ else {
860
+ this.pointEl.after(this.placeEl);
861
+ }
862
+ if(!parent.children().length) {
863
+ this.unsetParent(parent.parent());
864
+ }
865
+ if(!this.dragRootEl.find(opt.itemNodeName).length) {
866
+ this.dragRootEl.append('<div class="' + opt.emptyClass + '"/>');
867
+ }
868
+ // parent root list has changed
869
+ this.dragRootEl = pointElRoot;
870
+ if(isNewRoot) {
871
+ this.hasNewRoot = this.el[0] !== this.dragRootEl[0];
872
+ }
873
+ }
874
+ }
875
+
876
+ };
877
+
878
+ $.fn.nestable = function(params, val) {
879
+ var lists = this,
880
+ retval = this;
881
+
882
+ if(!('Nestable' in window)) {
883
+ window.Nestable = {};
884
+ Nestable.counter = 0;
885
+ }
886
+
887
+ lists.each(function() {
888
+ var plugin = $(this).data("nestable");
889
+
890
+ if(!plugin) {
891
+ Nestable.counter++;
892
+ $(this).data("nestable", new Plugin(this, params));
893
+ $(this).data("nestable-id", Nestable.counter);
894
+
895
+ }
896
+ else {
897
+ if(typeof params === 'string' && typeof plugin[params] === 'function') {
898
+ if (typeof val !== 'undefined') {
899
+ retval = plugin[params](val);
900
+ }else{
901
+ retval = plugin[params]();
902
+ }
903
+ }
904
+ }
905
+ });
906
+
907
+ return retval || lists;
908
+ };
909
+
910
+ })(window.jQuery || window.Zepto, window, document);