Got rid of crazy user name split

instead decorate with <wbr> to hint on best
spot to split
This commit is contained in:
Sam 2013-12-23 11:11:41 +11:00
parent 90077ae98d
commit d4819c3a65
5 changed files with 29 additions and 38 deletions

View File

@ -9,7 +9,7 @@ Handlebars.registerHelper('breakUp', function(property, options) {
prop = Ember.Handlebars.get(this, property, options);
if (!prop) return "";
return Discourse.Formatter.breakUp(prop, 13);
return Discourse.Formatter.breakUp(prop);
});
/**

View File

@ -6,39 +6,30 @@ Discourse.Formatter = (function(){
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
shortDate, shortDateNoYear, tinyDateYear, breakUp;
breakUp = function(string, maxLength){
if(string.length <= maxLength) {
return string;
breakUp = function(str){
var rval = [];
var prev = str[0];
var cur;
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("<wbr>");
} else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){
rval.pop();
rval.push("<wbr>");
rval.push(prev);
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){
rval.push("<wbr>");
}
var firstPart = string.substr(0, maxLength);
// work backward to split stuff like ABPoop to AB Poop
var i;
for(i=firstPart.length-1;i>0;i--){
if(firstPart[i].match(/[A-Z]/)){
break;
}
rval.push(cur);
prev = cur;
}
// work forwards to split stuff like ab111 to ab 111
if(i===0) {
for(i=1;i<firstPart.length;i++){
if(firstPart[i].match(/[^a-z]/)){
break;
}
}
}
return rval.join("");
if (i > 0 && i < firstPart.length) {
var offset = 0;
if(string[i] === "_") {
offset = 1;
}
return string.substr(0, i + offset) + " " + string.substring(i + offset);
} else {
return firstPart + " " + string.substr(maxLength);
}
};
shortDate = function(date){

View File

@ -4,7 +4,7 @@
<div>
<a href='/users/{{unbound username}}'>{{avatar this imageSize="large"}}</a>
</div>
<h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{breakUp username}}</a></h5>
<h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{{breakUp username}}}</a></h5>
</div>
</div>
<div class='span11 topic-body'>

View File

@ -22,7 +22,7 @@
{{#unless userDeleted}}
<div {{bindAttr class=":contents byTopicCreator:topic-creator :trigger-expansion"}}>
<a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{avatar this imageSize="large"}}</a>
<h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp username}}</a></h3>
<h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{{breakUp username}}}</a></h3>
{{#if showName}}
<h3><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp name}}</a></h3>

View File

@ -192,13 +192,13 @@ test("updateRelativeAge", function(){
test("breakUp", function(){
var b = function(s){ return Discourse.Formatter.breakUp(s,5); };
var b = function(s){ return Discourse.Formatter.breakUp(s); };
equal(b("hello"), "hello");
equal(b("helloworld"), "hello world");
equal(b("HeMans"), "He Mans");
equal(b("he_man"), "he_ man");
equal(b("he11111"), "he 11111");
equal(b("HRCBob"), "HRC Bob");
equal(b("helloworld"), "helloworld");
equal(b("HeMans11"), "He<wbr>Mans<wbr>11");
equal(b("he_man"), "he_<wbr>man");
equal(b("he11111"), "he<wbr>11111");
equal(b("HRCBob"), "HRC<wbr>Bob");
});