mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Extract breaking up a string into a separate file, include it in
embedded view.
This commit is contained in:
parent
db8f4ea58f
commit
e8ee490778
4 changed files with 73 additions and 46 deletions
|
@ -1,8 +1,10 @@
|
|||
/* global BreakString:true */
|
||||
|
||||
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny,
|
||||
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
||||
shortDate, shortDateNoYear, tinyDateYear, breakUp, relativeAgeTinyShowsYear;
|
||||
shortDate, shortDateNoYear, tinyDateYear, relativeAgeTinyShowsYear;
|
||||
|
||||
/*
|
||||
/*
|
||||
* memoize.js
|
||||
* by @philogb and @addyosmani
|
||||
* with further optimizations by @mathias
|
||||
|
@ -44,50 +46,9 @@ var cappedMemoize = function ( fn, max ) {
|
|||
};
|
||||
};
|
||||
|
||||
breakUp = function(str, hint){
|
||||
var rval = [];
|
||||
var prev = str[0];
|
||||
var cur;
|
||||
var brk = "<wbr>​";
|
||||
|
||||
var hintPos = [];
|
||||
if(hint) {
|
||||
hint = hint.toLowerCase().split(/\s+/).reverse();
|
||||
var current = 0;
|
||||
while(hint.length > 0) {
|
||||
var word = hint.pop();
|
||||
if(word !== str.substr(current, word.length).toLowerCase()) {
|
||||
break;
|
||||
}
|
||||
current += word.length;
|
||||
hintPos.push(current);
|
||||
}
|
||||
}
|
||||
|
||||
rval.push(prev);
|
||||
for (var i=1;i<str.length;i++) {
|
||||
cur = str[i];
|
||||
if(prev.match(/[^0-9]/) && cur.match(/[0-9]/)){
|
||||
rval.push(brk);
|
||||
} else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){
|
||||
rval.pop();
|
||||
rval.push(brk);
|
||||
rval.push(prev);
|
||||
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){
|
||||
rval.push(brk);
|
||||
} else if(hintPos.indexOf(i) > -1) {
|
||||
rval.push(brk);
|
||||
}
|
||||
|
||||
rval.push(cur);
|
||||
prev = cur;
|
||||
}
|
||||
|
||||
return rval.join("");
|
||||
|
||||
};
|
||||
|
||||
breakUp = cappedMemoize(breakUp, 100);
|
||||
var breakUp = cappedMemoize(function(str, hint){
|
||||
return new BreakString(str).break(hint);
|
||||
}, 100);
|
||||
|
||||
shortDate = function(date){
|
||||
return moment(date).format(I18n.t("dates.medium.date_year"));
|
||||
|
|
|
@ -38,3 +38,4 @@
|
|||
//= require show-html.js
|
||||
//= require lock-on.js
|
||||
//= require ember-cloaking
|
||||
//= require break_string
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<%= stylesheet_link_tag 'embed' %>
|
||||
<%= javascript_include_tag 'break_string' %>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
|
@ -43,6 +44,16 @@
|
|||
for (i=0; i<cookedLinks.length; i++) {
|
||||
cookedLinks[i].target = "_blank";
|
||||
}
|
||||
|
||||
// Adjust all names
|
||||
var names = document.querySelectorAll('.username');
|
||||
for (i=0; i<names.length; i++) {
|
||||
var username = names[i].innerHTML;
|
||||
if (username) {
|
||||
names[i].innerHTML = new BreakString(username).break();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
})();
|
||||
|
|
54
vendor/assets/javascripts/break_string.js
vendored
Normal file
54
vendor/assets/javascripts/break_string.js
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
(function() {
|
||||
|
||||
var BREAK = "<wbr>​";
|
||||
|
||||
/**
|
||||
A class for intelligently breaking up strings at logical points.
|
||||
**/
|
||||
var BreakString = function(string) {
|
||||
this.string = string;
|
||||
};
|
||||
|
||||
BreakString.prototype.break = function(hint) {
|
||||
var hintPos = [],
|
||||
str = this.string;
|
||||
|
||||
if(hint) {
|
||||
hint = hint.toLowerCase().split(/\s+/).reverse();
|
||||
var current = 0;
|
||||
while(hint.length > 0) {
|
||||
var word = hint.pop();
|
||||
if(word !== str.substr(current, word.length).toLowerCase()) {
|
||||
break;
|
||||
}
|
||||
current += word.length;
|
||||
hintPos.push(current);
|
||||
}
|
||||
}
|
||||
|
||||
var rval = [],
|
||||
prev = str[0];
|
||||
rval.push(prev);
|
||||
for (var i=1;i<str.length;i++) {
|
||||
var cur = str[i];
|
||||
if(prev.match(/[^0-9]/) && cur.match(/[0-9]/)){
|
||||
rval.push(BREAK);
|
||||
} else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){
|
||||
rval.pop();
|
||||
rval.push(BREAK);
|
||||
rval.push(prev);
|
||||
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){
|
||||
rval.push(BREAK);
|
||||
} else if(hintPos.indexOf(i) > -1) {
|
||||
rval.push(BREAK);
|
||||
}
|
||||
|
||||
rval.push(cur);
|
||||
prev = cur;
|
||||
}
|
||||
return rval.join("");
|
||||
};
|
||||
|
||||
this.BreakString = BreakString;
|
||||
|
||||
}).call(this);
|
Loading…
Reference in a new issue