Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Directly reference jQuery and not $ #267

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

* ImageMagick or GraphicsMagick (for MiniMagick);
* [Bootstrap 3](http://getbootstrap.com/) fully installed in your app.
* jQuery
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Bootstrap 3 fully installed in your app" already implies that jQuery is required.



## Installation
Expand Down
164 changes: 85 additions & 79 deletions app/assets/javascripts/bootsy/area.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,96 @@
/* global Bootsy */

window.Bootsy = window.Bootsy || {};

Bootsy.Area = function($el) {
var self = this;

this.$el = $el;
this.unsavedChanges = false;
this.locale = $el.data('bootsy-locale') || $('html').attr('lang');
if (!$.fn.wysihtml5.locale.hasOwnProperty(this.locale)) this.locale = 'en';

this.options = {
locale: this.locale,
alertUnsavedChanges: $el.data('bootsy-alert-unsaved'),
uploader: $el.data('bootsy-uploader'),
color: $el.data('bootsy-color'),
emphasis: $el.data('bootsy-emphasis'),
'font-styles': $el.data('bootsy-font-styles'),
html: $el.data('bootsy-html'),
image: $el.data('bootsy-image'),
link: $el.data('bootsy-link'),
lists: $el.data('bootsy-lists'),
events: {
change: function() {
self.unsavedChanges = true;
$el.trigger('change');
(function($) {
/* global Bootsy */

window.Bootsy = window.Bootsy || {};

Bootsy.Area = function($el) {
var self = this;

this.$el = $el;
this.unsavedChanges = false;
this.locale = $el.data("bootsy-locale") || $("html").attr("lang");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why single quotes?

if (!$.fn.wysihtml5.locale.hasOwnProperty(this.locale)) this.locale = "en";

this.options = {
locale: this.locale,
alertUnsavedChanges: $el.data("bootsy-alert-unsaved"),
uploader: $el.data("bootsy-uploader"),
color: $el.data("bootsy-color"),
emphasis: $el.data("bootsy-emphasis"),
"font-styles": $el.data("bootsy-font-styles"),
html: $el.data("bootsy-html"),
image: $el.data("bootsy-image"),
link: $el.data("bootsy-link"),
lists: $el.data("bootsy-lists"),
events: {
change: function() {
self.unsavedChanges = true;
$el.trigger("change");
}
}
}
};
};
};

// Alert for unsaved changes
Bootsy.Area.prototype.unsavedChangesAlert = function () {
if (this.unsavedChanges) {
return $.fn.wysihtml5.locale[this.locale].bootsy.alertUnsaved;
}
};

// Clear everything
Bootsy.Area.prototype.clear = function () {
this.editor.clear();
this.setImageGalleryId('');
this.modal.$el.data('gallery-loaded', false);
};

Bootsy.Area.prototype.setImageGalleryId = function(id) {
this.$el.data('gallery-id', id);
this.$el.siblings('.bootsy_image_gallery_id').val(id);
};

// Init components
Bootsy.Area.prototype.init = function() {
if (!this.$el.data('bootsy-initialized')) {
if ((this.options.image === true) && (this.options.uploader === true)) {
this.modal = new Bootsy.Modal(this);
this.options.image = false;
this.options.customCommand = true;
this.options.customCommandCallback = this.modal.show.bind(this.modal);
this.options.customTemplates = { customCommand: Bootsy.imageTemplate };

// Alert for unsaved changes
Bootsy.Area.prototype.unsavedChangesAlert = function() {
if (this.unsavedChanges) {
return $.fn.wysihtml5.locale[this.locale].bootsy.alertUnsaved;
}
};

this.editor = this.$el.wysihtml5($.extend(true, {}, Bootsy.options, this.options)).data('wysihtml5').editor;
// Clear everything
Bootsy.Area.prototype.clear = function() {
this.editor.clear();
this.setImageGalleryId("");
this.modal.$el.data("gallery-loaded", false);
};

// Mechanism for unsaved changes alert
if (this.options.alertUnsavedChanges !== false) {
window.onbeforeunload = this.unsavedChangesAlert.bind(this);
}
Bootsy.Area.prototype.setImageGalleryId = function(id) {
this.$el.data("gallery-id", id);
this.$el.siblings(".bootsy_image_gallery_id").val(id);
};

this.$el.closest('form').submit(function() {
this.unsavedChanges = false;
// Init components
Bootsy.Area.prototype.init = function() {
if (!this.$el.data("bootsy-initialized")) {
if (this.options.image === true && this.options.uploader === true) {
this.modal = new Bootsy.Modal(this);
this.options.image = false;
this.options.customCommand = true;
this.options.customCommandCallback = this.modal.show.bind(this.modal);
this.options.customTemplates = { customCommand: Bootsy.imageTemplate };
}

return true;
}.bind(this));
this.editor = this.$el
.wysihtml5($.extend(true, {}, Bootsy.options, this.options))
.data("wysihtml5").editor;

this.$el.data('bootsy-initialized', true);
}
};
// Mechanism for unsaved changes alert
if (this.options.alertUnsavedChanges !== false) {
window.onbeforeunload = this.unsavedChangesAlert.bind(this);
}

// Insert image in the text
Bootsy.Area.prototype.insertImage = function(image) {
this.editor.currentView.element.focus();
this.$el.closest("form").submit(
function() {
this.unsavedChanges = false;

if (this.caretBookmark) {
this.editor.composer.selection.setBookmark(this.caretBookmark);
this.caretBookmark = null;
}
return true;
}.bind(this)
);

this.editor.composer.commands.exec('insertImage', image);
};
this.$el.data("bootsy-initialized", true);
}
};

// Insert image in the text
Bootsy.Area.prototype.insertImage = function(image) {
this.editor.currentView.element.focus();

if (this.caretBookmark) {
this.editor.composer.selection.setBookmark(this.caretBookmark);
this.caretBookmark = null;
}

this.editor.composer.commands.exec("insertImage", image);
};
})(jQuery);
80 changes: 41 additions & 39 deletions app/assets/javascripts/bootsy/editor_options.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
window.Bootsy = window.Bootsy || {};
(function($) {
window.Bootsy = window.Bootsy || {};

var pageStylesheets = [];
$('link[rel="stylesheet"]').each(function () {
pageStylesheets.push($(this).attr('href'));
});
var pageStylesheets = [];
$('link[rel="stylesheet"]').each(function() {
pageStylesheets.push($(this).attr("href"));
});

window.Bootsy.options = {};
window.Bootsy.options = {};

$.extend(true, window.Bootsy.options, $.fn.wysihtml5.defaultOptions, {
parserRules: {
classes: {
"wysiwyg-float-left": 1,
"wysiwyg-float-right": 1,
"wysiwyg-float-inline": 1
},
tags: {
"cite": {
"check_attributes": {
"title": "alt"
}
$.extend(true, window.Bootsy.options, $.fn.wysihtml5.defaultOptions, {
parserRules: {
classes: {
"wysiwyg-float-left": 1,
"wysiwyg-float-right": 1,
"wysiwyg-float-inline": 1
},
"img": {
"check_attributes": {
"src": "src"
tags: {
cite: {
check_attributes: {
title: "alt"
}
},
"add_class": {
"align": "align_img"
}
},
// this allows youtube embed codes
"iframe": {
set_attributes: {
"frameborder": "0",
"allowfullscreen": "1"
img: {
check_attributes: {
src: "src"
},
add_class: {
align: "align_img"
}
},
check_attributes: {
"width": "numbers",
"height": "numbers",
"src": "href"
// this allows youtube embed codes
iframe: {
set_attributes: {
frameborder: "0",
allowfullscreen: "1"
},
check_attributes: {
width: "numbers",
height: "numbers",
src: "href"
}
}
}
}
},
color: true,
stylesheets: pageStylesheets
});
},
color: true,
stylesheets: pageStylesheets
});
})(jQuery);
58 changes: 30 additions & 28 deletions app/assets/javascripts/bootsy/init.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
/* global Bootsy */
window.Bootsy = window.Bootsy || {};
(function($) {
/* global Bootsy */
window.Bootsy = window.Bootsy || {};

// Public: Intialize Bootsy editors in all visible `textarea`
// elements that has the `bootsy_text_area` class.
Bootsy.init = function() {
if (!Bootsy.areas) {
Bootsy.areas = {};
}
// Public: Intialize Bootsy editors in all visible `textarea`
// elements that has the `bootsy_text_area` class.
Bootsy.init = function() {
if (!Bootsy.areas) {
Bootsy.areas = {};
}

$('textarea.bootsy_text_area').each(function(index) {
if (!$(this).data('bootsy-initialized')) {
var area = new Bootsy.Area($(this));
var areaIdx = $(this).attr('id') || index;
$("textarea.bootsy_text_area").each(function(index) {
if (!$(this).data("bootsy-initialized")) {
var area = new Bootsy.Area($(this));
var areaIdx = $(this).attr("id") || index;

/* There's always people who let elements share ids */
if(Bootsy.areas[areaIdx] !== undefined) {
areaIdx = $(this).attr('id') + index;
}
/* There's always people who let elements share ids */
if (Bootsy.areas[areaIdx] !== undefined) {
areaIdx = $(this).attr("id") + index;
}

area.init();
area.init();

Bootsy.areas[areaIdx] = area;
}
});
};
Bootsy.areas[areaIdx] = area;
}
});
};

/* Initialize Bootsy on document load */
$(function() {
$(window).on('load', function() {
Bootsy.init();
/* Initialize Bootsy on document load */
$(function() {
$(window).on("load", function() {
Bootsy.init();

/* Reload Bootsy on page load when using Turbolinks. */
document.addEventListener('turbolinks:load', Bootsy.init);
/* Reload Bootsy on page load when using Turbolinks. */
document.addEventListener("turbolinks:load", Bootsy.init);
});
});
});
})(jQuery);
Loading