Add a new item to static list and auto refresh in angular js - angularjs

I want to display image based on the following codes:
app.controller('PostingCtrl', ['$scope','$http', '$q', function($scope, $http, $q) {
// Set of Photos
$scope.pictures = [
{src: 'images/sample1.jpg', desc: 'Sample Image 01'},
{src: 'images/sample2.jpg', desc: 'Sample Image 02'},
{src: 'images/sample3.jpg', desc: 'Sample Image 03'}
];
// initial image index
$scope._Index = 0;
// if a current image is the same as requested image
$scope.isActive = function (index) {
return $scope._Index === index;
};
// show a certain image
$scope.showPhoto = function (index) {
$scope._Index = index;
};
$scope.onCameraUpload = function() {
navigator.camera.getPicture(
function(imageInfo) {
var image = {src: imageInfo, desc: 'none'};
$scope.pictures.push.apply($scope.pictures, image);
},
function(message) {
alert('Failed because: ' + message);
},
{
quality: 50,
sourceType : Camera.PictureSourceType.CAMERA
}
);
};
}]);
The default images are loading well. However, I want to add another image to the list based on the image from camera upload given in the code and then automatically refresh it to the page.
I have tried as given in the code, but it did not work. Please help.
Edit:
Here is the html code
<section style="padding: 0 8px">
<!-- slider container -->
<div class="container slider">
<!-- enumerate all photos -->
<img ng-repeat="picture in pictures" class="slide" ng-swipe-right="showPrev()" ng-swipe-left="showNext()" ng-show="isActive($index)" ng-src="{{picture.src}}" />
<!-- extra navigation controls -->
<ul class="nav">
<li ng-repeat="picture in pictures" ng-class="{'active':isActive($index)}">
<img src="{{picture.src}}" alt="{{picture.desc}}" title="{{picture.desc}}" ng-click="showPhoto($index);" />
</li>
</ul>
</div>
</section>
<section style="padding: 0 8px 8px">
<ons-button modifier="normal" ng-click="onCameraUpload()" style="float: right; width: 48.5%;">
<ons-icon icon="camera"></ons-icon> Camera Upload
</ons-button>
</section>

I assume that that you need to replace your push command :
$scope.pictures.push(image);
If this is not working, please paste your HTML code taht is doing your loop.
This may be coming from that point if you binded it one way (with ::)
EDIT :
Here is how I get pictures from Cordova, my query is a bit different from yours... please verify yours
var options = {
quality: 50,
destinationType: Camera.DestinationType.NATIVE_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: false,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 700,
targetHeight: 700,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation: true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
d.resolve(imageData);
}, function(err) {
d.reject(err);
});

Related

Pie Chart using Chartjs in angularjs 1.5.8 with components not showing up in view

