fix closure linter warnings

This commit is contained in:
Pavel Reznikov 2015-06-24 18:48:07 -07:00
commit 14f319e9dc
3 changed files with 257 additions and 221 deletions

View file

@ -3,52 +3,53 @@
// (c) 2014-2015 Pavel Reznikov
// gridstack.js may be freely distributed under the MIT license.
(function (factory) {
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery', 'lodash', 'jquery-ui/core', 'jquery-ui/widget', 'jquery-ui/mouse', 'jquery-ui/draggable', 'jquery-ui/resizable'], factory);
define(['jquery', 'lodash', 'jquery-ui/core', 'jquery-ui/widget', 'jquery-ui/mouse', 'jquery-ui/draggable',
'jquery-ui/resizable'], factory);
}
else {
factory(jQuery, _);
}
})(function ($, _) {
})(function($, _) {
var scope = window;
var Utils = {
is_intercepted: function (a, b) {
is_intercepted: function(a, b) {
return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y);
},
sort: function (nodes, dir, width) {
width = width || _.chain(nodes).map(function (node) { return node.x + node.width; }).max().value();
sort: function(nodes, dir, width) {
width = width || _.chain(nodes).map(function(node) { return node.x + node.width; }).max().value();
dir = dir != -1 ? 1 : -1;
return _.sortBy(nodes, function (n) { return dir * (n.x + n.y * width); });
return _.sortBy(nodes, function(n) { return dir * (n.x + n.y * width); });
},
create_stylesheet: function (id) {
var style = document.createElement("style");
style.setAttribute("type", "text/css");
style.setAttribute("data-gs-id", id);
create_stylesheet: function(id) {
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.setAttribute('data-gs-id', id);
if (style.styleSheet) {
style.styleSheet.cssText = "";
style.styleSheet.cssText = '';
}
else {
style.appendChild(document.createTextNode(""));
style.appendChild(document.createTextNode(''));
}
document.getElementsByTagName('head')[0].appendChild(style);
return style.sheet;
},
insert_css_rule: function (sheet, selector, rules, index) {
if(typeof sheet.insertRule === 'function') {
sheet.insertRule(selector + "{" + rules + "}", index);
insert_css_rule: function(sheet, selector, rules, index) {
if (typeof sheet.insertRule === 'function') {
sheet.insertRule(selector + '{' + rules + '}', index);
}
else if(typeof sheet.addRule === 'function') {
else if (typeof sheet.addRule === 'function') {
sheet.addRule(selector, rules, index);
}
},
toBool: function (v) {
toBool: function(v) {
if (typeof v == 'boolean')
return v;
if (typeof v == 'string') {
@ -61,24 +62,24 @@
var id_seq = 0;
var GridStackEngine = function (width, onchange, float, height, items) {
var GridStackEngine = function(width, onchange, float, height, items) {
this.width = width;
this.float = float || false;
this.height = height || 0;
this.nodes = items || [];
this.onchange = onchange || function () {};
this.onchange = onchange || function() {};
this._update_counter = 0;
this._float = this.float;
};
GridStackEngine.prototype.batch_update = function () {
GridStackEngine.prototype.batch_update = function() {
this._update_counter = 1;
this.float = true;
};
GridStackEngine.prototype.commit = function () {
GridStackEngine.prototype.commit = function() {
this._update_counter = 0;
if (this._update_counter == 0) {
this.float = this._float;
@ -87,16 +88,16 @@
}
};
GridStackEngine.prototype._fix_collisions = function (node) {
GridStackEngine.prototype._fix_collisions = function(node) {
this._sort_nodes(-1);
var nn = node, has_locked = Boolean(_.find(this.nodes, function (n) { return n.locked }));
var nn = node, has_locked = Boolean(_.find(this.nodes, function(n) { return n.locked }));
if (!this.float && !has_locked) {
nn = {x: 0, y: node.y, width: this.width, height: node.height};
}
while (true) {
var collision_node = _.find(this.nodes, function (n) {
var collision_node = _.find(this.nodes, function(n) {
return n != node && Utils.is_intercepted(n, nn);
}, this);
if (typeof collision_node == 'undefined') {
@ -107,31 +108,32 @@
}
};
GridStackEngine.prototype.is_area_empty = function (x, y, width, height) {
GridStackEngine.prototype.is_area_empty = function(x, y, width, height) {
var nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1};
var collision_node = _.find(this.nodes, function (n) {
var collision_node = _.find(this.nodes, function(n) {
return Utils.is_intercepted(n, nn);
}, this);
return collision_node == null;
};
GridStackEngine.prototype._sort_nodes = function (dir) {
GridStackEngine.prototype._sort_nodes = function(dir) {
this.nodes = Utils.sort(this.nodes, dir, this.width);
};
GridStackEngine.prototype._pack_nodes = function () {
GridStackEngine.prototype._pack_nodes = function() {
this._sort_nodes();
if (this.float) {
_.each(this.nodes, function (n, i) {
_.each(this.nodes, function(n, i) {
if (n._updating || typeof n._orig_y == 'undefined' || n.y == n._orig_y)
return;
var new_y = n.y;
while (new_y >= n._orig_y) {
var collision_node = _.chain(this.nodes)
.find(function (bn) {
return n != bn && Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
.find(function(bn) {
return n != bn &&
Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
})
.value();
@ -144,7 +146,7 @@
}, this);
}
else {
_.each(this.nodes, function (n, i) {
_.each(this.nodes, function(n, i) {
if (n.locked)
return;
while (n.y > 0) {
@ -154,7 +156,7 @@
if (i > 0) {
var collision_node = _.chain(this.nodes)
.take(i)
.find(function (bn) {
.find(function(bn) {
return Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
})
.value();
@ -171,7 +173,7 @@
}
};
GridStackEngine.prototype._prepare_node = function (node, resizing) {
GridStackEngine.prototype._prepare_node = function(node, resizing) {
node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0 });
node.x = parseInt('' + node.x);
@ -213,7 +215,7 @@
return node;
};
GridStackEngine.prototype._notify = function () {
GridStackEngine.prototype._notify = function() {
if (this._update_counter) {
return;
}
@ -222,12 +224,12 @@
this.onchange(deleted_nodes);
};
GridStackEngine.prototype.clean_nodes = function () {
_.each(this.nodes, function (n) {n._dirty = false });
GridStackEngine.prototype.clean_nodes = function() {
_.each(this.nodes, function(n) {n._dirty = false });
};
GridStackEngine.prototype.get_dirty_nodes = function () {
return _.filter(this.nodes, function (n) { return n._dirty; });
GridStackEngine.prototype.get_dirty_nodes = function() {
return _.filter(this.nodes, function(n) { return n._dirty; });
};
GridStackEngine.prototype.add_node = function(node) {
@ -244,12 +246,12 @@
if (node.auto_position) {
this._sort_nodes();
for (var i = 0; ; ++i) {
for (var i = 0;; ++i) {
var x = i % this.width, y = Math.floor(i / this.width);
if (x + node.width > this.width) {
continue;
}
if (!_.find(this.nodes, function (n) {
if (!_.find(this.nodes, function(n) {
return Utils.is_intercepted({x: x, y: y, width: node.width, height: node.height}, n);
})) {
node.x = x;
@ -267,15 +269,15 @@
return node;
};
GridStackEngine.prototype.remove_node = function (node) {
GridStackEngine.prototype.remove_node = function(node) {
node._id = null;
this.nodes = _.without(this.nodes, node);
this._pack_nodes();
this._notify(node);
};
GridStackEngine.prototype.can_move_node = function (node, x, y, width, height) {
var has_locked = Boolean(_.find(this.nodes, function (n) { return n.locked }));
GridStackEngine.prototype.can_move_node = function(node, x, y, width, height) {
var has_locked = Boolean(_.find(this.nodes, function(n) { return n.locked }));
if (!this.height && !has_locked)
return true;
@ -286,21 +288,29 @@
null,
this.float,
0,
_.map(this.nodes, function (n) { if (n == node) { cloned_node = $.extend({}, n); return cloned_node; } return $.extend({}, n) }));
_.map(this.nodes, function(n) {
if (n == node) {
cloned_node = $.extend({}, n);
return cloned_node;
}
return $.extend({}, n);
}));
clone.move_node(cloned_node, x, y, width, height);
var res = true;
if (has_locked)
res &= !Boolean(_.find(clone.nodes, function (n) { return n != cloned_node && Boolean(n.locked) && Boolean(n._dirty); }));
res &= !Boolean(_.find(clone.nodes, function(n) {
return n != cloned_node && Boolean(n.locked) && Boolean(n._dirty);
}));
if (this.height)
res &= clone.get_grid_height() <= this.height;
return res;
};
GridStackEngine.prototype.can_be_placed_with_respect_to_height = function (node) {
GridStackEngine.prototype.can_be_placed_with_respect_to_height = function(node) {
if (!this.height)
return true;
@ -309,12 +319,12 @@
null,
this.float,
0,
_.map(this.nodes, function (n) { return $.extend({}, n) }));
_.map(this.nodes, function(n) { return $.extend({}, n) }));
clone.add_node(node);
return clone.get_grid_height() <= this.height;
};
GridStackEngine.prototype.move_node = function (node, x, y, width, height, no_pack) {
GridStackEngine.prototype.move_node = function(node, x, y, width, height, no_pack) {
if (typeof x != 'number') x = node.x;
if (typeof y != 'number') y = node.y;
if (typeof width != 'number') width = node.width;
@ -347,28 +357,28 @@
return node;
};
GridStackEngine.prototype.get_grid_height = function () {
return _.reduce(this.nodes, function (memo, n) { return Math.max(memo, n.y + n.height); }, 0);
GridStackEngine.prototype.get_grid_height = function() {
return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0);
};
GridStackEngine.prototype.begin_update = function (node) {
_.each(this.nodes, function (n) {
GridStackEngine.prototype.begin_update = function(node) {
_.each(this.nodes, function(n) {
n._orig_y = n.y;
});
node._updating = true;
};
GridStackEngine.prototype.end_update = function () {
_.each(this.nodes, function (n) {
GridStackEngine.prototype.end_update = function() {
_.each(this.nodes, function(n) {
n._orig_y = n.y;
});
var n = _.find(this.nodes, function (n) { return n._updating; });
var n = _.find(this.nodes, function(n) { return n._updating; });
if (n) {
n._updating = false;
}
};
var GridStack = function (el, opts) {
var GridStack = function(el, opts) {
var self = this, one_column_mode;
this.container = $(el);
@ -409,9 +419,9 @@
this._init_styles();
this.grid = new GridStackEngine(this.opts.width, function (nodes) {
this.grid = new GridStackEngine(this.opts.width, function(nodes) {
var max_height = 0;
_.each(nodes, function (n) {
_.each(nodes, function(n) {
if (n._id == null) {
n.el.remove();
}
@ -430,24 +440,29 @@
if (this.opts.auto) {
var elements = [];
var _this = this;
this.container.children('.' + this.opts.item_class).each(function (index, el) {
this.container.children('.' + this.opts.item_class).each(function(index, el) {
el = $(el);
elements.push({
el: el,
i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width // Use opts.width as weight for Y
i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width
});
});
_.chain(elements).sortBy(function (x) { return x.i; }).each(function (i) {
_.chain(elements).sortBy(function(x) { return x.i; }).each(function(i) {
self._prepare_element(i.el);
}).value();
}
this.set_animation(this.opts.animate);
this.placeholder = $('<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '"><div class="placeholder-content" /></div>').hide();
this.container.height((this.grid.get_grid_height()) * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
this.placeholder = $(
'<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '">' +
'<div class="placeholder-content" /></div>').hide();
var on_resize_handler = function () {
this.container.height(
this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) -
this.opts.vertical_margin);
var on_resize_handler = function() {
if (self._is_one_column_mode()) {
if (one_column_mode)
return;
@ -455,7 +470,7 @@
one_column_mode = true;
self.grid._sort_nodes();
_.each(self.grid.nodes, function (node) {
_.each(self.grid.nodes, function(node) {
self.container.append(node.el);
if (!node.no_move) {
@ -472,7 +487,7 @@
one_column_mode = false;
_.each(self.grid.nodes, function (node) {
_.each(self.grid.nodes, function(node) {
if (!node.no_move) {
node.el.draggable('enable');
}
@ -487,7 +502,7 @@
on_resize_handler();
};
GridStack.prototype._init_styles = function () {
GridStack.prototype._init_styles = function() {
if (this._styles_id) {
$('[data-gs-id="' + this._styles_id + '"]').remove();
}
@ -497,7 +512,7 @@
this._styles._max = 0;
};
GridStack.prototype._update_styles = function (max_height) {
GridStack.prototype._update_styles = function(max_height) {
if (this._styles == null) {
return;
}
@ -541,18 +556,21 @@
}
};
GridStack.prototype._update_container_height = function () {
GridStack.prototype._update_container_height = function() {
if (this.grid._update_counter) {
return;
}
this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
this.container.height(
this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) -
this.opts.vertical_margin);
};
GridStack.prototype._is_one_column_mode = function () {
return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) <= this.opts.min_width;
GridStack.prototype._is_one_column_mode = function() {
return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) <=
this.opts.min_width;
};
GridStack.prototype._prepare_element = function (el) {
GridStack.prototype._prepare_element = function(el) {
var self = this;
el = $(el);
@ -577,7 +595,7 @@
var cell_width, cell_height;
var on_start_moving = function (event, ui) {
var on_start_moving = function(event, ui) {
self.container.append(self.placeholder);
var o = $(this);
self.grid.clean_nodes();
@ -596,7 +614,7 @@
el.resizable('option', 'minHeight', self.opts.cell_height * (node.min_height || 1));
};
var on_end_moving = function (event, ui) {
var on_end_moving = function(event, ui) {
self.placeholder.detach();
var o = $(this);
node.el = o;
@ -618,9 +636,9 @@
el.draggable(_.extend(this.opts.draggable, {
start: on_start_moving,
stop: on_end_moving,
drag: function (event, ui) {
drag: function(event, ui) {
var x = Math.round(ui.position.left / cell_width),
y = Math.floor((ui.position.top + cell_height/2) / cell_height);
y = Math.floor((ui.position.top + cell_height / 2) / cell_height);
if (!self.grid.can_move_node(node, x, y, node.width, node.height)) {
return;
}
@ -631,9 +649,9 @@
})).resizable(_.extend(this.opts.resizable, {
start: on_start_moving,
stop: on_end_moving,
resize: function (event, ui) {
resize: function(event, ui) {
var x = Math.round(ui.position.left / cell_width),
y = Math.floor((ui.position.top + cell_height/2) / cell_height),
y = Math.floor((ui.position.top + cell_height / 2) / cell_height),
width = Math.round(ui.size.width / cell_width),
height = Math.round(ui.size.height / cell_height);
if (!self.grid.can_move_node(node, x, y, width, height)) {
@ -655,7 +673,7 @@
el.attr('data-gs-locked', node.locked ? 'yes' : null);
};
GridStack.prototype.set_animation = function (enable) {
GridStack.prototype.set_animation = function(enable) {
if (enable) {
this.container.addClass('grid-stack-animate');
}
@ -664,7 +682,7 @@
}
};
GridStack.prototype.add_widget = function (el, x, y, width, height, auto_position) {
GridStack.prototype.add_widget = function(el, x, y, width, height, auto_position) {
el = $(el);
if (typeof x != 'undefined') el.attr('data-gs-x', x);
if (typeof y != 'undefined') el.attr('data-gs-y', y);
@ -678,12 +696,12 @@
return el;
};
GridStack.prototype.will_it_fit = function (x, y, width, height, auto_position) {
GridStack.prototype.will_it_fit = function(x, y, width, height, auto_position) {
var node = {x: x, y: y, width: width, height: height, auto_position: auto_position};
return this.grid.can_be_placed_with_respect_to_height(node);
};
GridStack.prototype.remove_widget = function (el, detach_node) {
GridStack.prototype.remove_widget = function(el, detach_node) {
detach_node = typeof detach_node === 'undefined' ? true : detach_node;
el = $(el);
var node = el.data('_gridstack_node');
@ -694,17 +712,17 @@
el.remove();
};
GridStack.prototype.remove_all = function (detach_node) {
_.each(this.grid.nodes, function (node) {
GridStack.prototype.remove_all = function(detach_node) {
_.each(this.grid.nodes, function(node) {
this.remove_widget(node.el, detach_node);
}, this);
this.grid.nodes = [];
this._update_container_height();
};
GridStack.prototype.resizable = function (el, val) {
GridStack.prototype.resizable = function(el, val) {
el = $(el);
el.each(function (index, el) {
el.each(function(index, el) {
el = $(el);
var node = el.data('_gridstack_node');
if (typeof node == 'undefined' || node == null) {
@ -722,9 +740,9 @@
return this;
};
GridStack.prototype.movable = function (el, val) {
GridStack.prototype.movable = function(el, val) {
el = $(el);
el.each(function (index, el) {
el.each(function(index, el) {
el = $(el);
var node = el.data('_gridstack_node');
if (typeof node == 'undefined' || node == null) {
@ -742,19 +760,19 @@
return this;
};
GridStack.prototype.disable = function () {
GridStack.prototype.disable = function() {
this.movable(this.container.children('.' + this.opts.item_class), false);
this.resizable(this.container.children('.' + this.opts.item_class), false);
};
GridStack.prototype.enable = function () {
GridStack.prototype.enable = function() {
this.movable(this.container.children('.' + this.opts.item_class), true);
this.resizable(this.container.children('.' + this.opts.item_class), true);
};
GridStack.prototype.locked = function (el, val) {
GridStack.prototype.locked = function(el, val) {
el = $(el);
el.each(function (index, el) {
el.each(function(index, el) {
el = $(el);
var node = el.data('_gridstack_node');
if (typeof node == 'undefined' || node == null) {
@ -767,7 +785,7 @@
return this;
};
GridStack.prototype._update_element = function (el, callback) {
GridStack.prototype._update_element = function(el, callback) {
el = $(el).first();
var node = el.data('_gridstack_node');
if (typeof node == 'undefined' || node == null) {
@ -789,8 +807,8 @@
self.grid.end_update();
};
GridStack.prototype.resize = function (el, width, height) {
this._update_element(el, function (el, node) {
GridStack.prototype.resize = function(el, width, height) {
this._update_element(el, function(el, node) {
width = (width != null && typeof width != 'undefined') ? width : node.width;
height = (height != null && typeof height != 'undefined') ? height : node.height;
@ -798,8 +816,8 @@
});
};
GridStack.prototype.move = function (el, x, y) {
this._update_element(el, function (el, node) {
GridStack.prototype.move = function(el, x, y) {
this._update_element(el, function(el, node) {
x = (x != null && typeof x != 'undefined') ? x : node.x;
y = (y != null && typeof y != 'undefined') ? y : node.y;
@ -807,8 +825,8 @@
});
};
GridStack.prototype.update = function (el, x, y, width, height) {
this._update_element(el, function (el, node) {
GridStack.prototype.update = function(el, x, y, width, height) {
this._update_element(el, function(el, node) {
x = (x != null && typeof x != 'undefined') ? x : node.x;
y = (y != null && typeof y != 'undefined') ? y : node.y;
width = (width != null && typeof width != 'undefined') ? width : node.width;
@ -818,7 +836,7 @@
});
};
GridStack.prototype.cell_height = function (val) {
GridStack.prototype.cell_height = function(val) {
if (typeof val == 'undefined') {
return this.opts.cell_height;
}
@ -829,7 +847,7 @@
this._update_styles();
};
GridStack.prototype.cell_width = function () {
GridStack.prototype.cell_width = function() {
var o = this.container.children('.' + this.opts.item_class).first();
return Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
};
@ -845,16 +863,16 @@
return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)};
};
GridStack.prototype.batch_update = function () {
GridStack.prototype.batch_update = function() {
this.grid.batch_update();
};
GridStack.prototype.commit = function () {
GridStack.prototype.commit = function() {
this.grid.commit();
this._update_container_height()
this._update_container_height();
};
GridStack.prototype.is_area_empty = function (x, y, width, height) {
GridStack.prototype.is_area_empty = function(x, y, width, height) {
return this.grid.is_area_empty(x, y, width, height);
};
@ -862,8 +880,8 @@
scope.GridStackUI.Utils = Utils;
$.fn.gridstack = function (opts) {
return this.each(function () {
$.fn.gridstack = function(opts) {
return this.each(function() {
if (!$(this).data('gridstack')) {
$(this).data('gridstack', new GridStack(this, opts));
}