Skip to content

Commit

Permalink
show image faster, open url
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Jan 14, 2024
1 parent 1a6d648 commit ac524c8
Show file tree
Hide file tree
Showing 5 changed files with 470 additions and 20 deletions.
49 changes: 49 additions & 0 deletions application/assets/images/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions application/assets/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,41 @@ const helper = (() => {
}
};

let readFile = (filepath, callback) => {
let sdcard = "";

try {
sdcard = navigator.getDeviceStorage("sdcard");
} catch (e) {}

if ("b2g" in navigator) {
try {
sdcard = navigator.b2g.getDeviceStorage("sdcard");
} catch (e) {}
}

let request = sdcard.get(filepath);
console.log(filepath);

request.onsuccess = function (e) {
const file = e.target.result; // The retrieved file
console.log(file);

const reader = new FileReader();

reader.addEventListener("load", function (event) {
callback(event.target.result);
console.log(event.target.result);
});

reader.readAsText(file);
};
request.onerror = function (e) {
// Handle errors
console.error("Error reading file:", e);
};
};

//delete file
let renameFile = function (filename, new_filename, callback) {
let sdcard = "";
Expand Down Expand Up @@ -450,6 +485,7 @@ const helper = (() => {
toaster,
add_script,
deleteFile,
readFile,
isOnline,
side_toaster,
renameFile,
Expand Down
230 changes: 230 additions & 0 deletions application/assets/js/vcard-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
var vCardParser = (function () {
var fieldPropertyMapping = {
"TITLE": "title",
"TEL": "telephone",
"FN": "displayName",
"N": "name",
"EMAIL": "email",
"CATEGORIES": "categories",
"ADR": "address",
"URL": "url",
"NOTE": "notes",
"ORG": "organization",
"BDAY": "birthday",
"PHOTO": "photo",
};

function lookupField(context, fieldName) {
var propertyName = fieldPropertyMapping[fieldName];

if (!propertyName && fieldName !== "BEGIN" && fieldName !== "END") {
context.info("define property name for " + fieldName);
propertyName = fieldName;
}

return propertyName;
}

function removeWeirdItemPrefix(line) {
// sometimes lines are prefixed by "item" keyword like "item1.ADR;type=WORK:....."
return line.substring(0, 4) === "item"
? line.match(/item\d\.(.*)/)[1]
: line;
}

function singleLine(context, fieldValue, fieldName) {
// convert escaped new lines to real new lines.
fieldValue = fieldValue.replace("\\n", "\n");

// append value if previously specified
if (context.currentCard[fieldName]) {
context.currentCard[fieldName] += "\n" + fieldValue;
} else {
context.currentCard[fieldName] = fieldValue;
}
}

function typedLine(context, fieldValue, fieldName, typeInfo, valueFormatter) {
var isDefault = false;

// strip type info and find out is that preferred value
typeInfo = typeInfo.filter(function (type) {
isDefault = isDefault || type.name === "PREF";
return type.name !== "PREF";
});

typeInfo = typeInfo.reduce(function (p, c) {
p[c.name] = c.value;
return p;
}, {});

context.currentCard[fieldName] = context.currentCard[fieldName] || [];

context.currentCard[fieldName].push({
isDefault: isDefault,
valueInfo: typeInfo,
value: valueFormatter ? valueFormatter(fieldValue) : fieldValue,
});
}

function commaSeparatedLine(context, fieldValue, fieldName) {
context.currentCard[fieldName] = fieldValue.split(",");
}

function dateLine(context, fieldValue, fieldName) {
// if value is in "19531015T231000Z" format strip time field and use date value.
fieldValue =
fieldValue.length === 16 ? fieldValue.substr(0, 8) : fieldValue;

var dateValue;

if (fieldValue.length === 8) {
// "19960415" format ?
dateValue = new Date(
fieldValue.substr(0, 4),
fieldValue.substr(4, 2),
fieldValue.substr(6, 2)
);
} else {
// last chance to try as date.
dateValue = new Date(fieldValue);
}

if (!dateValue || isNaN(dateValue.getDate())) {
dateValue = null;
context.error("invalid date format " + fieldValue);
}

context.currentCard[fieldName] = dateValue && dateValue.toJSON(); // always return the ISO date format
}

function structured(fields) {
return function (context, fieldValue, fieldName) {
var values = fieldValue.split(";");

context.currentCard[fieldName] = fields.reduce(function (p, c, i) {
p[c] = values[i] || "";
return p;
}, {});
};
}

function addressLine(context, fieldValue, fieldName, typeInfo) {
typedLine(context, fieldValue, fieldName, typeInfo, function (value) {
var names = value.split(";");

return {
// ADR field sequence
postOfficeBox: names[0],
number: names[1],
street: names[2] || "",
city: names[3] || "",
region: names[4] || "",
postalCode: names[5] || "",
country: names[6] || "",
};
});
}

function noop() {}

function endCard(context) {
// store card in context and create a new card.
context.cards.push(context.currentCard);
context.currentCard = {};
}

var fieldParsers = {
"BEGIN": noop,
"VERSION": noop,
"N": structured(["surname", "name", "additionalName", "prefix", "suffix"]),
"TITLE": singleLine,
"TEL": typedLine,
"EMAIL": typedLine,
"ADR": addressLine,
"NOTE": singleLine,
"NICKNAME": commaSeparatedLine,
"BDAY": dateLine,
"URL": singleLine,
"CATEGORIES": commaSeparatedLine,
"END": endCard,
"FN": singleLine,
"ORG": singleLine,
"UID": singleLine,
"PHOTO": singleLine,
};

function feedData(context) {
for (var i = 0; i < context.data.length; i++) {
var line = removeWeirdItemPrefix(context.data[i]);

var pairs = line.split(":"),
fieldName = pairs[0],
fieldTypeInfo,
fieldValue = pairs.slice(1).join(":");

// is additional type info provided ?
if (
fieldName.indexOf(";") >= 0 &&
line.indexOf(";") < line.indexOf(":")
) {
var typeInfo = fieldName.split(";");
fieldName = typeInfo[0];
fieldTypeInfo = typeInfo.slice(1).map(function (type) {
var info = type.split("=");
return {
name: info[0].toLowerCase(),
value: info[1].replace(/"(.*)"/, "$1"),
};
});
}

// ensure fieldType is in upper case
fieldName = fieldName.toUpperCase();

var fieldHandler = fieldParsers[fieldName];

if (fieldHandler) {
fieldHandler(
context,
fieldValue,
lookupField(context, fieldName),
fieldTypeInfo
);
} else if (fieldName.substring(0, 2) != "X-") {
// ignore X- prefixed extension fields.
context.info(
"unknown field " + fieldName + " with value " + fieldValue
);
}
}
}

function parse(data) {
var lines = data
// replace escaped new lines
.replace(/\n\s{1}/g, "")
// split if a character is directly after a newline
.split(/\r\n(?=\S)|\r(?=\S)|\n(?=\S)/);

var context = {
info: function (desc) {
console.info(desc);
},
error: function (err) {
console.error(err);
},
data: lines,
currentCard: {},
cards: [],
};

feedData(context);

return context.cards;
}

return {
parse: parse,
};
})();
1 change: 1 addition & 0 deletions application/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<script src="http://127.0.0.1/api/v1/shared/core.js"></script>
<script src="http://127.0.0.1/api/v1/shared/session.js"></script>
<script src="http://127.0.0.1/api/v1/apps/service.js"></script>
<script defer src="assets/js/vcard-parser.js"></script>

<script defer src="assets/js/pdf.min.js"></script>
<script defer src="assets/js/pdf.worker.js"></script>
Expand Down
Loading

0 comments on commit ac524c8

Please sign in to comment.