I want to create a dashboard like feature on my existing angular app using Chart.js. So, I created a view for the dashboard.
<md-content layout-padding layout-fill flex class="animated fadeIn">
<div layout-padding>
<md-card-title>
<md-card-title-text>
<span class="md-headline">Dashboard</span>
</md-card-title-text>
</md-card-title>
</div>
<md-divider></md-divider>
<div>
<md-grid-list md-cols-gt-md="12" md-cols-sm="3" md-cols-md="8"
md-row-height-gt-md="1:1" md-row-height="4:3"
md-gutter-gt-md="8px" md-gutter-gt-sm="4px" md-gutter="2px">
<md-grid-tile md-colspan-gt-sm="3" md-rowspan-gt-sm="3">
<pie-chart header="'Auths'" auth-counts="ctrl.authCounts" flex-gt-sm></pie-chart>
</md-grid-tile>
</md-grid-list>
</div>
</md-content>
The dashboard controller is loaded from the route (ui.router). The controller is empty at the moment.
//router
.state('home.dashboard', {
url: '/dashboard',
controller: 'DashboardController as ctrl',
templateUrl: baseUrl + 'Scripts/src/views/dashboard/dashboard.home.html'
})
function DashboardController() {
var ctrl = this;
ctrl.$onInit = function () {
console.log('testing');
ctrl.authCounts = [800, 200];
}
}
Then, the component pie-chart looks like the following.
(function () {
'use strict';
angular.module('chart')
.component('pieChart', {
controller: PieChartComponentController,
templateUrl: 'Scripts/src/views/dashboard/pie.chart.template.html',
bindings: {
header: '<',
authCounts: '<'
}
});
PieChartComponentController.$inject = ['$timeout'];
function PieChartComponentController($timeout) {
var $ctrl = this;
$ctrl.$onInit = function () {
var myPieChart;
var data;
}
$ctrl.isArrayEmpty = function (authCount) {
if (!authCount) return true;
return authCount.length === 0;
}
$ctrl.createChart = function () {
var pieData = {
labels: ["Purple","Green","Orange","Yellow"],
datasets: [
{
data: [20, 40, 10, 30],
backgroundColor: ["#878BB6","#4ACAB4","#FF8153","#FFEA88"]
}
]
};
var ctx = document.getElementById("myPieChart").getContext("2d");
var myPieChart = new Chart(ctx, {
type: 'pie',
data: pieData
});
} // end createChart()
}
})();
And the pie-chart template looks like the following.
<div layout-align="start center" ng-if="$ctrl.isArrayEmpty($ctrl.authCounts)">
Not enough information available to create the chart.
</div>
<canvas id="myPieChart" width="250" height="250" ng-init="$ctrl.createChart()" ng-if="$ctrl.isArrayEmpty($ctrl.authCounts)" flex-gt-sm></canvas>
</md-card-content>
It seems like everything is fine, but the canvas height and width is 0 and the chart is not rendered.
However, if I remove the pie-chart component, and put the <canvas> element outside in the dashboard template and the data in the dashboard, it renders the chart. But that's not what I want to do, though. I need to have component for each type. So, what am I doing wrong that's causing this behavior?
I also tried angular-chart.js that showed the same behavior. If I put the <canvas> element in the dashboard template, it displays the chart but not from inside pie-chart element. I need help troubleshooting it. I'm definitely missing something here but can't figure out what. Is the canvas being rendered before the data is provided, or is there something going on that I'm overlooking (or oblivious about)?

AngularJS ng-model not bound to Kendo UI dropdown when datasource data not in range

I'm developing a complex application using angularjs vs Kendo UI.
Here is the simple view:
<div id="example" ng-app="KendoDemos">
<div ng-controller="MyCtrl">
<h4>DropDown</h4>
<select kendo-drop-down-list="" options="options" ng-model="selectedProduct"></select>
</div>
</div>
Here is simple js:
<script>
angular.module("KendoDemos", [ "kendo.directives" ])
.controller("MyCtrl", function($scope){
$scope.selectedProduct = 51;
$scope.options = {
autoBind: true,
dataTextField: "ProductName",
dataValueField: "ProductID",
filter: "contains",
suggest: true,
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: "//demos.telerik.com/kendo-ui/service/Products"
}
},
pageSize: 50
}
};
})
</script>
Now you can see that $scope.selectedProduct = 51; and pageSize: 50 and that means that the dropdown is never get initialized with a predefined value. Feel free to update $scope.selectedProduct with the value 1 to 50 and see the dropdown actually initialized. This is the simplest code to highlight the problem. In real world this is pretty much the same scenario so I'd like to figure out how to make this binding work in an elegant way.
Here is a dojo: http://dojo.telerik.com/OpeqO
You can watch the selectedProduct and if the new value is null you can change the default.
$scope.$watch('selectedProduct',function(newVal,oldVal,$scope){
if(newVal==null){
$scope.selectedProduct = 1;
}
});

Couple of questions about fotorama

