jQuery.fn.getPath = function () {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
while (node.length) {
var realNode = node[0], name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
var parent = node.parent();
var siblings = parent.children(name);
if (siblings.length > 1) {
name += ':eq(' + siblings.index(realNode) + ')';
}
path = name + (path ? '>' + path : '');
node = parent;
}
return path;
};
var FamiliarityAdmin = function () {
this.familiarityEditorialSet = {};
this.familiaritySaveUrl = '<%=Familiarity::Engine.routes.url_helpers.save_familiarities_path(format: :json)%>';
this.familiaritiesUrl = '<%= Familiarity::Engine.routes.url_helpers.familiarities_path(format: :json) %>';
}
FamiliarityAdmin.familiaritySwitchBoard = function () {
var familiarity_admin = new FamiliarityAdmin();
var switch_state = 'On';
$('div.familiaritySwitch').remove();
var _div = $('
').html('Switch ' + switch_state + ' familiarity!
').click(function () {
familiarity_admin.familiaritySwitch(switch_state);
switch_state = (switch_state == "On") ? "Off" : "On";
$(this).html('Switch ' + switch_state + ' familiarity!
');
}).appendTo($('body'));
}
FamiliarityAdmin.prototype = {
findOrBuildPopup: function (selection_path, inner_text) {
if (typeof(inner_text) == 'undefined') {
inner_text = "";
}
var _div = this.familiarityEditorialSet[selection_path];
if (_div == null) {
var _div = $('');
//Adding 'Close' handler
_div.children('a').click(function () {
$(this).parents('div.familiarityPopUpEdit').hide();
});
//Populating textarea
_div.children('textarea').val(inner_text);
this.familiarityEditorialSet[selection_path] = _div;
_div.appendTo($('body'));
}
$.each(this.familiarityEditorialSet, function (k, v) {
v.hide();
});
return _div;
},
startFamiliarizing: function () {
var _current_object = this;
$('.familiarityTag').bind("mouseenter.myFamiliarity", function (e) {
var _this = $(this);
var currentSelectionPath = _this.getPath();
// console.log(currentSelectionPath);
_this.addClass('familiar-selector');
_this.parents().removeClass('familiar-selector');
e.stopImmediatePropagation();
}).bind("mouseleave.myFamiliarity", function (e) {
var _this = $(this);
var currentSelectionPath = _this.getPath();
_this.removeClass('familiar-selector');
e.stopImmediatePropagation();
}).bind("click.myFamiliarity", function (e) {
var _this = $(this);
var currentSelectionPath = _this.getPath();
var _left_position = _this.position().left;
var _top_position = _this.position().top + _this.height();
var _div = _current_object.findOrBuildPopup(currentSelectionPath).css({
left: _left_position,
top: _top_position
}).show().children('textarea').focus();
// console.log(_this);
e.preventDefault();
e.stopPropagation();
});
},
saveToBackEnd: function () {
var _result_hash = {}
$.each(this.familiarityEditorialSet, function (k, v) {
if (v.children('textarea').val().trim().length > 0) {
_result_hash[k] = v.children('textarea').val();
}
})
jQuery.post(this.familiaritySaveUrl, {
from_page: $(location).attr('pathname'),
'familiarity_hash': _result_hash
});
},
stopFamiliarizing: function () {
$('.familiarityTag').unbind('.myFamiliarity');
},
familiaritySwitch: function (i_switch) {
if (i_switch == "Off") {
this.stopFamiliarizing();
$('.familiarityPopUpEdit').hide();
if (confirm("Do you want to save the collected familiarity data?")) {
this.saveToBackEnd();
}
}
$.each($('body *:not(.familiaritySwitch, .familiarityPopUpEdit)'), function (i, e) {
if (i_switch == "On") {
if ($(e).parents('.familiarityPopUpEdit').length <= 0) {
$(e).addClass('familiarityTag');
}
} else {
$(e).removeClass('familiarityTag');
}
});
if (i_switch == "On") {
var _current_object = this;
this.startFamiliarizing();
$.get(this.familiaritiesUrl, function (data) {
var familiarity_hash = data[$(location).attr('pathname')]
if (familiarity_hash == null) {
familiarity_hash = {}
}
$.each(familiarity_hash, function (k, v) {
_current_object.findOrBuildPopup(k, v);
});
});
}
}
}