Project

General

Profile

Feature #29824 » initials-front-end.patch

Anonymous, 2019-01-26 12:16

View differences:

public/javascripts/application.js (working copy)
926 926
  $(window).resize(setFilecontentContainerHeight);
927 927
}
928 928

  
929

  
930
function avatarInitials() {
931
  let objects = document.querySelectorAll(".gravatar, .gravatar-child");
932
  let buffer = {};
933
  for (let i = 0; i < objects.length; i++) {
934
    if (objects[i].tagName.toLowerCase() === "img") {
935
      if (buffer[objects[i].title] == null) buffer[objects[i].title] = [objects[i]];
936
      else buffer[objects[i].title].push(objects[i]);
937
    }
938
  }
939
  for (let i = 0; i < Object.keys(buffer).length; i++) {
940
    let avatarLink = buffer[Object.keys(buffer)[i]][0].src;
941
    let elements = buffer[Object.keys(buffer)[i]];
942
    let avatarUsername = buffer[Object.keys(buffer)[i]][0].title;
943
    buffer[Object.keys(buffer)[i]].avatarURL = avatarLink;
944
    if (!avatarLink.match("(identicon)|(wavatar)|(monsterid)|(retro)|(mm)")) {
945
      checkFile(buffer[Object.keys(buffer)[i]][0].src, function(error, response) {
946
        if (error) {
947
          let initials = (avatarUsername.match(" ")) ? avatarUsername.split(" ")[0].charAt(0) + avatarUsername.split(" ")[1].charAt(0) : avatarUsername.charAt(0).toUpperCase();
948
          let extractedMD5 = avatarLink.substring(avatarLink.lastIndexOf("/") + 1, avatarLink.lastIndexOf("?"));
949
          let md5toHex = (parseInt(parseInt(extractedMD5, 36).toExponential().slice(2,-5),10) & 0xFFFFFF).toString(16);
950
          for (let i = 0; i < elements.length; i++) {
951
            elements[i].setAttribute("src", generateAvatarOBJ(elements[i].clientWidth, initials, findForegroundYIQ(md5toHex), "#" + md5toHex));
952
          }
953
        }
954
      });
955
    }
956
  }
957

  
958
  function checkFile(url, callback) {
959
    let request = new XMLHttpRequest();
960
    request.open('GET', url + "404");
961
    request.onload = function () {
962
      if (request.status < 200 || request.status > 299) callback({msg: "Error: Status " + request.status + " on resource " + url, code: request.status});
963
      else callback(null, request);
964
    }
965
    request.send();
966
  }
967

  
968
  function findForegroundYIQ(color) {
969
    return ((((parseInt(color.substr(0,2),16)*299) + (parseInt(color.substr(2,2),16)*587) + (parseInt(color.substr(4,2),16)*114))/1000)>=128) ? 'black' : 'white';
970
  }
971

  
972
  function generateAvatarOBJ(size, text, hexText, hexBg, dataURI) {
973
    text = text || '';
974
    size = size || 60;
975
    canvas = document.createElement('canvas');
976
    canvas.width = size;
977
    canvas.height = size;
978
    context = canvas.getContext("2d");
979
    context.fillStyle = hexBg;
980
    context.fillRect(0, 0, canvas.width, canvas.height);
981
    context.font = Math.round(canvas.width/1.5) + "px Arial";
982
    context.textAlign = "center";
983
    context.fillStyle = hexText;
984
    context.fillText(text, size/2, size/1.35);
985
    dataURI = canvas.toDataURL();
986
    canvas = null;
987
    return dataURI;
988
  }
989
}
990

  
991

  
929 992
$(document).ready(setupAjaxIndicator);
930 993
$(document).ready(hideOnLoad);
931 994
$(document).ready(addFormObserversForDoubleSubmit);
......
933 996
$(document).ready(setupAttachmentDetail);
934 997
$(document).ready(setupTabs);
935 998
$(document).ready(setupFilePreviewNavigation);
999
$(document).ready(avatarInitials);
(1-1/3)