I found your Fotorama gallery script v4.6.4 and it is exactly what I was looking for.
I do have a couple of questions:
How can I move the Caption down so that it is not overlaying the bottom of an image?
I would like it to be completely separate i.e.
Main Image
Caption
Thumbs
Here is an image that shows how the caption is currently obscuring the lower part of the main image, seems worse the smaller the viewport:
Caption Overlaying Image
Currently the caption shows the image title & a download link for the displayed image.
I would like the link to point to the Flickr image page instead of the ability to download it.
My JS script is currently:
<script type="text/javascript">
$(function() {
var AddPhotosToCarousel = function(data) {
var imgs = [];
$.each(data.photoset.photo, function(index, photo) {
// TODO: use flickr.photos.getSizes
caption = photo['title'] +
' <small>(Download)</small>'
imgs.unshift({img: photo['url_m'],
thumb: photo['url_s'],
caption: caption});
});
$('.fotorama').fotorama({
data: imgs,
width: '100%',
maxwidth: '960',
maxheight: '100%',
ratio: '4/3',
nav: 'thumbs',
autoplay: 8000,
keyboard: 'true',
arrows: 'true',
transition: 'crossfade'
});
};
$.getJSON('https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=[API KEY]&photoset_id=[PHOTOSET ID]&format=json&extras=url_s,url_m,url_o&jsoncallback=?', AddPhotosToCarousel);
});
</script>
this forms a URL in the format:
https://farm2.staticflickr.com/1566/[IMAGE ID][SECRET]_o.jpg
but I would like it to form a URL in format:
https://www.flickr.com/photos/[USER]/[PHOTO ID]/in/dateposted-public/
Any help would be appreciated.
Kind regards..,
OK, I found this page:
Jquery JSON Flickr API Returning Photos in a Set
& altered my script to:
<script type="text/javascript">
$(function() {
var AddPhotosToCarousel = function(data) {
var imgs = [];
$.each(data.photoset.photo, function(index, photo) {
var a_href = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + photo.id + "/";
// TODO: use flickr.photos.getSizes
caption = photo['title'] +
' (Open)'
imgs.unshift({img: photo['url_m'],
thumb: photo['url_s'],
caption: caption});
});
$('.fotorama').fotorama({
data: imgs,
width: '100%',
maxwidth: '960',
maxheight: '100%',
ratio: '4/3',
nav: 'thumbs',
autoplay: 8000,
keyboard: 'true',
arrows: 'true',
transition: 'crossfade',
click: 'false'
});
};
$.getJSON('https://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=72157664523293315&api_key=449126625d797cace5a6d5a90532b741&extras=url_s,url_m&jsoncallback=?', AddPhotosToCarousel);
});
</script>
While this does build the direct image link, clicking on this link only advances the slideshow instead of opening the link.
You can see that I also included the click: 'false' option but this has not stopped the issue.
How do I stop the slideshow advancing when clicking the link?
Regards..,
Right, got question 2 sorted, seemed to be an issue with Chrome caching the page.
Script is now:
<script type="text/javascript">
$(function() {
var AddPhotosToCarousel = function(data) {
var imgs = [];
$.each(data.photoset.photo, function(index, photo) {
var a_href = "http://www.flickr.com/photos/1cm69/" + photo.id + "/";
// TODO: use flickr.photos.getSizes
caption = ' <small><a href="' + a_href + '" target=_blank>' + photo['title'] + '</a></small>'
imgs.unshift({img: photo['url_m'],
thumb: photo['url_s'],
caption: caption});
});
$('.fotorama').fotorama({
data: imgs,
width: '100%',
maxwidth: '960',
maxheight: '100%',
ratio: '4/3',
nav: 'thumbs',
autoplay: 8000,
keyboard: 'true',
arrows: 'true',
transition: 'crossfade'
});
};
$.getJSON('https://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=72157664523293315&api_key=449126625d797cace5a6d5a90532b741&extras=url_s,url_m&jsoncallback=?', AddPhotosToCarousel);
});
</script>
& working.
So only one question left, that of the Caption overlaying the bottom of the image.
Regards..,

Increase width of column in ui.grid

