Skip to content

Commit

Permalink
init callbacks, update build dependencies, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
skrsm committed Feb 17, 2022
1 parent f0ef96c commit 583a0c1
Show file tree
Hide file tree
Showing 11 changed files with 1,168 additions and 706 deletions.
61 changes: 53 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://www.gnu.org/licenses/gpl-3.0.html">
<img alt="License" src="https://img.shields.io/badge/License-GPL-blue.svg">
</a>
<a href="https://snyk.io/test/npm/cuttr/1.3.2">
<img src="https://snyk.io/test/npm/cuttr/1.3.2/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/npm/cuttr/1.3.1" style="max-width:100%;">
<a href="https://snyk.io/test/npm/cuttr/1.4.0">
<img src="https://snyk.io/test/npm/cuttr/1.4.0/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/npm/cuttr/1.3.1" style="max-width:100%;">
</a>
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=K9X3RW27WJHWE&source=url">
<img alt="License" src="https://img.shields.io/badge/donate-PayPal.me-ff69b4.svg">
Expand Down Expand Up @@ -54,9 +54,9 @@ Link directly to Cuttr files on [unpkg](https://unpkg.com/cuttr).
Link directly to Cuttr files on [cdnjs](https://cdnjs.com/libraries/cuttr).

``` html
<script src="https://cdnjs.cloudflare.com/ajax/libs/cuttr/1.3.2/cuttr.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cuttr/1.4.0/cuttr.min.js"></script>
<!-- or -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cuttr/1.3.2/cuttr.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cuttr/1.4.0/cuttr.js"></script>

```

Expand Down Expand Up @@ -94,7 +94,9 @@ If you want to use Cuttr to develop commercial sites, themes, projects, and appl

### Open source license

If you are creating an open source application under a license compatible with the [GNU GPL license v3](https://www.gnu.org/licenses/gpl-3.0.html), you may use Cuttr under the terms of the GPLv3.
If you are creating an open source application under a license compatible with the [GNU GPL license v3](https://www.gnu.org/licenses/gpl-3.0.html), you may use Cuttr under the terms of the GPLv3.

The credit comments in the JavaScript files should be kept intact (even after combination or minification).

[Read more about Cuttr's licenses](https://cuttr.kulahs.de/pricing.html).

Expand Down Expand Up @@ -144,8 +146,10 @@ let truncateElement = new Cuttr( '.container', {
// DEFAULTS LISTED

licenseKey: 'YOUR_KEY_HERE',
// use the license key provided on the purchase of the fullPage Commercial License
// if your project is open source and it is compatible with the GPLv3 license leave this field blank
// this option is compulsory
// use the license key provided on the purchase of the Cuttr Commercial License
// if your project is open source and it is compatible with the GPLv3 license you can request a license key
// please read more about licenses here https://github.com/d-e-v-s-k/cuttr-js#license

truncate: 'characters',
// Truncate method
Expand Down Expand Up @@ -196,7 +200,7 @@ let truncateElement = new Cuttr( '.container', {
```

### Methods
Each plugin instance comes with some public methods to call.
Each plugin instance comes with some public methods to call. See them in action inside the examples folder at methods.html.

Example Initialization:

Expand Down Expand Up @@ -254,6 +258,47 @@ $(document).ready(function() {
});
```

### Callbacks
Each plugin instance provides some callbacks. See them in action inside the examples folder at callbacks.html.

At plugin initialization, a series of callbacks are available:

#### afterTruncate()
Callback fired once the original content has been truncated.

```javascript
let truncateElement = new Cuttr('.element', {
//options here
truncate: 'words',
length: 12,

// callbacks
afterTruncate: function(){
let truncateElement = this;
console.log(this);
alert('"afterTruncate" callback fired!');
}
});
```

#### afterExpand()
Callback fired once the original content has been expanded.

```javascript
let truncateElement = new Cuttr('.element', {
//options here
truncate: 'words',
length: 12,

// callbacks
afterExpand: function(){
let truncateElement = this;
console.log(this);
alert('"afterExpand" callback fired!');
}
});
```

## Demos & Examples

[Checkout our demos & examples page](https://cuttr.kulahs.de/examples.html)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cuttr",
"description": "Cuttr is a javascript plugin that truncates multi-line string content with multiple truncation methods and custom ellipsis.",
"main": "dist/cuttr.js",
"version": "1.3.2",
"version": "1.4.0",
"authors": [
"DEVSK"
],
Expand Down
76 changes: 55 additions & 21 deletions dist/cuttr.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*!
* Cuttr 1.3.1
* Cuttr 1.4.0
* https://github.com/d-e-v-s-k/cuttr-js
*
* @license GPLv3 for open source use only
* or Cuttr Commercial License for commercial use
* https://cuttr.kulahs.de/pricing/
*
* Copyright (C) 2021 https://cuttr.kulahs.de/ - A project by DEVSK
* Copyright (C) 2022 https://cuttr.kulahs.de/ - A project by DEVSK
**/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
Expand Down Expand Up @@ -58,6 +58,9 @@
readMoreBtnSelectorClass: 'cuttr__readmore',
// read-more button selector
readMoreBtnAdditionalClasses: '',
// callback functions
afterTruncate: function afterTruncate() {},
afterExpand: function afterExpand() {},
// private options
dataIndex: 'data-cuttr-index' // cuttr index data attribute

Expand All @@ -70,16 +73,7 @@
Object.keys(options).forEach(function (key) {
self.options[key] = options[key];
});
} //using jQuery initialization? Creating the $.fn.fullpage object

/*window.cuttr_api = self;
if(options.$){
console.log(self);
Object.keys(self).forEach(function (key) {
options.$.fn.Cuttr[key] = self[key];
});
}*/

}

var init = function init() {
prepare.call(this);
Expand All @@ -90,8 +84,14 @@


function prepare() {
// return if no target element defined
if (!self.options.elementsToTruncate) return; // set element type depending on source
var isAuthorized = self.options && new RegExp('([\\d\\w]{8}-){3}[\\d\\w]{8}|^(?=.*?[A-Y])(?=.*?[a-y])(?=.*?[0-8])(?=.*?[#?!@$%^&*-]).{8,}$').test(self.options['li' + 'cen' + 'seK' + 'e' + 'y']) || document.domain.indexOf('cuttr' + '.' + 'kul' + 'ahs' + '.' + 'de') > -1; // return if no target element defined

if (!self.options.elementsToTruncate) {
return;
} else {
displayWarnings(isAuthorized);
} // set element type depending on source


if (!('length' in self.options.elementsToTruncate)) self.options.elementsToTruncate = [self.options.elementsToTruncate]; // loop through target elements to truncate

Expand All @@ -116,7 +116,10 @@
if (self.options.contentTruncationState[i]) {
if (self.options.readMore) addReadMore(currentElement);
currentElement.classList += ' ' + self.options.loadedClass;
}
} // here go the callbacks


self.options.afterTruncate.call(currentElement);
}
}
/*
Expand Down Expand Up @@ -288,7 +291,9 @@
if (btnPosition == 'inside' && self.options.readMore) addReadMore(currentElement, true); // update button text and aria

event.target.innerHTML = readLessText.replace(/<[^>]*>/g, ""); //event.target.setAttribute('aria-expanded', 'true');
// truncate content if its shown completely currently
// here go the callbacks

self.options.afterExpand.call(currentElement); // truncate content if its shown completely currently
} else {
// truncate content
truncatedContent = truncateIt(currentElement, currentContent.trim(), truncateLength, truncateEnding);
Expand All @@ -298,6 +303,20 @@
if (btnPosition == 'inside' && self.options.readMore) addReadMore(currentElement, true); // update button text and aria

event.target.innerHTML = readMoreText.replace(/<[^>]*>/g, ""); //event.target.setAttribute('aria-expanded', 'false');
// here go the callbacks

self.options.afterTruncate.call(currentElement);
}
}
/**
* Displays warnings
*/


function displayWarnings(isAuthorized) {
if (!isAuthorized) {
showError('error', 'Cuttr.js has a GPLv3 license and it requires a `licenseKey` option. Read about it here:');
showError('error', 'https://github.com/d-e-v-s-k/cuttr-js#options');
}
}
/*
Expand Down Expand Up @@ -341,7 +360,10 @@


if (btnExists) btnExists.innerHTML = readLessText.replace(/<[^>]*>/g, "");
}
} // here go the callbacks


self.options.afterExpand.call(currentElement);
}
}
};
Expand Down Expand Up @@ -370,7 +392,7 @@
var truncateLength = currentElement.dataset.cuttrLength ? currentElement.dataset.cuttrLength : self.options.length;
var truncateEnding = currentElement.dataset.cuttrEnding ? currentElement.dataset.cuttrEnding : self.options.ending;
var truncatedContent = void 0;
var btnExists = void 0; // hide content if its currently truncated
var btnExists = void 0; // hide content if its currently fully visible

if (self.options.contentVisibilityState[thisIndex]) {
// truncate content
Expand All @@ -390,7 +412,10 @@


if (btnExists) btnExists.innerHTML = readMoreText.replace(/<[^>]*>/g, "");
}
} // here go the callbacks


self.options.afterTruncate.call(currentElement);
}
}
};
Expand Down Expand Up @@ -441,7 +466,16 @@

currentElement = null;
}
};
}; //utils

/*
shows console message
*/


function showError(type, text) {
window.console && window.console[type] && window.console[type]('Cuttr: ' + text);
}

init();
return self;
Expand All @@ -450,7 +484,7 @@
return Cuttr;
});
/**
* jQuery adapter for Cuttr.js 1.1.0
* jQuery adapter for Cuttr.js 1.4.0
*/


Expand Down
Loading

0 comments on commit 583a0c1

Please sign in to comment.