I have inserted some dummy object in Grid,The data displays in the grid found some issues. Here is My code:
html:
<div class="gridStyle" ui-grid="gridOptions"></div>
js:
$scope.myData = [{
JobId: '196',
siteType: 'abc',
Title: 'Happy womans day',
Message: 'hello',
devicetype: 'A',
jobDuration: '819',
totalMsgs: '2016',
msgsDelivered: 'Msg not found In the whole Body',
msgsFailed: '789',
jobCreatedDate: '11-03-2015',
jobCreatedBy: 'abc#abc.com'
}];
$scope.gridOptions = {
data: 'myData'
};
css :
.ui-grid-header-cell .ui-grid-cell-contents {
height: 48px;
white-space: normal;
-ms-text-overflow: clip;
-o-text-overflow: clip;
text-overflow: clip;
overflow: visible;
}
Data displays in the grid but only upto some fixed width. Unable to wrap the long text,
for eg:Msg not found In the whole Body, Displays as Msg not foun...........
How can I increase width of each and every column?
I am not able to wrap the long text lines into 2-3 lines, the whole text displays in one line only
The above css works only for headers
OK, a few things.
Firstly, to set the column width you need to use column definitions, then you can set a width in pixels or percentage on each. Refer http://ui-grid.info/docs/#/tutorial/201_editable as an example that has column widths.
Secondly, there is the ability to add tooltips, which are one way to show longer cells that don't fit in the space available. Refer http://ui-grid.info/docs/#/tutorial/117_tooltips
Thirdly, you can make the rows taller and therefore have space to wrap content within them. Be aware that all rows must be the same height, so you can't make only the rows that need it taller.
gridOptions.rowHeight = 50;
You'll also need to set the white-space attribute on the div so that it wraps, which you can do by setting a class in the cellTemplate, and then adding a style to the css.
A plunker as an example: http://plnkr.co/edit/kyhRm08ZtIKYspDqgyRa?p=preview
Set the Dependency for the javascript:
angular.module('app', [
'ngTouch',
'ui.grid',
'ui.grid.autoResize',
'ui.grid.resizeColumns'
])
for ui-grid 3.0, I am using this directive:
.directive('setOuterHeight',['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element) {
$timeout(function(){
// Get the Parent Divider.
var parentContainer = element.parent()[0];
console.log(parentContainer.offsetHeight);
// Padding of ui-grid-cell-contents is 5px.
// TODO: Better way to get padding height?
var topBottomPadding = 10;
//Get the wrapped contents rowHeight.
var rowHeight = topBottomPadding + parentContainer.offsetHeight;
var gridRowHeight = scope.grid.options.rowHeight;
// Get current rowHeight
if (!gridRowHeight ||
(gridRowHeight && gridRowHeight < rowHeight)) {
// This will OVERRIDE the existing rowHeight.
scope.grid.options.rowHeight = rowHeight;
}
},0);
}
};
}])
Under the controller:
.controller('MainCtrl',
['$scope', '$q', '$timeout', 'uiGridConstants',
function ($scope, $q, $timeout, uiGridConstants) {
$scope.gridOptions = {
enableVerticalScrollbar: uiGridConstants.scrollbars.NEVER,
enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
columnDefs: [
{name: 'firstName', width: '*'},
{name: 'lastName', width: '*'},
{
name: 'company', width: '*',
cellTooltip: function (row) {
return row.entity.company;
},
cellTemplate: '<div class="ui-grid-cell-contents wrap" title="TOOLTIP"><div><span class="label" set-row-height>{{row.entity.company}} </span></div><div>'
},
{name: 'employed', width: '*'}
],
enableColumnResize: true,
rowHeight: 10,
};
}]);
*Note the set-row-height directive for cellTemplate.
For CSS, you will have to put the white-space as normal:
.wrap {
white-space: normal;
}
.wrap .label {
display: inline-block;
}
Lastly, the HTML to change the Grid Height:
<div id="grid1" ui-grid="gridOptions" class="grid" ng-style="{height: (gridOptions.data.length*gridOptions.rowHeight)+32+'px'}" ui-grid-resize-columns ui-grid-auto-resize></div>
Plunker example is here:
http://plnkr.co/edit/OwgEfru0QyFaU1XJFezv?p=preview

Click one Panel, Hide other Panels - Ext JS

I have three separate buttons with controllers. When one of the buttons is clicked, a panel is created and displayed (with animation). Here's what one of my controllers looks like:
Ext.define('AM.controller.Prod_Select', {
extend: 'Ext.app.Controller',
init: function() {
this.control({
'#prod_select': {
click: this.prodSelect
}
});
},
prodSelect: function() {
var subPanel = Ext.create('Ext.panel.Panel', {
width: 200,
height: 160,
html: '<center><p class="sub_panel_text "> Link <br /> Link <br /> Link </p> </center>',
bodyStyle: 'background:#010a4d',
border:false,
floating: true,
shadow: false,
style: 'opacity: 0;',
x: 770,
y: 75,
cls: 'sub_panel'
});
subPanel.show();
subPanel.animate({
duration: 1000,
to: {
opacity: .6,
x: 800,
y: 75
}
});
console.log('Clicked Prod');
}
});
This works just fine, but currently I can click one button x amount of times, and it will create x amount of panels. What I want out of this controller, however, is to create only a single panel from a click AND hide any other panel (with fade out) that may displayed already out of the three.
Is there any way to accomplish this? Thanks for reading!
You could add a property to the subPanel so it can be easily selected using ComponentQuery, then iterate through the query's results and hide the other panels.
var subPanel = Ext.create('Ext.panel.Panel', {
someCustomProperty: 'someCustomValue',
...
});
var allPanels = Ext.ComponentQuery.query('panel[someCustomProperty="someCustomValue"]');
Ext.Array.each(allPanels, function(panel) {
if (panel === subPanel) {
//show it
} else {
//hide it
}
});

Resources